# Card Stack

> A deck you swipe through: fling the top card and it tucks in at the back.

Category: Cards · Free · Updated 2026-10-20 · Live demo: https://www.motionkitui.com/components/card-stack

## When to use it

Testimonials, onboarding tips and photo or product decks on mobile-first pages, where swiping feels more natural than arrows.

## How it works

Every card sits in the same CSS grid cell, so the stack sizes itself to the tallest card. The cards keep an order array: the top one is draggable on the x axis, and a fling past the threshold (or a fast flick) moves it to the back. Each card then springs to its new depth, slightly lower and smaller than the one in front. Left and right arrow keys shuffle the stack too, and screen readers hear which card is on top.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/card-stack.json
```

Or copy `components/motionkit/card-stack.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `cards` | `ReactNode[]` |  | One entry per card, front to back. |
| `offset` | `number` | `14` | Vertical gap between stacked cards, in pixels. |
| `threshold` | `number` | `90` | Drag distance (px) that sends the top card to the back. |
| `label` | `string` | `"Cards"` | Accessible name for the stack. |

## Accessibility

- **Keyboard: Pass.** The stack is focusable; left and right arrow keys shuffle it, so dragging is never required.
- **Screen readers: Pass.** Only the top card is exposed, the stack says how to use the arrow keys, and a live region announces "Card 2 of 4".
- **Reduced motion: Pass.** With reduced motion on, cards change instantly with no spring.
- **Colour contrast: Pass.** Default card text measures 15.5:1. Check any colours you add.

## Source

```tsx
"use client";

import { useState, type KeyboardEvent, type ReactNode } from "react";
import { motion, useReducedMotion } from "motion/react";

type CardStackProps = {
  /** One entry per card, front to back. */
  cards: ReactNode[];
  /** Vertical gap between stacked cards, in pixels. */
  offset?: number;
  /** Drag distance (px) that sends the top card to the back. */
  threshold?: number;
  /** Accessible name for the stack, e.g. "Testimonials". */
  label?: string;
  className?: string;
};

export function CardStack({ cards, offset = 14, threshold = 90, label = "Cards", className = "" }: CardStackProps) {
  // order[0] is the card on top
  const [order, setOrder] = useState(() => cards.map((_, index) => index));
  const reduceMotion = useReducedMotion();
  const visible = Math.min(order.length, 4);

  const next = () => setOrder((current) => [...current.slice(1), current[0]]);
  const previous = () => setOrder((current) => [current[current.length - 1], ...current.slice(0, -1)]);

  function handleKeyDown(event: KeyboardEvent<HTMLDivElement>) {
    if (event.key === "ArrowRight") next();
    else if (event.key === "ArrowLeft") previous();
    else return;
    event.preventDefault();
  }

  return (
    <div
      role="group"
      aria-roledescription="card stack"
      aria-label={`${label}. Use the left and right arrow keys to shuffle.`}
      tabIndex={0}
      onKeyDown={handleKeyDown}
      className={`relative rounded-2xl focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-[#5FAEB4] ${className}`}
      style={{ paddingBottom: offset * (visible - 1) }}
    >
      {/* the grid stacks every card in the same cell so the container sizes to the tallest */}
      <div className="grid">
        {order.map((cardIndex, depth) => {
          const isTop = depth === 0;
          return (
            <motion.div
              key={cardIndex}
              aria-hidden={!isTop}
              className="col-start-1 row-start-1 rounded-2xl border border-white/10 bg-[#141B1B] p-6 text-[#F6F1E8] shadow-xl shadow-black/40"
              style={{ zIndex: cards.length - depth, cursor: isTop ? "grab" : "default" }}
              initial={false}
              animate={{
                y: Math.min(depth, visible - 1) * offset,
                scale: 1 - Math.min(depth, visible - 1) * 0.05,
                opacity: depth < visible ? 1 : 0,
                x: 0,
                rotate: 0,
              }}
              transition={reduceMotion ? { duration: 0 } : { type: "spring", stiffness: 300, damping: 28 }}
              drag={isTop ? "x" : false}
              dragSnapToOrigin
              whileDrag={{ cursor: "grabbing" }}
              onDragEnd={(_, info) => {
                if (Math.abs(info.offset.x) > threshold || Math.abs(info.velocity.x) > 600) next();
              }}
            >
              {cards[cardIndex]}
            </motion.div>
          );
        })}
      </div>
      <p className="sr-only" aria-live="polite">
        Card {order[0] + 1} of {cards.length}
      </p>
    </div>
  );
}
```
