Item
A row of content: a picture or icon, a title and a line under it, and actions on the end.
A title and a description, with an action at the end.
import { Button } from "@/components/ui/button";
import { Item, ItemActions, ItemContent, ItemDescription, ItemMedia, ItemTitle } from "@/components/ui/item";
import { InkIcon } from "@/components/ui/ink-icons";
export default function ItemDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-6">
<Item variant="outline" seed="item-demo-a">
<ItemContent>
<ItemTitle>Basic item</ItemTitle>
<ItemDescription>A title and a description, with an action at the end.</ItemDescription>
</ItemContent>
<ItemActions>
<Button variant="outline" size="sm" seed="item-demo-act">
Action
</Button>
</ItemActions>
</Item>
<Item variant="outline" size="sm" seed="item-demo-b" render={<a href="#" />}>
<ItemMedia variant="icon" seed="item-demo-icon">
<InkIcon name="check-circle" />
</ItemMedia>
<ItemContent>
<ItemTitle>Your profile has been verified.</ItemTitle>
</ItemContent>
<ItemActions>
<InkIcon name="chevron-right" className="size-4 text-ink-3" />
</ItemActions>
</Item>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ item"use client";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { useId, useMemo, type ComponentProps, type CSSProperties } from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import { penRadius, 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";
function ItemGroup({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="item-group" className={cn("group/item-group flex flex-col", className)} {...props} />;
}
function ItemSeparator({ className, ...props }: ComponentProps<typeof Separator>) {
return <Separator data-slot="item-separator" orientation="horizontal" className={cn("my-0", className)} {...props} />;
}
const itemVariants = cva(
"group/item relative flex flex-wrap items-center text-base transition-colors duration-(--dur-hover) outline-none [a]:cursor-pointer [a]:hover:bg-ink-5/50",
{
variants: {
variant: {
default: "",
outline: "",
muted: "",
},
size: {
default: "gap-4 p-4",
sm: "gap-2.5 px-4 py-3",
},
},
defaultVariants: { variant: "default", size: "default" },
},
);
/**
* A row of content: a picture or icon, a title and a line under it, and
* actions on the end. `outline` pencils a box around it, `muted` hatches it
* lightly. Render it as a link with `render={<a href="…" />}`.
*/
function Item({
className,
variant = "default",
size = "default",
render,
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: useRender.ComponentProps<"div"> & VariantProps<typeof itemVariants> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
const boxed = variant !== "default";
return useRender({
defaultTagName: "div",
props: mergeProps<"div">(
{
className: cn(itemVariants({ variant, size }), "focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-2 focus-visible:outline-ring", className),
children: (
<>
{boxed && (
<InkOutline pen={pen} seed={seed} estimate={[420, 72]} maxRadius={16} className={variant === "outline" ? "text-ink-line" : "text-ink-4"}>
{({ w, h, r, s }) => variant === "muted" && <ItemWash w={w} h={h} r={r} s={s} draw={pen.draw} />}
</InkOutline>
)}
{children}
</>
),
} as ComponentProps<"div">,
props,
),
render,
state: { slot: "item", variant, size },
});
}
/** The muted item's hatching, kept inside its box. */
function ItemWash({ w, h, r, s, draw }: { w: number; h: number; r: number; s: number; draw: Pen["draw"] }) {
const id = useId().replace(/[^\w-]/g, "");
const d = useMemo(() => hatchStrokes(s + 30, w, h, { gap: 6, angle: -50, jitter: 0.5, inset: 1.5 }).join(""), [s, w, h]);
return (
<>
<defs>
<clipPath id={`${id}-clip`}>
<path d={roundedRectPath(w, h, r)} />
</clipPath>
</defs>
<g clipPath={`url(#${id}-clip)`}>
<Stroke d={d} draw={draw ?? "auto"} delay={200} duration={560} width={0.9} opacity={0.28} />
</g>
</>
);
}
const itemMediaVariants = cva("relative flex shrink-0 items-center justify-center gap-2 group-has-[[data-slot=item-description]]/item:translate-y-0.5 group-has-[[data-slot=item-description]]/item:self-start [&_svg:not(.ink-sketch)]:pointer-events-none", {
variants: {
variant: {
default: "bg-transparent",
icon: "size-9 text-ink-2 [&_svg:not(.ink-sketch):not([class*='size-'])]:size-4.5",
image: "size-10 overflow-hidden rounded-(--pen-r) [&_img]:size-full [&_img]:object-cover",
},
},
defaultVariants: { variant: "default" },
});
/** The icon or picture at the start of the row. An icon is circled with the pen. */
function ItemMedia({ className, variant = "default", seed, children, ...props }: ComponentProps<"div"> & VariantProps<typeof itemMediaVariants> & { seed?: string | number }) {
const pen = usePen({});
return (
<div data-slot="item-media" data-variant={variant} className={cn(itemMediaVariants({ variant }), className)} style={{ "--pen-r": `${penRadius(pen, 8)}px` } as CSSProperties} {...props}>
{variant === "icon" && <InkOutline pen={pen} seed={seed} shape="ring" estimate={[36, 36]} pad={4} className="text-ink-3" />}
{children}
</div>
);
}
function ItemContent({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="item-content" className={cn("relative flex flex-1 flex-col gap-1 [&+[data-slot=item-content]]:flex-none", className)} {...props} />;
}
function ItemTitle({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="item-title" className={cn("flex w-fit items-center gap-2 text-base leading-snug font-bold", className)} {...props} />;
}
function ItemDescription({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="item-description"
className={cn("line-clamp-2 text-sm leading-normal text-balance text-ink-3 [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-ink", className)}
{...props}
/>
);
}
function ItemActions({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="item-actions" className={cn("relative flex items-center gap-2", className)} {...props} />;
}
function ItemHeader({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="item-header" className={cn("relative flex basis-full items-center justify-between gap-2", className)} {...props} />;
}
function ItemFooter({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="item-footer" className={cn("relative flex basis-full items-center justify-between gap-2", className)} {...props} />;
}
export { Item, ItemMedia, ItemContent, ItemActions, ItemGroup, ItemSeparator, ItemTitle, ItemDescription, ItemHeader, ItemFooter };Usage
import { Item, ItemContent, ItemDescription, ItemMedia, ItemTitle } from "@/components/ui/item"
<Item variant="outline">
<ItemContent>
<ItemTitle>Spring notebook</ItemTitle>
<ItemDescription>Forty pages of field sketches.</ItemDescription>
</ItemContent>
</Item>Examples
Variants
Plain, pencilled round, or lightly hatched.
No box; the row just sits on the page.
Pencilled round.
Lightly hatched, like a note.
import { Item, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item";
export default function ItemVariants() {
return (
<div className="flex w-full max-w-md flex-col gap-6">
{(["default", "outline", "muted"] as const).map((variant) => (
<Item key={variant} variant={variant} seed={`item-${variant}`}>
<ItemContent>
<ItemTitle>{variant[0].toUpperCase() + variant.slice(1)}</ItemTitle>
<ItemDescription>{variant === "default" ? "No box; the row just sits on the page." : variant === "outline" ? "Pencilled round." : "Lightly hatched, like a note."}</ItemDescription>
</ItemContent>
</Item>
))}
</div>
);
}A list
Items in a group, ruled apart.
ada@example.com
margaret@example.com
grace@example.com
import { Fragment } from "react";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Button } from "@/components/ui/button";
import { Item, ItemActions, ItemContent, ItemDescription, ItemGroup, ItemMedia, ItemSeparator, ItemTitle } from "@/components/ui/item";
import { InkIcon } from "@/components/ui/ink-icons";
const people = [
{ name: "Ada Lovelace", email: "ada@example.com", initials: "AL" },
{ name: "Margaret Hamilton", email: "margaret@example.com", initials: "MH" },
{ name: "Grace Hopper", email: "grace@example.com", initials: "GH" },
];
export default function ItemList() {
return (
<ItemGroup className="w-full max-w-md">
{people.map((person, i) => (
<Fragment key={person.email}>
{i > 0 && <ItemSeparator />}
<Item>
<ItemMedia>
<Avatar seed={`item-${person.initials}`}>
<AvatarFallback>{person.initials}</AvatarFallback>
</Avatar>
</ItemMedia>
<ItemContent className="gap-0">
<ItemTitle>{person.name}</ItemTitle>
<ItemDescription>{person.email}</ItemDescription>
</ItemContent>
<ItemActions>
<Button variant="ghost" size="icon" aria-label={`Message ${person.name}`} seed={`item-msg-${i}`}>
<InkIcon name="arrow-right" />
</Button>
</ItemActions>
</Item>
</Fragment>
))}
</ItemGroup>
);
}Header and footer
Full-width rows above and below the content.
Forty pages of field sketches, three of them finished, the rest in pencil.
import { Badge } from "@/components/ui/badge";
import { Item, ItemContent, ItemDescription, ItemFooter, ItemHeader, ItemTitle } from "@/components/ui/item";
export default function ItemHeaderExample() {
return (
<Item variant="outline" seed="item-header" className="max-w-md">
<ItemHeader>
<ItemTitle>Spring notebook</ItemTitle>
<Badge variant="secondary" seed="item-header-badge">
Draft
</Badge>
</ItemHeader>
<ItemContent>
<ItemDescription>Forty pages of field sketches, three of them finished, the rest in pencil.</ItemDescription>
</ItemContent>
<ItemFooter>
<span className="text-sm text-ink-3">Edited yesterday</span>
<span className="text-sm text-ink-3">40 pages</span>
</ItemFooter>
</Item>
);
}API reference
- variantdefault
"default" "default" | "outline" | "muted"No box, a pencilled box, or a lightly hatched one.
- sizedefault
"default" "default" | "sm"Row padding.
- render
ReactElementMake the whole row a link: render={<a href="…" />}.
- ItemMedia variantdefault
"default" "default" | "icon" | "image"A bare slot, an icon circled with the pen, or a small picture.
- 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.