Containment
Card stack
Cards stacked like a deck. Hover or focus fans them out with a little rotation; press a card and it lifts to the top while the rest restack behind it.
Examples and variants
Themes
Hover, then pick a card.
On top: Minimal
function ThemeStack() {
const [top, setTop] = useState(themes[0].name)
return (
<div className="flex w-full flex-col items-center gap-stack-md">
<CardStack
items={themes}
getKey={(t) => t.id}
getLabel={(t) => t.name}
label="Themes"
onFront={(t) => setTop(t.name)}
renderCard={(t) => (
<span className="flex size-full flex-col justify-end p-4" style={{ background: `var(${t.tint})` }}>
<span className="text-base font-medium">{t.name}</span>
<span className="text-xs text-muted-foreground">{t.note}</span>
</span>
)}
/>
<p className="text-sm text-muted-foreground">On top: {top}</p>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/card-stack.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cnCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/card-stack.tsxcard-stack
components/ui/card-stack.tsx"use client" import * as React from "react" import { cn } from "cn" /* Card stack: cards stacked like a deck. Hover or focus fans them out with a little rotation, staggered; press a card and it lifts to the top while the rest restack behind it. Calm fans without rotation; still (and reduced motion) lays the cards out as a grid. Each card is a button. */ function CardStack<T>({ items, getKey, getLabel, renderCard, onFront, label = "Cards", className, }: { items: T[] getKey: (item: T) => string | number getLabel: (item: T) => string renderCard: (item: T) => React.ReactNode /** Called with the card that was brought to the front. */ onFront?: (item: T) => void label?: string className?: string }) { const [order, setOrder] = React.useState(() => items.map(getKey)) const [lifted, setLifted] = React.useState<string | number | null>(null) const keys = items.map(getKey) // Keep the user's order but follow added or removed items. const shown = [...order.filter((key) => keys.includes(key)), ...keys.filter((key) => !order.includes(key))] const byKey = new Map(items.map((item) => [getKey(item), item])) const n = shown.length function front(key: string | number) { if (shown[0] === key) return setOrder([key, ...shown.filter((k) => k !== key)]) setLifted(key) const item = byKey.get(key) if (item !== undefined) onFront?.(item) } return ( <ul aria-label={label} data-slot="card-stack" className={cn("card-stack relative", className)}> {/* DOM order stays fixed so focus doesn't jump; position comes from the stack index. */} {keys.map((key) => { const item = byKey.get(key) as T const i = shown.indexOf(key) return ( <li key={key} className="card-stack-card" data-lifted={lifted === key || undefined} onAnimationEnd={() => setLifted((current) => (current === key ? null : current))} style={{ "--i": i, "--fan": i - (n - 1) / 2, zIndex: n - i } as React.CSSProperties} > <button type="button" aria-label={i === 0 ? `${getLabel(item)}, on top` : `Bring ${getLabel(item)} to the front`} onClick={() => front(key)} className="block size-full overflow-hidden rounded-(--stack-corner) bg-(--stack-container) text-start shadow-(--stack-elevation) outline-none focus-visible:shadow-(--focus-ring)" > {renderCard(item)} </button> </li> ) })} </ul> ) } export { CardStack }
Imports
What the examples on this page import:
import { Button } from "@/components/ui/button"
import { CardStack } from "@/components/ui/card-stack"
import { Confetti } from "@/components/ui/confetti"
import { Dice } from "@/components/ui/dice"
import { FlipBoard } from "@/components/ui/flip-board"
import { Metaballs } from "@/components/ui/metaballs"
import { tokenMs } from "@/lib/motion/token-ms"Anatomy
- 1Card · --stack-card-width, --stack-card-height, --stack-corner, --stack-container, --stack-elevation.
- 2Fan · --stack-offset, --stack-spread, --stack-tilt, --stack-stagger.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
items, getKey, getLabel, renderCard | T[] and functions | — | Cards. |
onFront | (item) => void | — | Called with the card brought to the front. |
label | string | "Cards" | List name. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--stack-card-width | calc(var(--space-32) * 1.25) | calc(var(--space-32) * 1.25) | same |
--stack-card-height | calc(var(--space-32) * 1.5) | calc(var(--space-32) * 1.5) | same |
--stack-corner | var(--shape-lg) | 22px | same |
--stack-container | var(--surface-container-raised) | oklch(1 0 0) | oklch(0.22 0 0) |
--stack-elevation | var(--elevation-2) | 0 1px 1px -0.5px oklch(0.22 0.01 260 / 6%), 0 3px 3px -1.5px oklch(0.22 0.01 260 / 6%), 0 6px 6px -3px oklch(0.22 0.01 260 / 6%), 0 12px 12px -6px oklch(0.22 0.01 260 / 6%), 0 24px 24px -12px oklch(0.22 0.01 260 / 6%) | 0 4px 12px oklch(0 0 0 / 40%), 0 12px 28px oklch(0 0 0 / 35%) |
--stack-offset | var(--space-2) | 8px | same |
--stack-spread | var(--space-12) | 48px | same |
--stack-tilt | 5deg | 5deg | same |
--stack-stagger | var(--duration-stagger) | 40ms | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Hover / press | Fans with rotation, staggered; the pressed card lifts and lands on top. Calm: no rotation. Still: a grid. | --spring-snappy, --spring-bouncy |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Each card is a button named "Bring X to the front"; focus fans the stack like hover does.
- DOM order never changes, so focus stays put.
Usage
Do
- Use for a few visual choices (themes, plans).
Don’t
- Don't hide more than five cards in a stack.
Code
"use client"
import * as React from "react"
import { cn } from "cn"
/*
Card stack: cards stacked like a deck. Hover or focus fans them out with a
little rotation, staggered; press a card and it lifts to the top while the
rest restack behind it. Calm fans without rotation; still (and reduced
motion) lays the cards out as a grid. Each card is a button.
*/
function CardStack<T>({
items,
getKey,
getLabel,
renderCard,
onFront,
label = "Cards",
className,
}: {
items: T[]
getKey: (item: T) => string | number
getLabel: (item: T) => string
renderCard: (item: T) => React.ReactNode
/** Called with the card that was brought to the front. */
onFront?: (item: T) => void
label?: string
className?: string
}) {
const [order, setOrder] = React.useState(() => items.map(getKey))
const [lifted, setLifted] = React.useState<string | number | null>(null)
const keys = items.map(getKey)
// Keep the user's order but follow added or removed items.
const shown = [...order.filter((key) => keys.includes(key)), ...keys.filter((key) => !order.includes(key))]
const byKey = new Map(items.map((item) => [getKey(item), item]))
const n = shown.length
function front(key: string | number) {
if (shown[0] === key) return
setOrder([key, ...shown.filter((k) => k !== key)])
setLifted(key)
const item = byKey.get(key)
if (item !== undefined) onFront?.(item)
}
return (
<ul aria-label={label} data-slot="card-stack" className={cn("card-stack relative", className)}>
{/* DOM order stays fixed so focus doesn't jump; position comes from the stack index. */}
{keys.map((key) => {
const item = byKey.get(key) as T
const i = shown.indexOf(key)
return (
<li
key={key}
className="card-stack-card"
data-lifted={lifted === key || undefined}
onAnimationEnd={() => setLifted((current) => (current === key ? null : current))}
style={{ "--i": i, "--fan": i - (n - 1) / 2, zIndex: n - i } as React.CSSProperties}
>
<button
type="button"
aria-label={i === 0 ? `${getLabel(item)}, on top` : `Bring ${getLabel(item)} to the front`}
onClick={() => front(key)}
className="block size-full overflow-hidden rounded-(--stack-corner) bg-(--stack-container) text-start shadow-(--stack-elevation) outline-none focus-visible:shadow-(--focus-ring)"
>
{renderCard(item)}
</button>
</li>
)
})}
</ul>
)
}
export { CardStack }