Item
Lays out one entry of a list with an icon or image, a title, a description and actions.
Ferrolva Acciai, arriving Thursday at Brescia.
Installation
pnpm dlx shadcn@latest add @silkware/itemTo copy the component by hand, install its dependencies and the Separator:
pnpm add react-aria-components class-variance-authority cnThen copy the source into components/ui/item.tsx:
"use client"
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"import { Link as LinkPrimitive, type LinkProps } from "react-aria-components"
import { Separator } from "@/components/ui/separator"
function ItemGroup({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="item-group" className={cn( "group/item-group flex w-full flex-col gap-4 has-data-[size=sm]:gap-2.5 has-data-[size=xs]:gap-2", className )} {...props} /> )}
function ItemSeparator({ className, ...props}: React.ComponentProps<typeof Separator>) { return ( <Separator data-slot="item-separator" orientation="horizontal" className={cn("my-2", className)} {...props} /> )}
const itemVariants = cva( "group/item flex w-full flex-wrap items-center rounded-md border text-sm transition-colors duration-100 outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 [a]:transition-colors [a]:hover:bg-muted", { variants: { variant: { default: "border-transparent", outline: "border-border", muted: "border-transparent bg-muted/50", }, size: { default: "gap-3.5 px-4 py-3.5", sm: "gap-2.5 px-3 py-2.5", xs: "gap-2 px-2.5 py-2 in-data-[slot=dropdown-menu-content]:p-0", }, }, defaultVariants: { variant: "default", size: "default", }, })
function Item({ className, variant = "default", size = "default", ...props}: Omit<LinkProps, "children"> & React.HTMLAttributes<HTMLElement> & VariantProps<typeof itemVariants>) { const Element = "href" in props ? LinkPrimitive : "div" return ( <Element data-slot="item" data-variant={variant} data-size={size} className={cn(itemVariants({ variant, size, className }))} {...props} /> )}
const itemMediaVariants = cva( "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]:pointer-events-none", { variants: { variant: { default: "bg-transparent", icon: "[&_svg:not([class*='size-'])]:size-4", image: "size-10 overflow-hidden rounded-sm group-data-[size=sm]/item:size-8 group-data-[size=xs]/item:size-6 [&_img]:size-full [&_img]:object-cover", }, }, defaultVariants: { variant: "default", }, })
function ItemMedia({ className, variant = "default", ...props}: React.ComponentProps<"div"> & VariantProps<typeof itemMediaVariants>) { return ( <div data-slot="item-media" data-variant={variant} className={cn(itemMediaVariants({ variant, className }))} {...props} /> )}
function ItemContent({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="item-content" className={cn( "flex flex-1 flex-col gap-1 group-data-[size=xs]/item:gap-0 [&+[data-slot=item-content]]:flex-none", className )} {...props} /> )}
function ItemTitle({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="item-title" className={cn( "line-clamp-1 flex w-fit items-center gap-2 text-sm leading-snug font-medium underline-offset-4", className )} {...props} /> )}
function ItemDescription({ className, ...props }: React.ComponentProps<"p">) { return ( <p data-slot="item-description" className={cn( "line-clamp-2 text-left text-sm leading-normal font-normal text-muted-foreground group-data-[size=xs]/item:text-xs [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary", className )} {...props} /> )}
function ItemActions({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="item-actions" className={cn("flex items-center gap-2", className)} {...props} /> )}
function ItemHeader({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="item-header" className={cn( "flex basis-full items-center justify-between gap-2", className )} {...props} /> )}
function ItemFooter({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="item-footer" className={cn( "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, ItemActions, ItemContent, ItemDescription, ItemFooter, ItemGroup, ItemHeader, ItemMedia, ItemSeparator, ItemTitle,} from "@/components/ui/item"<Item variant="outline"> <ItemMedia variant="icon"> <TruckIcon /> </ItemMedia> <ItemContent> <ItemTitle>Delivery PO-2416</ItemTitle> <ItemDescription>Arriving Thursday at Brescia.</ItemDescription> </ItemContent> <ItemActions> <Button size="sm">Track</Button> </ItemActions></Item>An Item renders a div, unless you pass href: then it renders a React Aria Link and the whole item is the link. Do not put a button or another link inside an item that has href.
Examples
Variants
default has no border and no fill, outline has a hairline border, and muted has a soft fill.
No border and no fill, for lists on a plain page.
A border, for items that stand alone.
A soft fill, for a note or a summary.
Size
default, sm and xs set the padding and the gap. At xs the description shrinks to the small text size.
Site Bergamo, 52 people.
Site Bergamo, 52 people.
With an avatar
Put an Avatar in ItemMedia to list people.
Production planner, Bergamo.
With an image
ItemMedia variant="image" makes a rounded square that crops its image. Give the image alt text.
Updated by purchasing on Monday.
Group
ItemGroup stacks items with a gap, and ItemSeparator draws a rule between them. The group sets no role, so a screen reader reads the items in order as a run of blocks.
3 open orders, Bergamo
1 open order, Brescia
No open orders
As a link
Pass href to make the item a link to another page. Internal paths navigate on the client through the router.
Header and footer
ItemHeader and ItemFooter span the full width above and below the content. Use them for a reference and a status, or a name and a figure. This example uses the brand's state square with a word.
Fasteners and brackets for the Bergamo line.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus to the item when it is a link, or to the controls inside it. |
| Enter | Follows the link when the item has an href. |
React Aria provides, when the item has href:
- The
linkrole, press handling and client-side navigation. - A focus ring for keyboard focus only.
You provide:
- List semantics, if you need them and the group has no
ItemSeparator:role="list"on theItemGroupandrole="listitem"on each item. - A title that names the entry. Titles and descriptions are clamped to one and two lines, so the full text must still make sense when cut.
alttext on an image inItemMedia, and anaria-labelon an icon-only action.- One interactive target per item: either the whole item is a link, or the actions inside it are buttons.
Guidelines
When to use
- For a list of entries that each have a title, a short description and an action: suppliers, deliveries, people.
- For settings rows with a label, a hint and a control.
- For a menu of destinations, with each item a link.
When not to use
- For records compared field by field. Use a Table.
- For a form field with a label and a control. Use Field.
- For a self-contained panel with several actions. Use a Card.
- For a message about the state of the page. Use an Alert.
Content
- The title names the entry in a few words. The description adds one fact in one line.
- Put the action on the right and make it the same kind across the list.
- Keep descriptions short: they are cut at two lines.
Length
Bergamo warehouse, closed on Friday.
Keep the title short and the description to one fact.
The count covers every shelf in aisles one to nine, the receiving dock and the returns cage, and it was signed off by Marco together with the two people from purchasing who checked the pallets.
Don’t write paragraphs. The text is cut at two lines and the reader loses the end.
API reference
Item
Renders a div, or a React Aria Link when href is set. It accepts every React Aria Link prop when it is a link, and the props below.
| Prop | Type | Default | Description |
|---|---|---|---|
variant? | "default" | "outline" | "muted" | "default" | The border and fill. |
size? | "default" | "sm" | "xs" | "default" | The padding and the gap. |
href? | string | None | Renders the item as a link to this address. |
className? | string | None | Classes merged after the variant classes. |
ItemGroup
Renders a div that stacks items. It sets no role. It accepts every div prop.
ItemSeparator
Renders a Separator with horizontal margins. It accepts every Separator prop.
ItemMedia
Renders the leading media of an item.
| Prop | Type | Default | Description |
|---|---|---|---|
variant? | "default" | "icon" | "image" | "default" | Bare content, an icon sized to 16px, or a rounded image that fills a 40px square. |
ItemContent
Renders a div that holds the title and the description and takes the free width. It accepts every div prop.
ItemTitle
Renders a div clamped to one line. It accepts every div prop.
ItemDescription
Renders a p in muted text, clamped to two lines. Links inside it are underlined. It accepts every p prop.
ItemActions
Renders a flex div for the trailing controls. It accepts every div prop.
ItemHeader and ItemFooter
Render a full-width div above or below the content, with the children spread to both ends. They accept every div prop.