Skip to content
Browse documentation

A tactile stack that fans out, follows your hand, and deals the next card with a measured flick.

Used forTestimonialsOnboarding tipsCase studies and showcasesMobile-style content

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/card-deck

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/card-deck.json. More

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • Testimonials

    A stack of customer quotes that visitors flick through.

  • Onboarding tips

    Short cards to deal through, one at a time.

  • Case studies and showcases

    Let people browse a small collection hands-on on a landing page.

  • Mobile-style content

    Flashcards, recipes or product picks in a stack you can swipe.

What it handles for you

A row of cards with arrow buttons is a carousel. A deck is something you hold. This gives a stack of your own cards that feel and behave like one.

Instead of wiring upa carousel librarydrag and flick codea spring animationkeyboard handlingwrap-around logic
  1. It fans out

    Hover or focus the front card and the cards behind open slightly, so people can see there is more.

  2. It follows your hand

    The front card tracks the pointer with a slight rotation while the next one rises.

  3. It deals on a flick

    Drag past about a quarter of the card’s width, or flick, and the card is dealt away from where you let go.

  4. It springs back

    An unfinished drag returns on a damped spring that keeps the speed of your release.

  5. Your content in every card

    renderCard draws anything. Links, buttons and inputs inside a card keep their own behaviour.

  6. Works without dragging

    Arrow keys and Previous and Next buttons browse the deck, and vertical touch scrolling still works.

Overview

A small collection you can hold. The cards sit in a shallow stack; hovering or focusing the front card gently fans the ones behind it. Drag sideways and the front card follows your hand with a slight rotation while the next comes forward. A committed drag or quick flick deals it away. Release early and a damped spring brings it home. The same collection works with arrow buttons and keyboard controls, and its cards can contain your own content.

States

rest
Up to four cards are visible, with 12px spacing and a 5.5% size difference between layers.
fan
Hover or keyboard focus opens the stack to 17px spacing and a slight alternating rotation.
drag
The front card follows the pointer without React updates on each frame. Every card has its own compositor layer, and the cards behind are moved directly as the next one rises in proportion to the drag, so nothing repaints mid-gesture.
deal
Cross 27% of the card’s width (at most 100px), or flick intentionally, to deal a card. The outgoing card continues from the release position.
return
An incomplete or cancelled drag returns along a sampled damped spring over 520ms, using the release velocity.

Usage

usage.tsx
import { CardDeck } from "@/components/card-deck";<CardDeck  items={[{ id: "capture", title: "Catch the little things." }, { id: "connect", title: "Follow the thread." }]}  getLabel={(card) => card.title}  renderCard={(card) => <div className="p-6"><h3>{card.title}</h3></div>}/>

Depends on lucide-react, installed for you by the CLI along with the keyframes it needs.

Works with

A controlled collection

Render your own card contents; the deck owns the stack and gestures. The collection wraps in both directions.

example.tsx
"use client";import { useState } from "react";import { CardDeck } from "@/components/card-deck";const cards = [  { id: "capture", title: "Capture", body: "Save a thought before it disappears." },  { id: "connect", title: "Connect", body: "Find the thread between your ideas." },];export function Collection() {  const [value, setValue] = useState("capture");  return (    <CardDeck items={cards} value={value} onValueChange={setValue} height={300}      getLabel={(card) => card.title}      renderCard={(card, { index }) => (        <article className="flex h-full flex-col p-6">          <span className="text-xs text-muted-foreground">0{index + 1}</span>          <h3 className="mt-auto text-2xl">{card.title}</h3>          <p className="mt-3 text-sm text-muted-foreground">{card.body}</p>        </article>      )}    />  );}

Props

items
T[] where T extends { id: string }
The collection in reading order. IDs must be unique.
renderCard
(item: T, state: { active: boolean; index: number }) => ReactNode
Your card contents. Only the front card is interactive; rear cards and outgoing copies are inert.
getLabel= item.id
(item: T) => string
Accessible name for each card.
value / defaultValue
string
Controlled selected ID or initial selected ID. Defaults to the first card.
onValueChange
(id: string) => void
Receives the next selected ID after a gesture or button action.
height= 340
number
Card height in CSS pixels, at least 120. Leave space for your longest content.
label= Card collection
string
Accessible label for the collection region.
className
string
Additional classes on the root. Standard div attributes are forwarded.

Accessibility and motion

  • Left and Right on the front card browse the collection. Previous and Next buttons offer equivalent pointer and touch controls.
  • Only the front card is focusable. Back cards and the departing copy are inert and hidden from assistive technology.
  • Links, buttons, form inputs and elements marked data-deck-no-drag keep their own interactions without starting a card drag.
  • Vertical touch scrolling is preserved. Pointer cancellation returns the card, and a pause before release does not count as a flick.
  • Reduced motion removes dragging transforms, fanning and animated exits. Gestures still select cards, with immediate state changes.
  • The collection wraps. Empty collections render no controls; one-card collections disable browsing.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

The CLI also installs Number roll.

Source

'use client';import * as React from 'react';import { ArrowLeft, ArrowRight } from 'lucide-react';import { EASE_OUT, EASE_SETTLE, INTERACTION_FOCUS, useInteractionReducedMotion } from './ui-interaction-motion';import { useCardDeckGesture, type DeckThrow } from './use-card-deck-gesture';import { NumberRoll } from './number-roll';export interface CardDeckProps<T extends { id: string }> extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children' | 'onChange'> {  items: T[];  renderCard: (item: T, state: { active: boolean; index: number }) => React.ReactNode;  getLabel?: (item: T) => string;  value?: string;  defaultValue?: string;  onValueChange?: (id: string) => void;  label?: string;  /** Fixed card height keeps the stack stable while content changes. */  height?: number;}function DepartingCard({ motion, reduced, children }: { motion: DeckThrow; reduced: boolean; children: React.ReactNode }) {  const ref = React.useRef<HTMLDivElement>(null);  React.useLayoutEffect(() => {    const el = ref.current;    if (!el || reduced) return;    const end = -motion.direction * el.offsetWidth * 1.35;    const fromAngle = Math.sign(motion.from) * Math.min(16, Math.sqrt(Math.abs(motion.from)) * 1.05);    const endAngle = -motion.direction * 15;    const dur = Math.min(380, Math.max(260, 360 - Math.abs(motion.velocity) * 80));    const animation = el.animate([      { transform: `translate3d(${motion.from}px,${-Math.abs(motion.from) * 0.05}px,0) rotate(${fromAngle}deg)`, opacity: 1 },      { transform: `translate3d(${end}px,-10px,0) rotate(${endAngle}deg)`, opacity: 0 },    ], { duration: dur, easing: EASE_OUT, fill: 'forwards' });    return () => animation.cancel();  }, [motion, reduced]);  return <div ref={ref} aria-hidden="true" inert className="pointer-events-none absolute inset-0 z-20 overflow-hidden rounded-[22px] border border-border bg-card text-card-foreground">{children}</div>;}/** Where a card behind the front one sits: lower and smaller by depth, rising as the front card leaves. */const stackTransform = (depth: number, progress: number, fanned: boolean) =>  `translate3d(0,${depth * (fanned ? 16 : 11) - progress * 11}px,0) scale(${1 - depth * 0.05 + progress * 0.05}) rotate(${fanned ? (depth % 2 ? -1 : 1) * depth * 1.3 : 0}deg)`;/** * A tactile stack. Gestures write transforms straight onto the cards, and every card has its own compositor layer, * so a drag moves pictures instead of repainting them. React changes only when a card is chosen. */export function CardDeck<T extends { id: string }>({ items, renderCard, getLabel = (item) => item.id, value, defaultValue, onValueChange, label = 'Card collection', height = 340, className = '', ...props }: CardDeckProps<T>) {  const reduced = useInteractionReducedMotion();  const [own, setOwn] = React.useState(defaultValue ?? items[0]?.id);  const selected = value ?? own;  const index = Math.max(0, items.findIndex((item) => item.id === selected));  const current = items[index];  const [leaving, setLeaving] = React.useState<DeckThrow | null>(null);  const busy = React.useRef(false);  const [fanned, setFanned] = React.useState(false);  const restoreCardFocus = React.useRef(false);  const choose = (direction: number, from?: number, velocity = 0) => {    if (items.length < 2 || !current || busy.current) return false;    const next = items[(index + direction + items.length) % items.length];    restoreCardFocus.current = Boolean(document.activeElement?.hasAttribute('data-deck-active'));    busy.current = !reduced;    const transform = frontRef.current ? getComputedStyle(frontRef.current).transform : 'none';    const origin = from ?? (transform === 'none' ? 0 : new DOMMatrixReadOnly(transform).m41);    if (!reduced) setLeaving({ id: current.id, direction, from: origin, velocity });    if (value === undefined) setOwn(next.id);    onValueChange?.(next.id);    return true;  };  const backs = React.useRef(new Map<number, HTMLDivElement>());  const fannedRef = React.useRef(false);  const onProgress = (progress: number, settle: boolean) => {    if (reduced) return;    backs.current.forEach((el, depth) => {      el.style.transition = settle ? `transform 480ms ${EASE_SETTLE}` : 'none';      el.style.transform = stackTransform(depth, progress, fannedRef.current);    });  };  const { rootRef, frontRef, dragging, handlers, resetFront } = useCardDeckGesture({ id: current?.id, count: items.length, reduced, onThrow: choose, onProgress });  if (leaving && leaving.id === current?.id) {    setLeaving(null);  }  React.useLayoutEffect(() => {    if (restoreCardFocus.current) { frontRef.current?.focus({ preventScroll: true }); restoreCardFocus.current = false; }  }, [current?.id, frontRef]);  React.useLayoutEffect(() => {    if (leaving && leaving.id === current?.id) {      // A controlled parent may reject a selection. Keep the original card available.      resetFront();      busy.current = false;    }  }, [leaving, current?.id, resetFront]);  React.useEffect(() => {    const timer = window.setTimeout(() => { setLeaving(null); busy.current = false; }, reduced ? 0 : 430);    return () => window.clearTimeout(timer);  }, [selected, leaving, reduced]);  const ordered = current ? Array.from({ length: Math.min(4, items.length) }, (_, depth) => ({ item: items[(index + depth) % items.length], depth })) : [];  const departing = leaving && items.find((item) => item.id === leaving.id);  const expanded = fanned && !reduced;  React.useLayoutEffect(() => {    fannedRef.current = expanded;  });  return (    <div ref={rootRef} aria-label={label} role="region" className={`w-full text-foreground ${className}`} {...props}>      <div className="mx-auto max-w-[320px] px-3">        <div className="relative isolate" style={{ height: Math.max(120, height) }} onPointerEnter={() => setFanned(true)} onPointerLeave={() => setFanned(false)}>          {ordered.slice().reverse().map(({ item, depth }) => {            const active = depth === 0;            return (              <div key={item.id} ref={active ? frontRef : (el) => { if (el) backs.current.set(depth, el); }} tabIndex={active ? 0 : -1} role="group" aria-roledescription="card"                aria-label={active ? `${getLabel(item)}, ${index + 1} of ${items.length}. Use Left and Right to browse.` : undefined}                aria-hidden={!active || undefined} inert={!active} data-deck-active={active || undefined}                onKeyDown={active ? (event) => {                  if (event.target !== event.currentTarget) return;                  if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') { event.preventDefault(); choose(event.key === 'ArrowRight' ? 1 : -1); }                } : undefined}                onFocus={active ? () => setFanned(true) : undefined}                onBlur={active ? (event) => { if (!event.currentTarget.contains(event.relatedTarget as Node)) setFanned(false); } : undefined}                {...(active ? handlers : {})}                className={`absolute inset-0 overflow-hidden rounded-[22px] border border-border bg-card text-card-foreground ${active ? `${dragging ? 'cursor-grabbing' : 'cursor-grab'} ${INTERACTION_FOCUS}` : 'pointer-events-none'}`}                style={{                  zIndex: 10 - depth, touchAction: active ? 'pan-y' : undefined, willChange: reduced ? undefined : 'transform',                  transform: active ? undefined : stackTransform(depth, 0, expanded),                  transformOrigin: '50% 100%',                  transition: reduced || dragging ? 'none' : `transform 520ms ${EASE_SETTLE}`,                }}>                {renderCard(item, { active, index: items.indexOf(item) })}              </div>            );          })}          {leaving && departing && <DepartingCard key={leaving.id} motion={leaving} reduced={reduced}>{renderCard(departing, { active: false, index: items.indexOf(departing) })}</DepartingCard>}        </div>      </div>      {current && <div className="mt-14 flex items-center justify-center gap-5">        <button type="button" aria-label="Previous card" disabled={items.length < 2} onClick={() => choose(-1)} className={`cursor-pointer disabled:cursor-default flex size-9 items-center justify-center rounded-full border border-border transition-colors hover:bg-accent disabled:opacity-30 ${INTERACTION_FOCUS}`}><ArrowLeft className="size-3.5" aria-hidden="true" /></button>        <p aria-live="polite" aria-atomic="true" className="min-w-12 text-center font-mono text-[11px] tabular-nums text-muted-foreground"><NumberRoll value={index + 1} format={{ minimumIntegerDigits: 2 }} /> / <NumberRoll value={items.length} format={{ minimumIntegerDigits: 2 }} /></p>        <button type="button" aria-label="Next card" disabled={items.length < 2} onClick={() => choose(1)} className={`cursor-pointer disabled:cursor-default flex size-9 items-center justify-center rounded-full border border-border transition-colors hover:bg-accent disabled:opacity-30 ${INTERACTION_FOCUS}`}><ArrowRight className="size-3.5" aria-hidden="true" /></button>      </div>}    </div>  );}

Registry item: /r/card-deck.json