Expandable Card
Expandable cards for React and Tailwind CSS: click a card and it grows into a large overlay panel, with the image, title and summary morphing into place through shared Motion layout animations. Built on Radix Dialog, so focus trapping, Escape, outside click and focus return work out of the box.
- Shared layoutId morph
- Radix Dialog
- Grid or list
- Data-driven
- prefers-reduced-motion
import { Compass, Mountain, Sunrise, Waves } from "lucide-react";
import { Button } from "@/components/ui/button";
import { ExpandableCard, type ExpandableCardItem } from "@/components/dusk/expandable-card/expandable-card";
function Art({ icon: Icon, tone }: { icon: typeof Mountain; tone: string }) {
return (
<span className="grid size-full place-items-center" style={{ background: tone }}>
<Icon className="size-9 text-white/60" aria-hidden />
</span>
);
}
const items: ExpandableCardItem[] = [
{
id: "ridge",
title: "Northern Ridge",
subtitle: "Photo series · 12 shots",
description:
"Three mornings above the tree line, chasing the few minutes when fog lifts off the valley and the ridge catches first light.",
media: <Art icon={Mountain} tone="#232826" />,
content: <p>Shot on a 35mm prime at f/8. Prints ship on matte cotton paper in three sizes.</p>,
action: <Button size="sm">View the series</Button>,
},
{
id: "tide",
title: "Low Tide",
subtitle: "Photo series · 8 shots",
description:
"Long exposures of the estuary at dusk, where the water turns to glass and the mud flats mirror the sky.",
media: <Art icon={Waves} tone="#22262b" />,
content: <p>Each frame is a 30-second exposure with a ten-stop ND filter.</p>,
action: <Button size="sm">View the series</Button>,
},
{
id: "dawn",
title: "First Light",
subtitle: "Photo series · 16 shots",
description:
"A year of sunrises from the same rooftop, one frame a week, stitched into a slow calendar of color.",
media: <Art icon={Sunrise} tone="#2a2623" />,
content: <p>The full sequence is also available as a 90-second timelapse.</p>,
action: <Button size="sm">View the series</Button>,
},
{
id: "trail",
title: "Lost Trail",
subtitle: "Photo series · 10 shots",
description:
"Forest paths that fade into nothing: overgrown, half-remembered routes through old woodland.",
media: <Art icon={Compass} tone="#262429" />,
content: <p>Printed in black and white with a warm selenium tone.</p>,
action: <Button size="sm">View the series</Button>,
},
];
export default function ExpandableCardDemo() {
return <ExpandableCard items={items} className="max-w-2xl" />;
}Installation
$ npx shadcn@latest add https://duskui.com/r/expandable-card.json1. Install the dependencies.
npm install motion @radix-ui/react-dialog lucide-react2. Copy the source into
components/dusk/expandable-card/expandable-card.tsx.expandable-card.tsx "use client"; import * as React from "react"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { AnimatePresence, LayoutGroup, motion, useReducedMotion, type Transition } from "motion/react"; import { ArrowUpRight, XIcon } from "lucide-react"; import { cn } from "@/lib/utils"; const morph: Transition = { type: "spring", bounce: 0.12, duration: 0.5 }; const fade: Transition = { duration: 0.2, ease: "easeOut" }; export interface ExpandableCardItem { /** Stable, unique id. */ id: string; title: string; /** Small line under the title (author, category, date…). */ subtitle?: string; /** Short summary. Clamped to two lines on the card, shown in full when expanded. */ description: string; /** Artwork shared by the card and the panel: an `<img>`, an illustration or a gradient. */ media?: React.ReactNode; /** Extra content that only appears in the expanded panel. */ content?: React.ReactNode; /** Action rendered at the bottom of the panel, e.g. a button or link. */ action?: React.ReactNode; } export interface ExpandableCardProps extends Omit<React.ComponentProps<"div">, "children"> { items: ExpandableCardItem[]; /** `grid` shows media-first tiles; `list` shows compact rows with a thumbnail. */ layout?: "grid" | "list"; /** Called with the expanded item's id, or `null` when it closes. */ onActiveChange?: (id: string | null) => void; /** Classes for each card. */ cardClassName?: string; /** Classes for the expanded panel. */ panelClassName?: string; } /** * A grid or list of cards. Clicking one expands it into a larger panel: the * card surface, artwork, title and summary share Motion `layoutId`s, so they * fly into place, while extra content fades in after the morph. * * Built on Radix Dialog: focus is trapped inside the panel, Escape and outside * clicks close it, the page behind is inert and focus returns to the card that * opened it. Under reduced motion the panel simply fades. */ export function ExpandableCard({ items, layout = "grid", onActiveChange, cardClassName, panelClassName, className, ...props }: ExpandableCardProps) { const baseId = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [activeId, setActiveId] = React.useState<string | null>(null); const opener = React.useRef<HTMLButtonElement | null>(null); const active = items.find((item) => item.id === activeId) ?? null; const layoutId = (id: string, part: string) => (reduceMotion ? undefined : `${baseId}-${id}-${part}`); const setActive = (id: string | null) => { setActiveId(id); onActiveChange?.(id); }; const list = layout === "list"; return ( <LayoutGroup id={baseId}> <div data-slot="expandable-card" className={cn( list ? "flex w-full flex-col gap-2" : "grid w-full grid-cols-[repeat(auto-fill,minmax(min(100%,13rem),1fr))] gap-4", className )} {...props} > {items.map((item) => ( <motion.button key={item.id} type="button" layoutId={layoutId(item.id, "surface")} transition={morph} aria-haspopup="dialog" aria-expanded={activeId === item.id} onClick={(event) => { opener.current = event.currentTarget; setActive(item.id); }} // borderRadius lives in style so Motion keeps the corners round while scaling. style={{ borderRadius: list ? 14 : 18 }} className={cn( "group/card relative flex overflow-hidden border bg-card text-left text-card-foreground outline-none", "transition-colors duration-150 hover:border-foreground/20", "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40", list ? "items-center gap-4 p-2.5 pr-4" : "flex-col", cardClassName )} > {item.media && ( <motion.span layoutId={layoutId(item.id, "media")} transition={morph} style={{ borderRadius: list ? 10 : 0 }} className={cn( "relative block shrink-0 overflow-hidden [&_img]:size-full [&_img]:object-cover", list ? "size-14" : "aspect-[4/3] w-full" )} > {item.media} </motion.span> )} <span className={cn("block min-w-0 flex-1", !list && "p-4")}> <motion.span layoutId={layoutId(item.id, "title")} layout="position" transition={morph} className="block truncate font-semibold tracking-tight" > {item.title} </motion.span> {item.subtitle && ( <motion.span layoutId={layoutId(item.id, "subtitle")} layout="position" transition={morph} className="block truncate text-xs text-muted-foreground" > {item.subtitle} </motion.span> )} {!list && ( <motion.span layoutId={layoutId(item.id, "description")} layout="position" transition={morph} className="mt-2 line-clamp-2 block text-sm text-muted-foreground" > {item.description} </motion.span> )} </span> {list && ( <ArrowUpRight aria-hidden className="size-4 shrink-0 text-muted-foreground transition-transform duration-200 group-hover/card:translate-x-0.5 group-hover/card:-translate-y-0.5 group-hover/card:text-foreground motion-reduce:transition-none" /> )} </motion.button> ))} </div> <DialogPrimitive.Root open={active !== null} onOpenChange={(open) => !open && setActive(null)}> <AnimatePresence> {active && ( // forceMount hands mounting to AnimatePresence so the exit morph can play. <DialogPrimitive.Portal key={active.id} forceMount> <DialogPrimitive.Overlay forceMount asChild> <motion.div className="fixed inset-0 z-50 bg-black/60 backdrop-blur-sm" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={fade} /> </DialogPrimitive.Overlay> <div className="pointer-events-none fixed inset-0 z-50 grid place-items-center p-4"> <DialogPrimitive.Content forceMount asChild // Return focus to the exact card that opened the panel. onCloseAutoFocus={(event) => { event.preventDefault(); opener.current?.focus(); }} > <motion.div layoutId={layoutId(active.id, "surface")} transition={morph} initial={reduceMotion ? { opacity: 0 } : undefined} animate={reduceMotion ? { opacity: 1 } : undefined} exit={reduceMotion ? { opacity: 0 } : undefined} style={{ borderRadius: 24 }} className={cn( "pointer-events-auto relative flex max-h-[calc(100dvh-2rem)] w-full max-w-lg flex-col overflow-y-auto overflow-x-hidden border bg-card text-card-foreground outline-none", "shadow-lg", panelClassName )} > {active.media && ( <motion.div layoutId={layoutId(active.id, "media")} transition={morph} style={{ borderRadius: 0 }} className="relative h-56 w-full shrink-0 overflow-hidden sm:h-64 [&_img]:size-full [&_img]:object-cover" > {active.media} </motion.div> )} <div className="p-6"> <DialogPrimitive.Title asChild> <motion.h2 layoutId={layoutId(active.id, "title")} layout="position" transition={morph} className="pr-8 text-xl font-semibold tracking-tight" > {active.title} </motion.h2> </DialogPrimitive.Title> {active.subtitle && ( <motion.p layoutId={layoutId(active.id, "subtitle")} layout="position" transition={morph} className="text-sm text-muted-foreground" > {active.subtitle} </motion.p> )} <DialogPrimitive.Description asChild> <motion.p layoutId={list ? undefined : layoutId(active.id, "description")} layout="position" transition={morph} initial={list ? { opacity: 0 } : undefined} animate={list ? { opacity: 1, transition: { ...fade, delay: 0.1 } } : undefined} exit={list ? { opacity: 0, transition: { duration: 0.1 } } : undefined} className="mt-3 text-sm leading-relaxed text-muted-foreground" > {active.description} </motion.p> </DialogPrimitive.Description> {(active.content || active.action) && ( <motion.div className="mt-4 space-y-4 text-sm leading-relaxed text-muted-foreground" initial={{ opacity: 0, y: reduceMotion ? 0 : 8 }} animate={{ opacity: 1, y: 0, transition: { ...fade, delay: reduceMotion ? 0 : 0.15 } }} exit={{ opacity: 0, y: reduceMotion ? 0 : 4, transition: { duration: 0.1 } }} > {active.content} {active.action && <div className="pt-2">{active.action}</div>} </motion.div> )} </div> <DialogPrimitive.Close asChild> <motion.button type="button" className={cn( "absolute top-3 right-3 z-10 inline-flex size-8 items-center justify-center rounded-full bg-black/40 text-white/80 outline-none backdrop-blur", "transition-colors hover:bg-black/60 hover:text-white focus-visible:ring-[3px] focus-visible:ring-ring/50" )} initial={{ opacity: 0, scale: reduceMotion ? 1 : 0.96 }} animate={{ opacity: 1, scale: 1, transition: { duration: 0.2, ease: "easeOut", delay: reduceMotion ? 0 : 0.15 } }} exit={{ opacity: 0, scale: reduceMotion ? 1 : 0.96, transition: { duration: 0.1 } }} > <XIcon className="size-4" aria-hidden /> <span className="sr-only">Close</span> </motion.button> </DialogPrimitive.Close> </motion.div> </DialogPrimitive.Content> </div> </DialogPrimitive.Portal> )} </AnimatePresence> </DialogPrimitive.Root> </LayoutGroup> ); }
Usage
import { ExpandableCard } from "@/components/dusk/expandable-card/expandable-card"
const items = [
{
id: "atlas",
title: "Atlas",
subtitle: "Case study",
description: "A design system for a fleet of internal tools.",
media: <img src="/atlas.jpg" alt="" />,
content: <p>Longer story shown only in the expanded panel.</p>,
},
]
export function Projects() {
return <ExpandableCard items={items} />
}Examples
List layout
layout="list" shows compact rows with a thumbnail that grows into the panel's full-width artwork. Good for playlists, changelogs and team pages.
import { Disc3, Headphones, Music2, Radio } from "lucide-react";
import { ExpandableCard, type ExpandableCardItem } from "@/components/dusk/expandable-card/expandable-card";
function Cover({ icon: Icon, tone }: { icon: typeof Disc3; tone: string }) {
return (
<span className="grid size-full place-items-center" style={{ background: tone }}>
<Icon className="size-1/3 min-h-5 min-w-5 text-white/60" aria-hidden />
</span>
);
}
const tracks: ExpandableCardItem[] = [
{
id: "neon-rain",
title: "Neon Rain",
subtitle: "Night Drive · 3:42",
description: "Warm analog synths over a slow, rain-soaked beat. The opener of the record.",
media: <Cover icon={Disc3} tone="#243029" />,
content: <p>Written in one take on a borrowed Juno-60, then left almost untouched.</p>,
},
{
id: "afterglow",
title: "Afterglow",
subtitle: "Night Drive · 4:05",
description: "A shimmering arpeggio that builds into the album's brightest chorus.",
media: <Cover icon={Music2} tone="#232b33" />,
content: <p>The vocal was recorded at 4am in a stairwell for the natural reverb.</p>,
},
{
id: "static",
title: "Static Bloom",
subtitle: "Night Drive · 2:58",
description: "Tape hiss, a drum machine and a melody that keeps almost resolving.",
media: <Cover icon={Radio} tone="#2b2633" />,
content: <p>Built around a loop sampled from an old shortwave broadcast.</p>,
},
{
id: "last-exit",
title: "Last Exit",
subtitle: "Night Drive · 5:31",
description: "The closing track: a long fade into ambient noise and passing headlights.",
media: <Cover icon={Headphones} tone="#332a22" />,
content: <p>Best heard with headphones; the stereo field slowly widens to the end.</p>,
},
];
export default function ExpandableCardListDemo() {
return <ExpandableCard items={tracks} layout="list" className="max-w-md" />;
}API Reference
Each item is { id, title, subtitle?, description, media?, content?, action? }. The root also accepts any <div> attribute.
| Prop | Type | Default | Description |
|---|---|---|---|
| items | ExpandableCardItem[] | — | Cards to render. media, title, subtitle and description are shared between the card and the panel. |
| layout | "grid" | "list" | "grid" | Media-first tiles in a responsive grid, or compact rows. |
| onActiveChange | (id: string | null) => void | — | Called with the expanded item's id, or null when the panel closes. |
| cardClassName | string | — | Classes for each card. |
| panelClassName | string | — | Classes for the expanded panel, e.g. a wider max-w. |
| item.content | ReactNode | — | Extra content that fades in only in the panel. |
| item.action | ReactNode | — | A button or link at the bottom of the panel. |
Accessibility
- Each card is a real
<button>witharia-haspopup="dialog". - The panel is a Radix Dialog: focus moves inside and is trapped, the page behind is inert, and the title and summary are wired to aria-labelledby and aria-describedby.
- Escape, the close button and clicking the backdrop all close it, and focus returns to the exact card that opened it.
- With prefers-reduced-motion the layout morph is turned off and the panel simply fades in and out.
- Give images in
mediaanalt(oralt=""when the title already describes them).