Skip to content
Primitives

Flex

The general flexbox wrapper — direction, alignment, justification, wrapping, and a gap from the shared scale.

Motionnone — layout primitive

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Flex, flexVariants } from "kineto/flex";

Preview

Logo
Docs
Pricing
Sign in
tsx
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/cn";
import { gapClass, type Gap } from "@/lib/gap";

const flexVariants = cva("flex min-w-0", {
  variants: {
    direction: {
      row: "flex-row",
      "row-reverse": "flex-row-reverse",
      col: "flex-col",
      "col-reverse": "flex-col-reverse",
    },
    align: {
      start: "items-start",
      center: "items-center",
      end: "items-end",
      stretch: "items-stretch",
      baseline: "items-baseline",
    },
    justify: {
      start: "justify-start",
      center: "justify-center",
      end: "justify-end",
      between: "justify-between",
      around: "justify-around",
      evenly: "justify-evenly",
    },
    wrap: {
      true: "flex-wrap",
      false: "flex-nowrap",
    },
    // `inline-flex` wins over the base `flex` via tailwind-merge (cn dedupes display utilities).
    inline: {
      true: "inline-flex",
      false: "",
    },
  },
  defaultVariants: {
    direction: "row",
    align: "stretch",
    justify: "start",
    wrap: false,
    inline: false,
  },
});

export interface FlexProps
  extends React.HTMLAttributes<HTMLDivElement>,
    Omit<VariantProps<typeof flexVariants>, "wrap" | "inline"> {
  /** Space between children, from the shared gap scale. */
  gap?: Gap;
  /** Allow children to wrap onto multiple lines. */
  wrap?: boolean;
  /** Render as `inline-flex` instead of `flex`. */
  inline?: boolean;
  /** Render as the child element instead of a `<div>`. */
  asChild?: boolean;
}

/**
 * The general flexbox wrapper — direction, alignment, justification, wrapping, and a `gap` from the
 * shared scale. Reach for `Stack` when you just want a vertical/horizontal run with consistent gaps;
 * use `Flex` when you need full control (e.g. `justify="between"` toolbars).
 */
const Flex = React.forwardRef<HTMLDivElement, FlexProps>(
  (
    { className, direction, align, justify, wrap, inline, gap = "none", asChild = false, ...props },
    ref
  ) => {
    const Comp = asChild ? Slot : "div";
    return (
      <Comp
        ref={ref}
        className={cn(
          flexVariants({ direction, align, justify, wrap, inline }),
          gapClass[gap],
          className
        )}
        {...props}
      />
    );
  }
);
Flex.displayName = "Flex";

export { Flex, flexVariants };

src/components/ui/flex.tsx

Props

PropTypeDefaultDescription
direction"row" | "row-reverse" | "col" | "col-reverse""row"Flex direction.
gap"none" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl""none"Space between children, from the shared gap scale.
align"start" | "center" | "end" | "stretch" | "baseline""stretch"Cross-axis alignment (align-items).
justify"start" | "center" | "end" | "between" | "around" | "evenly""start"Main-axis distribution (justify-content).
wrapbooleanfalseAllow children to wrap onto multiple lines.
inlinebooleanfalseRender as inline-flex instead of flex.
asChildbooleanfalseRender as the child element instead of a <div>.