kipluo
React9 min read3 steps

How to Create a Draggable Card Stack with Spring Physics

A production card stack needs more than transform: translateX. This guide covers pointer capture, grip-point torque, velocity projection on release, and recycling dismissed cards behind the deck.

You needReact 18+Pointer Capture APIUnderstanding of velocity = dx / dt
Loading demo
Grab near the top vs bottom of the top card to feel opposite torque, or press Left/Right arrow keys.Component page

1. Calculate grip-aware tilt instead of fixed rotation

Most card stacks rotate purely by horizontal offset (deg = dx * 0.05). That looks acceptable until you drag from the bottom of a card: in real life, pulling the bottom-right corner tilts the card counter-clockwise, while pulling the top-right tilts clockwise.

Capture the normalized vertical grab position gripY in [-1, 1] on pointerdown, then scale rotation by dx * 0.06 * (gripY >= 0 ? -1 : 1) (with a gentle floor so grabbing dead-center still tilts slightly).

gripTorque.ts
export function computeGripSign(clientY: number, rect: DOMRect): number {
  const normalized = ((clientY - rect.top) / rect.height) * 2 - 1; // -1 (top) to +1 (bottom)
  // Top half tilts with the drag (+), bottom half tilts against (-)
  return Math.abs(normalized) < 0.15 ? 0.45 : -normalized;
}

2. Project fling velocity on release

Users rarely drag a card all the way past 50% of the screen—they flick it 40 pixels very fast. If you only check distance > 120, fast flicks bounce stubbornly back.

Track an exponential moving average of pointer velocity (px/s) during pointermove, then project where the card would land after 140ms: projectedX = dx + vx * 0.14. Dismiss whenever |projectedX| > threshold.

CardStackCore.tsx
import { useRef, useState } from "react";

type Card = { id: string; title: string };

export function SimpleCardStack({ initial }: { initial: Card[] }) {
  const [cards, setCards] = useState(initial);
  const drag = useRef({ active: false, startX: 0, lastX: 0, lastT: 0, vx: 0, dx: 0 });
  const topRef = useRef<HTMLDivElement>(null);

  const onDown = (e: React.PointerEvent<HTMLDivElement>) => {
    e.currentTarget.setPointerCapture(e.pointerId);
    drag.current = { active: true, startX: e.clientX, lastX: e.clientX, lastT: performance.now(), vx: 0, dx: 0 };
  };

  const onMove = (e: React.PointerEvent<HTMLDivElement>) => {
    if (!drag.current.active || !topRef.current) return;
    const now = performance.now();
    const dt = Math.max(1, now - drag.current.lastT) / 1000;
    const instVx = (e.clientX - drag.current.lastX) / dt;
    drag.current.vx = drag.current.vx * 0.7 + instVx * 0.3;
    drag.current.lastX = e.clientX;
    drag.current.lastT = now;
    drag.current.dx = e.clientX - drag.current.startX;
    topRef.current.style.transform = `translate3d(${drag.current.dx}px, 0, 0) rotate(${drag.current.dx * 0.05}deg)`;
  };

  const onUp = () => {
    if (!drag.current.active) return;
    drag.current.active = false;
    const projected = drag.current.dx + drag.current.vx * 0.14;
    if (Math.abs(projected) > 110) {
      setCards((prev) => [...prev.slice(1), prev[0]]);
    }
    if (topRef.current) topRef.current.style.transform = "";
  };

  return (
    <div className="relative h-52 w-72 select-none touch-pan-y">
      {cards.slice(0, 3).map((card, depth) => (
        <div
          key={card.id}
          ref={depth === 0 ? topRef : undefined}
          onPointerDown={depth === 0 ? onDown : undefined}
          onPointerMove={depth === 0 ? onMove : undefined}
          onPointerUp={depth === 0 ? onUp : undefined}
          style={{
            zIndex: 3 - depth,
            transform: `translateY(${depth * 10}px) scale(${1 - depth * 0.05})`,
          }}
          className="absolute inset-0 cursor-grab rounded-2xl border bg-white p-5 shadow-md active:cursor-grabbing"
        >
          <p className="font-bold">{card.title}</p>
        </div>
      ))}
    </div>
  );
}

3. Yield to vertical scroll on touch devices

Always put touch-action: pan-y on the card container and wait until the pointer travels ~6px before deciding whether the gesture is horizontal (card drag) or vertical (page scroll). If |dy| > |dx|, release your drag state so mobile browsers scroll smoothly without fighting the card.

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 3 checked

Build it yourself or install it

Build it yourself when

You need a simple 3-card onboarding step with minimal motion polish.

Install Kipluo when

You want mid-flight interruption, arc-and-tuck z-index handoff, controlled order/reset(), mobile scroll-lock, and spatial audio.

Keep building