Stack

Children in a column with even space between them.

Space things out with Stack and Inline, not margins. The gap is a step on the system’s spacing scale, so the space between things means the same everywhere: 2xs holds a title to its description, lg separates the parts of a card, 3xl separates the sections of a page.

Preview
basic.tsx
import { Button } from "~/components/ui/button"
import { Inline } from "~/components/ui/inline"
import { Stack } from "~/components/ui/stack"
import { Text, textVariants } from "~/components/ui/text"

export default function Example() {
  return (
    <Stack gap="lg" className="w-full max-w-sm">
      <Stack gap="2xs">
        <h3 className={textVariants({ size: "md", weight: "semibold", family: "heading" })}>
          Weekend edition
        </h3>
        <Text size="sm" tone="muted">
          Twenty-four pages, out Saturday.
        </Text>
      </Stack>
      <Text size="sm">
        The front page leads with the harbor plan. The long read moves to page 8, and the puzzles
        take the back two pages.
      </Text>
      <Inline gap="sm" justify="end">
        <Button variant="outline">Preview</Button>
        <Button>Send to print</Button>
      </Inline>
    </Stack>
  )
}

It renders a <div>; give it render for another element, like <ul />. Children stretch to its width unless align says otherwise.

The scale

Every step, from 3xs to 4xl, as the gap between two squares.

Preview
scale.tsx
import { Inline } from "~/components/ui/inline"
import { spaceSteps, Stack } from "~/components/ui/stack"
import { Text } from "~/components/ui/text"

export default function Example() {
  return (
    <Stack gap="sm" className="w-full">
      {spaceSteps.map((step) => (
        <Inline key={step} gap="xl" wrap={false}>
          <Text render={<span />} size="xs" family="mono" tone="muted" className="w-10">
            {step}
          </Text>
          <Inline gap={step} wrap={false} aria-hidden="true">
            <span className="size-4 bg-primary" />
            <span className="size-4 bg-primary" />
          </Inline>
        </Inline>
      ))}
    </Stack>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesNothing else in the system.
Used byInlineprimitive
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20tailwind-variants@4.1.0

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/stack.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 tailwind-variants@4.1.0

Then copy the source:

components/ui/stack.tsx
import { renderElement, type RenderProp } from "@ui-sdk/react/primitive"
import type * as React from "react"
import { tv, type VariantProps } from "tailwind-variants"

import { cn } from "~/lib/utils"

/** The spacing scale's steps, smallest first. Their sizes are the theme's `--space-*` tokens. */
const spaceSteps = ["3xs", "2xs", "xs", "sm", "md", "lg", "xl", "2xl", "3xl", "4xl"] as const

/** The spacing scale as gaps, shared by Stack and Inline. */
const gaps: Record<"none" | (typeof spaceSteps)[number], string> = {
  none: "gap-0",
  "3xs": "gap-3xs",
  "2xs": "gap-2xs",
  xs: "gap-xs",
  sm: "gap-sm",
  md: "gap-md",
  lg: "gap-lg",
  xl: "gap-xl",
  "2xl": "gap-2xl",
  "3xl": "gap-3xl",
  "4xl": "gap-4xl",
}

const stackVariants = tv({
  base: "flex min-w-0 flex-col",
  variants: {
    gap: gaps,
    align: {
      start: "items-start",
      center: "items-center",
      end: "items-end",
      stretch: "items-stretch",
    },
  },
  defaultVariants: { gap: "md", align: "stretch" },
})

type StackVariants = VariantProps<typeof stackVariants>

type StackProps = React.ComponentProps<"div"> & {
  /** The space between children, a step on the spacing scale. */
  gap?: NonNullable<StackVariants["gap"]>
  /** Where children sit across the column. They stretch to its width unless you say otherwise. */
  align?: NonNullable<StackVariants["align"]>
  /** The element to render instead of a `<div>`, like `<section />` or `<ul />`. */
  render?: RenderProp
}

/** Children in a column with even space between them. Space things out with this, not margins. */
function Stack({ gap, align, render, className, ...props }: StackProps) {
  return renderElement(
    "div",
    { "data-slot": "stack", className: cn(stackVariants({ gap, align }), className), ...props },
    render,
  )
}

export { gaps, spaceSteps, Stack, stackVariants }
export type { StackProps }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

spaceSteps

The spacing scale's steps, smallest first. Their sizes are the theme's --space-* tokens.

Takes the props of a native div.

Stack

Children in a column with even space between them. Space things out with this, not margins.

alignours

Where children sit across the column. They stretch to its width unless you say otherwise.

Type NonNullable<"center" | "start" | "end" | "stretch">

gapours

The space between children, a step on the spacing scale.

Type NonNullable<"xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "none" | "3xs" | "2xs" | "4xl">

renderours

The element to render instead of a <div>, like <section /> or <ul />.

Type RenderProp