# Card deck

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

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.

## Install

```bash
npx shadcn@latest add @hairline/card-deck
```

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

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/card-deck
- Registry item (source): https://hairlineui.com/r/card-deck.json

## 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>}
/>
```

## A controlled collection

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

```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

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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: T) => string` | `item.id` | 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` | `number` | `340` | Card height in CSS pixels, at least 120. Leave space for your longest content. |
| `label` | `string` | `Card collection` | Accessible label for the collection region. |
| `className` | `string` | – | Additional classes on the root. Standard div attributes are forwarded. |

## 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.

## What it handles

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.

- **It fans out**: Hover or focus the front card and the cards behind open slightly, so people can see there is more.
- **It follows your hand**: The front card tracks the pointer with a slight rotation while the next one rises.
- **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.
- **It springs back**: An unfinished drag returns on a damped spring that keeps the speed of your release.
- **Your content in every card**: renderCard draws anything. Links, buttons and inputs inside a card keep their own behaviour.
- **Works without dragging**: Arrow keys and Previous and Next buttons browse the deck, and vertical touch scrolling still works.

## Replaces

a carousel library, drag and flick code, a spring animation, keyboard handling, wrap-around logic

## Where it fits

- **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.

## Notes

- 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.

License: MIT (https://hairlineui.com/license#components)
