Example Grid

A grid of captioned pieces, one column on phones to three on desktops: every card in a row is the same height and every caption sits on the same line.

Installation

npx shadcn@latest add @01orbital/example-grid

Usage

import {
  ExampleGrid,
  ExampleGridItem,
} from "@/components/grid/example-grid"
import { Text } from "@/components/text/text"

const COPY = {
  items: [
    {
      caption: "A short piece: its card still matches the row",
      body: "Two lines of text.",
    },
    {
      caption: "A tall piece: the row grows to fit it",
      body: "Five lines of text, so this card sets the height of the row for the cards beside it, and every caption below lines up.",
    },
    {
      caption: "A piece with its own background fills the frame",
      body: "Its ground reaches every edge of the frame.",
    },
  ],
} as const

/** ExampleGrid with three pieces of different heights: the cards and captions line up. */
export default function ExampleGridDemo() {
  return (
    <ExampleGrid className="w-full max-w-5xl">
      {COPY.items.map((item, index) => (
        <ExampleGridItem key={item.caption} caption={item.caption}>
          <div
            className={
              index === 2
                ? "bg-primary p-6 text-primary-foreground"
                : "bg-muted p-6"
            }
          >
            <Text className="text-current">{item.body}</Text>
          </div>
        </ExampleGridItem>
      ))}
    </ExampleGrid>
  )
}

This example installs with everything it uses:

npx shadcn@latest add @01orbital/example-grid-demo

Anatomy

Example Grid is built from 2 parts. In the example they nest like this:

ExampleGrid
└─ ExampleGridItem
  • ExampleGrid: A grid of captioned pieces, such as the examples under a demo: one column on phones, two from sm, three from lg (fewer with cols). Each ExampleGridItem spans two rows of a subgrid, so every card in a row is the same height and every caption sits on the same line.
  • ExampleGridItem: One piece and its caption. The piece fills a frame with the tile radius and stretches to the tallest card in its row; a piece with its own background fills the frame, a smaller one can center itself inside. The frame's one column can shrink below the piece's natural width, so wide content (a code block) wraps or scrolls inside the piece instead of being cut off at the frame's edge. The frame is positioned and isolated, so a piece placed with absolute (a bare scene texture) stays inside it.

API reference

ExampleGrid

A grid of captioned pieces, such as the examples under a demo: one column on phones, two from sm, three from lg (fewer with cols). Each ExampleGridItem spans two rows of a subgrid, so every card in a row is the same height and every caption sits on the same line.

Prop

Type

It also accepts the HTML attributes of the element it renders.

ExampleGridItem

One piece and its caption. The piece fills a frame with the tile radius and stretches to the tallest card in its row; a piece with its own background fills the frame, a smaller one can center itself inside. The frame's one column can shrink below the piece's natural width, so wide content (a code block) wraps or scrolls inside the piece instead of being cut off at the frame's edge. The frame is positioned and isolated, so a piece placed with absolute (a bare scene texture) stays inside it.

Prop

Type

It also accepts the HTML attributes of the element it renders.

Dependencies

npm: class-variance-authority, cn. The CLI installs them for you.

Requires these pieces. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:

Last updated on