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.
import type { ComponentProps, ReactNode } from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { DemoLabel } from "@/components/text/demo-label"
const exampleGridVariants = cva("grid grid-cols-1 gap-x-4 gap-y-10", {
variants: {
/**
* The most columns, reached at `lg`: `3` (the default) steps 1, 2, 3;
* `2` steps 1, 2 for pieces too wide for a third; `1` keeps one wide
* column for full-width pieces.
*/
cols: {
1: "",
2: "sm:grid-cols-2",
3: "sm:grid-cols-2 lg:grid-cols-3",
},
},
defaultVariants: { cols: 3 },
})
/**
* 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.
*/
function ExampleGrid({
cols,
className,
...props
}: ComponentProps<"ul"> & VariantProps<typeof exampleGridVariants>) {
return (
<ul
role="list"
data-slot="example-grid"
className={cn(exampleGridVariants({ cols }), className)}
{...props}
/>
)
}
type ExampleGridItemProps = ComponentProps<"li"> & {
/** The line under the piece, naming it: "Example: Ceramic study". */
caption: ReactNode
/**
* The piece's registry name, written to `data-example` so a gallery can
* link the card to that piece's own page. Optional.
*/
name?: string
}
/**
* 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.
*/
function ExampleGridItem({
caption,
name,
className,
children,
...props
}: ExampleGridItemProps) {
return (
<li
data-slot="example-grid-item"
data-example={name}
className={cn("row-span-2 grid grid-rows-subgrid gap-3", className)}
{...props}
>
<div
data-slot="example-grid-frame"
className="relative isolate grid grid-cols-[minmax(0,1fr)] overflow-hidden rounded-(--tile-radius)"
>
{children}
</div>
<DemoLabel data-slot="example-grid-caption">{caption}</DemoLabel>
</li>
)
}
export { ExampleGrid, ExampleGridItem }
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>
)
}
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
// Paragraphs in body content and introductions. It has no width of its own:
// the layout sets the measure. The default sets the foreground color; muted
// steps it down. Over art or a colored ground, pass className="text-current"
// to keep the ink around it. The text under a section's title is
// SectionDescription.
const textVariants = cva("w-full min-w-0 text-balance", {
variants: {
/**
* The text color. `default` (the default) is `foreground`; `muted`
* is `muted-foreground`. For the ink of a colored ground or art, pass
* `className="text-current"`.
*/
variant: {
default: "text-foreground",
muted: "text-muted-foreground",
},
/**
* The text size and line height: `xs` 12px/20px for a footnote or fine
* print; `sm` 14px/24px for the line under a card or panel title; `md`
* (the default) 16px/28px for body copy; `lg` 18px/28px at every width;
* `xl` 18px/28px, then 20px/32px from `sm` for a hero introduction.
*/
size: {
xs: "text-xs/5",
sm: "text-sm/6",
md: "text-base/7",
lg: "text-lg/7",
xl: "text-lg/7 sm:text-xl/8",
},
/**
* Tabular figures: every digit takes the same width, so numbers line up
* in a column and a changing count does not shift the text.
*/
tabular: {
true: "tabular-nums",
false: "",
},
},
defaultVariants: { variant: "default", size: "md", tabular: false },
})
/**
* A paragraph in body content or an introduction, a `p` by default. For
* several paragraphs, pass `render={<div />}` and put your own `p` elements
* inside.
*
* The default is the `foreground` color at regular weight (400, inherited
* from the page unless a parent sets one), and the lines are balanced when it
* wraps. `size` sets the text and its line height: `xs` 12px/20px for a
* footnote or fine print; `sm` 14px/24px for the line under a card or panel
* title; `md` (the default) 16px/28px for body copy in cards, lists and rows;
* `lg` 18px/28px; or `xl` 18px/28px and 20px/32px from `sm` for a hero.
* `variant="muted"` uses the muted text color. On art or a colored ground,
* pass `className="text-current"` to keep the ink around it. `tabular` sets
* tabular figures for numbers that line up or change. The layout controls the
* text's width and spacing. The text under a section's title is
* `SectionDescription`.
*/
function Text({
className,
variant = "default",
size = "md",
tabular = false,
render,
...props
}: useRender.ComponentProps<"p"> & VariantProps<typeof textVariants>) {
return useRender({
defaultTagName: "p",
render,
state: { slot: "text", variant, size, tabular },
props: mergeProps<"p">(
{ className: cn(textVariants({ variant, size, tabular }), className) },
props
),
})
}
export { Text, textVariants }
import type { ComponentProps } from "react"
import { cn } from "cn"
/**
* A caption under a demo or gallery piece, in medium-weight mono at
* 12px/20px. Long captions wrap with balanced last lines. The caption
* inherits its ground's text color; native paragraph props pass through.
*/
function DemoLabel({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="demo-label"
className={cn("font-mono text-xs/5 font-medium text-pretty", className)}
{...props}
/>
)
}
export { DemoLabel }
Installation
npx shadcn@latest add @01orbital/example-gridUsage
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-demoAnatomy
Example Grid is built from 2 parts. In the example they nest like this:
ExampleGrid
āā ExampleGridItemExampleGrid: A grid of captioned pieces, such as the examples under a demo: one column on phones, two fromsm, three fromlg(fewer withcols). EachExampleGridItemspans 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 withabsolute(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