Card
A box pencilled around a block of content, with a ruled-off footer: plain, lifted off a hatched shadow, or washed with hatching, with a media slot and a pressable, selectable mode.
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
export default function CardDemo() {
return (
<div className="grid w-full gap-10 md:grid-cols-2">
<Card variant="lifted" seed="card-a">
<CardHeader>
<CardTitle>Ink refill</CardTitle>
<CardDescription>Medium point, blue. Writes about two kilometres.</CardDescription>
<CardAction>
<Badge variant="secondary" seed="card-badge">
In stock
</Badge>
</CardAction>
</CardHeader>
<CardContent>
<p className="text-ink-2">Fits most clicky pens. Comes in a paper sleeve, not plastic.</p>
</CardContent>
<CardFooter>
<Button size="sm" seed="card-buy">
Add to basket
</Button>
<Button size="sm" variant="ghost" seed="card-save">
Save for later
</Button>
</CardFooter>
</Card>
<Card size="sm" radius={12} seed="card-b">
<CardHeader>
<CardTitle>Rounded, smaller</CardTitle>
<CardDescription>size="sm" with a 12px radius.</CardDescription>
</CardHeader>
<CardContent>
<p className="text-ink-2">Cards take the same pen settings as everything else.</p>
</CardContent>
</Card>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ card"use client";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { useId, useMemo, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { Stroke } from "@/lib/ink";
import { InkOutline } from "@/lib/ink-outline";
import { hatchStrokes, roundedRectPath } from "@/lib/ink-sketch";
import { Separator } from "@/components/ui/separator";
type Variant = "outline" | "lifted" | "wash";
/**
* A box pencilled around a block of content. The outline is decoration, so
* it's drawn at a lighter pressure than a control's.
*
* - `outline`: just the pencilled box.
* - `lifted`: the box stands off a hatched shadow, the way a button does
* (`shadow="solid"` blacks it in; `interactive` cards lift further on hover).
* - `wash`: the box is lightly hatched across, like a note.
*
* `interactive` makes the whole card something to press: it rises on hover
* and takes a focus ring. Give it `render={<a href="…" />}` to make it a link.
* `selected` goes over the outline in full ink, for cards that are picked.
*/
function Card({
className,
size = "default",
variant = "outline",
interactive = false,
selected = false,
render,
children,
seed,
roughness,
passes,
radius,
corners,
fill,
shadow,
draw,
weight,
speed,
...props
}: useRender.ComponentProps<"div"> & Pen & { size?: "default" | "sm"; variant?: Variant; interactive?: boolean; selected?: boolean; seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, fill, shadow, draw, weight, speed });
// Only a lifted card has a shadow, and only a wash a fill, unless asked: a
// provider's shadow or fill is for controls, not every card on the page.
const lift = shadow ?? (variant === "lifted" ? "hatch" : "none");
const wash = fill ?? (variant === "wash" ? "hatch" : undefined);
return useRender({
defaultTagName: "div",
props: mergeProps<"div">(
{
"data-slot": "card",
"data-size": size,
"data-variant": variant,
"data-selected": selected ? "" : undefined,
className: cn(
"group/card relative flex flex-col gap-(--card-spacing) py-(--card-spacing) text-base text-card-foreground [--card-spacing:--spacing(6)] has-data-[slot=card-footer]:pb-0 data-[size=sm]:[--card-spacing:--spacing(4)]",
interactive && "ink-within cursor-pointer outline-none transition-transform duration-(--dur-hover) ease-out hover:-translate-y-0.5 focus-visible:-translate-y-0.5 active:translate-y-0 motion-reduce:transition-none",
className,
),
children: (
<>
<InkOutline pen={pen} seed={seed} estimate={[360, 220]} maxRadius={18} focusPass={interactive} className={cn("text-ink-line", selected && "text-ink")}>
{({ w, h, r, s }) => <CardInk w={w} h={h} r={r} s={s} lift={lift} wash={wash} interactive={interactive} draw={pen.draw} />}
</InkOutline>
{children}
</>
),
} as ComponentProps<"div">,
props,
),
render,
state: { slot: "card", variant },
});
}
/** What a card has besides its outline: a hatched shadow under it, a hatched wash over it. */
function CardInk({ w, h, r, s, lift, wash, interactive, draw }: { w: number; h: number; r: number; s: number; lift: NonNullable<Pen["shadow"]>; wash: Pen["fill"]; interactive: boolean; draw: Pen["draw"] }) {
const id = useId().replace(/[^\w-]/g, "");
const body = useMemo(() => roundedRectPath(w, h, r), [w, h, r]);
const hatch = useMemo(() => (wash && wash !== "flat" ? hatchStrokes(s + 30, w, h, { gap: 6.5, angle: -50, jitter: 0.5, inset: 1.5 }).join("") : ""), [wash, s, w, h]);
return (
<>
<defs>
{lift === "hatch" && (
<pattern id={`${id}-hatch`} patternUnits="userSpaceOnUse" width={3.2} height={3.2} patternTransform="rotate(-45)">
<path d="M0 -1V4.2" style={{ strokeWidth: 1.05 }} />
</pattern>
)}
{/* The shadow is masked out from under the card, so the card stays the paper itself. */}
{lift !== "none" && (
<mask id={`${id}-under`} maskUnits="userSpaceOnUse" x={-30} y={-30} width={w + 60} height={h + 60}>
<rect x={-30} y={-30} width={w + 60} height={h + 60} fill="#fff" />
<path d={body} style={{ fill: "#000", stroke: "#000", strokeWidth: 1.5 }} />
</mask>
)}
{hatch && (
<clipPath id={`${id}-clip`}>
<path d={body} />
</clipPath>
)}
</defs>
{lift !== "none" && (
<g mask={`url(#${id}-under)`}>
<g className={cn("transition-[translate] duration-(--dur-hover) ease-out motion-reduce:transition-none", interactive ? "translate-[6px] group-hover/card:translate-[9px] group-focus-visible/card:translate-[9px] group-active/card:translate-[4px]" : "translate-[7px]")}>
<path d={body} style={lift === "hatch" ? { fill: `url(#${id}-hatch)`, stroke: "none" } : { fill: "currentColor", stroke: "none", opacity: 0.8 }} />
<path d={body} style={{ fill: "none", strokeWidth: 1 }} />
</g>
</g>
)}
{hatch && (
<g clipPath={`url(#${id}-clip)`}>
<Stroke d={hatch} draw={draw ?? "auto"} delay={250} duration={620} width={0.9} opacity={0.3} />
</g>
)}
</>
);
}
function CardHeader({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="card-header"
className={cn(
"group/card-header @container/card-header relative grid auto-rows-min items-start gap-1 px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto]",
className,
)}
{...props}
/>
);
}
function CardTitle({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="card-title" className={cn("text-xl leading-snug font-bold group-data-[size=sm]/card:text-lg", className)} {...props} />;
}
function CardDescription({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="card-description" className={cn("text-sm text-ink-3", className)} {...props} />;
}
function CardAction({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="card-action" className={cn("col-start-2 row-span-2 row-start-1 self-start justify-self-end", className)} {...props} />;
}
function CardContent({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="card-content" className={cn("relative px-(--card-spacing)", className)} {...props} />;
}
/**
* A picture, or any drawing, across the top of the card: it runs edge to edge
* inside the pencilled box and is ruled off from what is under it.
*/
function CardMedia({ className, children, ...props }: ComponentProps<"div">) {
return (
<div data-slot="card-media" className={cn("relative mx-[3px] -mt-[calc(var(--card-spacing)-3px)] flex flex-col overflow-hidden [&>img]:w-full [&>img]:object-cover", className)} {...props}>
{children}
<Separator className="absolute inset-x-0 bottom-0" />
</div>
);
}
/** The footer is ruled off with a pen line rather than tinted. */
function CardFooter({ className, children, ...props }: ComponentProps<"div">) {
return (
<div data-slot="card-footer" className={cn("relative flex flex-col", className)} {...props}>
<div className="px-(--card-spacing)">
<Separator />
</div>
<div className="flex items-center gap-3 p-(--card-spacing)">{children}</div>
</div>
);
}
export { Card, CardHeader, CardFooter, CardTitle, CardAction, CardDescription, CardContent, CardMedia };Usage
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardMedia, CardTitle } from "@/components/ui/card"
<Card variant="lifted">
<CardMedia>
<img src="/refill.jpg" alt="" />
</CardMedia>
<CardHeader>
<CardTitle>Ink refill</CardTitle>
<CardDescription>Medium point, blue.</CardDescription>
</CardHeader>
<CardContent>Fits most clicky pens.</CardContent>
<CardFooter>…</CardFooter>
</Card>Examples
Variants
Pencilled, lifted off a hatched shadow, or washed with hatching.
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
const kinds = [
{ variant: "outline", note: "Just the pencilled box. The default, and the quietest." },
{ variant: "lifted", note: "Stands off a hatched shadow, like a button does." },
{ variant: "wash", note: "Hatched lightly across, like a note." },
] as const;
export default function CardVariants() {
return (
<div className="grid w-full gap-10 sm:grid-cols-3">
{kinds.map(({ variant, note }) => (
<Card key={variant} variant={variant} size="sm" seed={`cv-${variant}`}>
<CardHeader>
<CardTitle className="font-mono text-base">{variant}</CardTitle>
<CardDescription>{note}</CardDescription>
</CardHeader>
<CardContent>
<p className="text-sm text-ink-3">variant="{variant}"</p>
</CardContent>
</Card>
))}
<Card variant="lifted" shadow="solid" size="sm" seed="cv-solid" className="sm:col-span-3 sm:mx-auto sm:w-1/2">
<CardHeader>
<CardTitle className="font-mono text-base">lifted, solid</CardTitle>
<CardDescription>shadow="solid" blacks the shadow in.</CardDescription>
</CardHeader>
</Card>
</div>
);
}With a picture
CardMedia runs a photo or drawing edge to edge across the top, ruled off from the text.
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardMedia, CardTitle } from "@/components/ui/card";
import { InkIcon, type IconName } from "@/components/ui/ink-icons";
const goods: { name: string; note: string; price: string; icon: IconName; tag?: string }[] = [
{ name: "Cream notebook", note: "A5, 96 pages, lies flat.", price: "£8", icon: "notebook", tag: "New" },
{ name: "Blue ballpoint", note: "Medium point, refillable.", price: "£3", icon: "pen" },
{ name: "Morning coffee", note: "For the long first page.", price: "£4", icon: "coffee" },
];
export default function CardMediaExample() {
return (
<div className="grid w-full gap-10 sm:grid-cols-3">
{goods.map((item) => (
<Card key={item.name} size="sm" seed={`cm-${item.name}`}>
{/* Anything goes up here: a photo, an illustration, an icon. It runs edge to edge. */}
<CardMedia className="h-32 items-center justify-center bg-ink-5/30 text-ink-2">
<InkIcon name={item.icon} className="size-14" />
{item.tag && (
<Badge className="absolute top-3 left-3" seed={`cm-b-${item.name}`}>
{item.tag}
</Badge>
)}
</CardMedia>
<CardHeader>
<CardTitle>{item.name}</CardTitle>
<CardDescription>{item.note}</CardDescription>
</CardHeader>
<CardContent className="text-2xl font-bold tabular-nums">{item.price}</CardContent>
<CardFooter>
<Button size="sm" variant="outline" className="w-full" seed={`cm-buy-${item.name}`}>
Add
</Button>
</CardFooter>
</Card>
))}
</div>
);
}Pricing
Plans side by side; the one to pick is lifted and badged.
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { InkGlyph } from "@/lib/ink-glyphs";
const plans = [
{ name: "Pencil", price: "£0", note: "To try it out.", features: ["One notebook", "Cream paper"] },
{ name: "Ballpoint", price: "£6", note: "For everyday writing.", features: ["Unlimited notebooks", "Every pen and paper", "Night side"], popular: true },
{ name: "Fountain", price: "£18", note: "For a whole team.", features: ["Everything in Ballpoint", "Shared notebooks", "Margin notes for all"] },
];
export default function CardPricing() {
return (
<div className="grid w-full items-stretch gap-10 sm:grid-cols-3">
{plans.map((plan) => (
<Card key={plan.name} variant={plan.popular ? "lifted" : "outline"} size="sm" seed={`cp-${plan.name}`}>
{plan.popular && (
<Badge className="absolute -top-3 right-4 z-10" seed="cp-popular">
Popular
</Badge>
)}
<CardHeader>
<CardTitle>{plan.name}</CardTitle>
<CardDescription>{plan.note}</CardDescription>
</CardHeader>
<CardContent className="flex flex-1 flex-col gap-4">
<p>
<span className="text-4xl font-bold tabular-nums">{plan.price}</span>
<span className="text-ink-3"> / month</span>
</p>
<ul className="flex flex-col gap-2 text-ink-2">
{plan.features.map((feature) => (
<li key={feature} className="flex items-center gap-2">
<InkGlyph name="check" className="size-4 shrink-0 text-ink" />
{feature}
</li>
))}
</ul>
</CardContent>
<CardFooter>
<Button variant={plan.popular ? "default" : "outline"} className="w-full" seed={`cp-buy-${plan.name}`}>
Choose {plan.name}
</Button>
</CardFooter>
</Card>
))}
</div>
);
}Cards you press
interactive cards rise on hover; selected goes over the box in full ink. Here each is a radio button, through render.
"use client";
import { useState } from "react";
import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { InkIcon, type IconName } from "@/components/ui/ink-icons";
const options: { value: string; title: string; note: string; icon: IconName }[] = [
{ value: "note", title: "A note", note: "A line or two, in the margin.", icon: "edit" },
{ value: "letter", title: "A letter", note: "Ruled paper, a margin, a signature.", icon: "mail" },
{ value: "sketch", title: "A sketch", note: "Squared paper, for plans.", icon: "ruler" },
];
export default function CardPick() {
const [picked, setPicked] = useState("letter");
return (
<div role="radiogroup" aria-label="What to write" className="grid w-full gap-8 sm:grid-cols-3">
{options.map((option) => (
<Card
key={option.value}
interactive
selected={picked === option.value}
variant={picked === option.value ? "wash" : "outline"}
size="sm"
seed={`pick-${option.value}`}
render={<button type="button" role="radio" aria-checked={picked === option.value} onClick={() => setPicked(option.value)} />}
className="text-left"
>
<CardHeader>
<InkIcon name={option.icon} className="mb-2 size-6" />
<CardTitle>{option.title}</CardTitle>
<CardDescription>{option.note}</CardDescription>
</CardHeader>
</Card>
))}
</div>
);
}Login form
A card holding a form, with a full-width action and a quiet link underneath.
"use client";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export default function CardForm() {
return (
<Card className="w-full max-w-sm" seed="cf-card">
<CardHeader>
<CardTitle>Sign in</CardTitle>
<CardDescription>Use the email you booked with.</CardDescription>
</CardHeader>
<CardContent>
<form id="cf-form" onSubmit={(event) => event.preventDefault()}>
<FieldGroup>
<Field>
<FieldLabel htmlFor="cf-email">Email</FieldLabel>
<Input id="cf-email" type="email" placeholder="you@example.com" seed="cf-email" />
</Field>
<Field>
<FieldLabel htmlFor="cf-password">Password</FieldLabel>
<Input id="cf-password" type="password" seed="cf-password" />
</Field>
</FieldGroup>
</form>
</CardContent>
<CardFooter>
<div className="flex w-full flex-col items-center gap-3">
<Button type="submit" form="cf-form" className="w-full" seed="cf-submit">
Sign in
</Button>
<Button variant="link" seed="cf-forgot">
Forgot your password?
</Button>
</div>
</CardFooter>
</Card>
);
}Stats
Small cards in a grid, for numbers with a note.
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
const stats = [
["Pages written", "1,284", "+12% this month"],
["Ink used", "62 ml", "About half a refill"],
["Doodles", "389", "Mostly in margins"],
] as const;
export default function CardStats() {
return (
<div className="grid w-full gap-6 sm:grid-cols-3">
{stats.map(([label, value, note]) => (
<Card key={label} size="sm" seed={`cs-${label}`}>
<CardHeader>
<CardDescription>{label}</CardDescription>
<CardTitle className="text-3xl tabular-nums">{value}</CardTitle>
</CardHeader>
<CardContent>
<p className="text-sm text-ink-3">{note}</p>
</CardContent>
</Card>
))}
</div>
);
}API reference
- variantdefault
"outline" "outline" | "lifted" | "wash"The pencilled box alone; the box standing off a hatched shadow; or the box hatched lightly across.
- sizedefault
"default" "default" | "sm"Inner spacing.
- interactivedefault
false booleanThe whole card is something to press: it rises on hover and focus, and gets a focus pass of the pen.
- selecteddefault
false booleanGoes over the outline in full ink, for a card that has been picked.
- render
ReactElementRender as another element: a link (`<a href>`) or a button, for an interactive card.
- shadow
"hatch" | "solid" | "none"What a lifted card stands on. Defaults to "hatch" for the lifted variant, and to none otherwise.
- fill
"hatch" | "shade" | "scribble" | "flat"How a washed card is coloured in. Defaults to hatch for the wash variant.
- CardMedia
componentA picture or drawing across the top, edge to edge inside the box, ruled off underneath.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.