Skip to the content

Layout

stack

A flex row or column with a gap from a fixed scale, and props for alignment, distribution and wrapping.

Layout

When to use it

Spacing a run of siblings evenly: form fields, a button row, a header with actions. For columns that reflow by width use grid; for unequal tiles use bento-grid.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Layout page.

Bento grid

One theme

Mode, roundness and highlight colour reach every cell.

Copied in

The source is yours.

Accessible

Behaviour from Radix.

Any width

The grid follows its container, not the screen: one column, then two, then four.

Marquee

ControlsBento gridsMotionTransitions3DChartsThemesDark mode

Split

Media beside text

Stacks when its container is narrow.

Grid and stack

1
2
3
4
5
6

As many columns as fit, each at least 7rem wide.

Add it

npx shadcn@latest add @rivier-ui/stack

The registry is named once in components.json: see Installation.

Example

Import it from @/components/layout/stack.

<Stack gap="lg">
  <h2>Title</h2>
  <p>Body</p>
</Stack>
<Stack direction="horizontal" gap="sm" align="center" justify="between" wrap>
  <span>Plan</span>
  <Button size="sm">Change</Button>
</Stack>
<Stack asChild gap="xs"><ul><li>One</li><li>Two</li></ul></Stack>

What it needs

Plain markup: it can stay a server component. Packages: class-variance-authority, radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/layout/stack.tsx
/**
 * @name stack
 * @category layout
 * @description A flex row or column with a gap from a fixed scale, and props for alignment, distribution and wrapping.
 * @when Spacing a run of siblings evenly: form fields, a button row, a header with actions. For columns that reflow by width use grid; for unequal tiles use bento-grid.
 * @example
 * <Stack gap="lg">
 *   <h2>Title</h2>
 *   <p>Body</p>
 * </Stack>
 * <Stack direction="horizontal" gap="sm" align="center" justify="between" wrap>
 *   <span>Plan</span>
 *   <Button size="sm">Change</Button>
 * </Stack>
 * <Stack asChild gap="xs"><ul><li>One</li><li>Two</li></ul></Stack>
 */
import { Slot } from "radix-ui";
import { cva, type VariantProps } from "class-variance-authority";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

export const stackVariants = cva("flex min-w-0", {
  variants: {
    direction: {
      vertical: "flex-col",
      horizontal: "flex-row",
    },
    gap: {
      none: "gap-0",
      xs: "gap-1",
      sm: "gap-2",
      md: "gap-4",
      lg: "gap-6",
      xl: "gap-10",
    },
    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",
    },
    wrap: {
      true: "flex-wrap",
      false: "",
    },
  },
  defaultVariants: { direction: "vertical", gap: "md", wrap: false },
});

export type StackProps = ComponentProps<"div"> &
  VariantProps<typeof stackVariants> & {
    /** Render the child element (ul, nav, form) as the stack. */
    asChild?: boolean;
  };

export function Stack({ className, direction, gap, align, justify, wrap, asChild = false, ...props }: StackProps) {
  const Comp = asChild ? Slot.Root : "div";
  return <Comp data-slot="stack" className={cn(stackVariants({ direction, gap, align, justify, wrap }), className)} {...props} />;
}