Card Stack

A Tinder-style stack of draggable cards that flick off left or right past a threshold.

ByMihirJaiswalUpdated

Loading...

Card Stack is a Tinder-style React component that renders a deck of cards and lets the top one be grabbed and flicked left or right. Past the configurable drag threshold the card flies off and fires onSwipe; under it, the next card promotes to the top. Two more cards are mounted below in a scaled, offset stack to give a sense of depth without rendering the whole deck.

Useful for product tours, onboarding sequences, quote reels, and anywhere the "cycle through a small set" interaction beats a static carousel.

Installation

Usage

import { CardStack } from "@/components/ui/card-stack";
 
const items = [
  { id: "1", title: "First" },
  { id: "2", title: "Second" },
  { id: "3", title: "Third" },
];
 
export function Example() {
  return (
    <div className="relative h-72 w-xs">
      <CardStack
        items={items}
        getKey={(item) => item.id}
        renderCard={(item) => (
          <div className="flex h-full items-center justify-center">
            <h3 className="text-lg font-medium">{item.title}</h3>
          </div>
        )}
        onSwipe={(item, direction) => console.log(item, direction)}
      />
    </div>
  );
}

Props

PropTypeDefaultDescription
itemsT[]—Items to render as cards. First is on top.
renderCard(item, index) => ReactNode—Renders each card's content.
getKey(item, index) => string | numberindexStable key per item.
thresholdnumber120Drag distance in pixels that triggers a swipe.
stackSizenumber3How many cards are mounted at once.
stackOffsetnumber14Pixel offset between stacked cards.
stackScalenumber0.06Scale decrement per level back in the stack.
onSwipe(item, direction) => void—Fires when a card is swiped off ("left" | "right").
onEmpty() => void—Fires when the stack has no cards left.
classNamestring—Extra classes on the stack wrapper.
cardClassNamestring—Extra classes on each card surface.