$npx @dinachi/cli@latest add expandable-card
fullscreenshowClosebackdropClassNameprinciples.tsx
import {
ExpandableCard,
ExpandableCardBody,
ExpandableCardPanel,
ExpandableCardShared,
ExpandableCardTrigger,
} from "@/components/ui/expandable-card"
const items = [
{ id: "travel", kicker: "Shared layout", title: "The card travels", body: "Two elements with the same layoutId are one object to the layout engine. The card does not fade out while a dialog fades in, so the reader never has to work out whether the panel is the thing they tapped." },
{ id: "modal", kicker: "Accessibility", title: "It is a real modal", body: "aria-modal is a promise that the rest of the page is unreachable. Escape closes, focus moves in and comes back, Tab cycles inside, and the card underneath is inert rather than merely covered." },
{ id: "restraint", kicker: "Restraint", title: "Not for forty cards", body: "This is the effect most likely to end up on a dashboard grid, where it turns a glance into a wait. Reach for it where opening something is a decision rather than a habit." },
]
export function Principles() {
return (
<div className="grid gap-3 sm:grid-cols-3">
{items.map((item) => (
<ExpandableCard key={item.id}>
<ExpandableCardTrigger>
<ExpandableCardShared part="art" className="h-24 bg-muted" />
<div className="space-y-1 p-4">
<ExpandableCardShared part="kicker" className="text-xs text-muted-foreground">
{item.kicker}
</ExpandableCardShared>
<ExpandableCardShared part="title" className="text-sm font-medium">
{item.title}
</ExpandableCardShared>
</div>
</ExpandableCardTrigger>
<ExpandableCardPanel title={item.title}>
<ExpandableCardShared part="art" className="h-40 bg-muted" />
<div className="space-y-1 p-5">
<ExpandableCardShared part="kicker" className="text-xs text-muted-foreground">
{item.kicker}
</ExpandableCardShared>
<ExpandableCardShared part="title" className="text-lg font-medium">
{item.title}
</ExpandableCardShared>
<ExpandableCardBody className="pt-2">{item.body}</ExpandableCardBody>
</div>
</ExpandableCardPanel>
</ExpandableCard>
))}
</div>
)
}Usage
import {
ExpandableCard,
ExpandableCardBody,
ExpandableCardPanel,
ExpandableCardShared,
ExpandableCardTrigger,
} from "@/components/ui/expandable-card"
export function Story() {
return (
<ExpandableCard>
<ExpandableCardTrigger>
<ExpandableCardShared part="art" className="h-24 bg-muted" />
<ExpandableCardShared part="title" className="p-4 text-sm font-medium">
Shared layout
</ExpandableCardShared>
</ExpandableCardTrigger>
<ExpandableCardPanel title="Shared layout">
<ExpandableCardShared part="art" className="h-40 bg-muted" />
<ExpandableCardShared part="title" className="px-5 pt-4 text-lg font-medium">
Shared layout
</ExpandableCardShared>
<ExpandableCardBody className="px-5 pb-5">
Content that only exists in the panel.
</ExpandableCardBody>
</ExpandableCardPanel>
</ExpandableCard>
)
}Examples
Focus lands inside, Escape closes, focus comes back.
import {
ExpandableCard,
ExpandableCardTrigger,
ExpandableCardPanel,
ExpandableCardShared,
ExpandableCardBody,
} from '@/components/ui/expandable-card';
export function Example() {
return (
<ExpandableCard>
<ExpandableCardTrigger>
<ExpandableCardShared part="art" className="h-24 bg-muted" />
<ExpandableCardShared part="title" className="p-4 text-sm font-medium">
The card travels
</ExpandableCardShared>
</ExpandableCardTrigger>
<ExpandableCardPanel title="The card travels">
<ExpandableCardShared part="art" className="h-40 bg-muted" />
<ExpandableCardShared part="title" className="px-5 pt-4 text-lg font-medium">
The card travels
</ExpandableCardShared>
<ExpandableCardBody className="px-5 pb-5">
Content that only exists in the panel arrives after the landing.
</ExpandableCardBody>
</ExpandableCardPanel>
</ExpandableCard>
);
}Panel is closed
You hold the open state and the close control.
const [open, setOpen] = useState(false);
// showClose={false} hands the close control over too, which only works if
// something you own can still set open back to false.
<ExpandableCard open={open} onOpenChange={setOpen}>
<ExpandableCardTrigger>{/* ... */}</ExpandableCardTrigger>
<ExpandableCardPanel title="Own the open state" showClose={false}>
{/* ... */}
<button type="button" onClick={() => setOpen(false)}>Done reading</button>
</ExpandableCardPanel>
</ExpandableCard>The same travel, landing on all four edges.
// The card travels the same way; it just lands on the whole viewport. Content
// taller than the screen scrolls, and the close button stays put while it does.
<ExpandableCardPanel fullscreen title="The road that ends at the water">
<ExpandableCardShared part="art" className="h-56 bg-muted sm:h-72" />
<div className="mx-auto max-w-2xl space-y-3 px-6 py-8">
<ExpandableCardShared part="title" className="text-2xl font-medium">
The road that ends at the water
</ExpandableCardShared>
<ExpandableCardBody className="space-y-4 text-base">
{paragraphs.map((p) => <p key={p}>{p}</p>)}
</ExpandableCardBody>
</div>
</ExpandableCardPanel>A row expands the same way.
// Nothing here is card-shaped. Name the parts that exist on both sides and they
// travel; everything else waits for the landing.
<ExpandableCard>
<ExpandableCardTrigger className="rounded-lg px-4 py-3">
<div className="flex items-center justify-between gap-4">
<ExpandableCardShared part="title">Invoice 4021</ExpandableCardShared>
<ExpandableCardShared part="meta">Paid · 12 Aug</ExpandableCardShared>
</div>
</ExpandableCardTrigger>
<ExpandableCardPanel title="Invoice 4021" className="max-w-sm">
{/* the same two parts, larger */}
</ExpandableCardPanel>
</ExpandableCard>Notes
Motion tier
Needs motion. add --all skips it; install with add --motion or by name.
When to use it
- A hero item, or a short list where opening one is a deliberate choice.
- Skip it on large grids, where the travel slows every glance.
- If the detail view shares nothing with the card, use a Dialog.
Pairing parts
- Use each
partonce per side. Apartused twice on one side makes the layout engine pick one. - Put anything on only one side in
ExpandableCardBody. - Ids are scoped per card, so a grid of cards works.
Controlled
Use open and onOpenChange to close the panel from your own control. With showClose={false}, your control must set open back to false.
Full screen
- Centred: sized by content, never scrolls; rounded, bordered and shadowed.
fullscreen: fills the screen, content scrolls inside, no radius, border or shadow.- The close button stays fixed outside the scroller.
Behaviour
- The travel is a 420ms spring; the backdrop a 200ms fade.
- Reduced motion cuts. An app-level
MotionConfig reducedMotionis honoured. - The panel portals to
document.body. An ancestor with a transform, filter, backdrop-filter, perspective or paint containment breaksposition: fixed; passcontainerto portal elsewhere.
API
openboolean
Controlled open state.defaultOpenboolean = false
Starting open state when uncontrolled.onOpenChange
Runs before open or close, for people and agents. agent
(open, details) => voiddetails.cancel() refuses it.children
Card content. Its text is the agent's label while closed.agent
ReactNodeonClick(event) => void
Runs first. event.preventDefault() stops the card opening.title
Dialog name, and the agent's label while open.agent
stringshowCloseboolean = true
Set false to supply your own close control.fullscreenboolean = false
Fills the viewport; long content scrolls.closeLabelstring = "Close"
Accessible name for the close button.backdropClassNamestring
Classes for the backdrop.containerElement | null = document.body
Where the overlay is portalled.partstring
Names the pair. Use it once on the card, once in the panel.Takes the props of the element it renders.
Agent
Inside an AgentProvider, it appears in agent.snapshot() as disclosure.
Reads
labelThe card's text while closed. The panel's title while open.
openWhether the panel is showing.
Actions
open()While closed. Opens the panel like a click on the card.
close()While open. Closes the panel.
An agent's open or close runs through onOpenChange with details.reason set to imperative-action. isAgentChange(details) is true for it; details.cancel() keeps the card as it was and tells the agent the app refused.
await agent.act({
steps: [{ ref: "The card travels", action: "open" }],
})Accessibility
- The panel is a modal dialog with
aria-modal, named by itstitle. - Focus moves to the panel's first control on open and back to the card on close.
- The card is
inertwhile open. - Page scroll is locked, with scrollbar compensation so layout does not shift.
- The trigger is a button with
aria-haspopup="dialog"andaria-expanded.
Keyboard
SpaceEnterOpens the card
TabShift + TabCycles focus inside the open panel
EscCloses and returns focus to the card
SpaceEnterOpens the card
TabShift + TabCycles focus inside the open panel
EscCloses and returns focus to the card