8
Components

Flex

A utility-first and customizable flexbox component with support for alignment, direction, wrapping, and spacing.

1
2
3

Introduction

The Flex component provides a simple and intuitive way to create flexbox layouts in your Next.js applications. Built on top of Tailwind CSS utility classes, it offers a convenient API for controlling flex container properties without having to remember all the flexbox utility classes.

Flex containers are one of the most powerful layout tools in modern web design. This component makes it easy to implement complex flex layouts with a clean, consistent API.

Installation

Install the flex component using:

npx axionjs-ui add flex

File Structure

Here’s the file structure for the Flex component:

flex.tsx

Variants

Row Layout (Default)

1
2
3

Column Layout

A
B
C

Responsive Layout

Card 1
Card 2
Card 3

Flex Wrap (Grid-like)

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9

Examples

Card with Flex Layout

Card Title
Card description and content layout
Image

This card uses Flex for layout. All content is structured with the Flex component for proper spacing and alignment.

Chat Interface

Messages
Recent conversations
U1
User 1
5m ago
This is a message preview that demonstrates how to use Flex for chat interfaces
U2
User 2
10m ago
This is a message preview that demonstrates how to use Flex for chat interfaces
U3
User 3
15m ago
This is a message preview that demonstrates how to use Flex for chat interfaces

Dashboard Stats

Dashboard
Your stats for this month
Total Revenue
$45,231.89
+20.1%
Subscriptions
2,350
+12.3%
Active Users
1,294
+8.2%
Conversion Rate
3.8%
+1.4%

Form Layout

Contact Form
Get in touch with our team

Holy Grail Layout

Header

Main Content

This is a classic "Holy Grail" layout with a header, footer, main content area, and fixed-width sidebars.

Card 1
Card 2
Card 3
Card 4
Card 5
Card 6
Footer

Props

The Flex component comes with the following customizable props:

PropTypeDefault
direction
"row" | "column" | "row-reverse" | "column-reverse"
"row"
align
"start" | "center" | "end" | "stretch" | "baseline"
"stretch"
justify
"start" | "center" | "end" | "between" | "around" | "evenly"
"start"
gap
"none" | "sm" | "md" | "lg" | "xl"
"none"
wrap
"true" | "false"
"false"
asChild
boolean
false
className
string
-
children
React.ReactNode
-

Customization

The Flex component can be customized further by:

// In your flex.tsx file
const flexVariants = cva("flex", {
  variants: {
    // Other variants
    gap: {
      none: "gap-0",
      xs: "gap-1",     // Added extra small
      sm: "gap-2",
      md: "gap-4",
      lg: "gap-6",
      xl: "gap-8",
      xxl: "gap-12",   // Added extra large
    },
  },
  // Default variants
});

Use Cases

The Flex component is perfect for:

  • Layouts: Create complete page layouts with headers, sidebars, and content areas.
  • Navigation: Build responsive navigation bars and menus.
  • Card Arrangements: Organize card components in grids or rows.
  • Form Layout: Structure form fields with consistent spacing and alignment.
  • Content Alignment: Control the positioning and spacing of UI elements.
  • Media Displays: Create galleries, image grids, and video layouts.
  • Responsive Design: Build layouts that adapt to different screen sizes.

Accessibility

The Flex component follows these accessibility best practices:

  • Semantic HTML: Uses appropriate HTML elements for layout
  • Screen Reader Friendly: Maintains a logical document flow
  • Keyboard Navigation: Preserves natural tab order of elements
  • Focus Indicators: Retains proper focus handling

When using Flex for complex layouts, ensure that the content’s reading order makes sense when navigating with a screen reader or keyboard.

Features

The Flex component provides the following features:

  • Multiple Directions: Row, column, row-reverse, and column-reverse layouts.
  • Alignment Control: Fine-tuned control over how items align on both axes.
  • Spacing Options: Predefined gap sizes for consistent spacing.
  • Wrapping Control: Enable or disable wrapping for grid-like layouts.
  • Composition Support: Nest Flex containers for complex layouts.
  • Responsive Design: Compatible with Tailwind’s responsive modifiers.
  • Polymorphic Rendering: Use any component as a flex container with asChild.

Conclusion

The Flex component is a versatile, accessible, and lightweight solution for creating flexible layouts in your application. Its intuitive API simplifies the use of CSS Flexbox, making it easy to create responsive and adaptive layouts while maintaining a consistent design language across your Next.js application.