Throw Deck

A throwable card deck with spring physics, drag gestures, and shared-layout detail views.

Throw Deck

A throwable card deck with spring physics, drag gestures, and shared-layout detail views.

The top card can be grabbed and flung away; it arcs out, swaps to the back of the pile at the apex, and springs home as the next card rises into place. A short tap opens a modal detail view with the card artwork flowing into the panel. The previous/next buttons and arrow keys cycle the deck without dragging.

Installation

Install the component with the Craft-UI Blocks CLI — it drops throw-deck.tsx into your project and wires up its runtime dependencies:

npx mvpblocks add throw-deck
pnpm dlx mvpblocks add throw-deck
yarn dlx mvpblocks add throw-deck
bunx mvpblocks add throw-deck

File Structure

The CLI saves the component to your project's components/ui folder:

Import the default export from @/components/ui/throw-deck. The component is named CardStack, and its prop types are exported as CardStackProps, DeckCard, and DeckSpec:

"use client";

import CardStack from "@/components/ui/throw-deck";

export function DeckCard() {
  return <CardStack />;
}

Usage

CardStack is a client component and every prop is optional — with no props it renders four demo product cards with inline SVG artwork. Drop it into a wide card slot:

"use client";

import CardStack from "@/components/ui/throw-deck";

export function ProductDeck() {
  return <CardStack />;
}

Each DeckCard needs a unique id, title, subtitle, art, description, and a list of specs. art is any React node; inline SVG keeps the component portable with no image files. price is optional:

const cards = [
  {
    id: "poster-01",
    title: "Low Sun",
    subtitle: "Risograph · 2026",
    art: <MyArtwork variant="sun" />,
    description: "Three-color risograph print on warm paper.",
    specs: [
      { label: "Size", value: "50 × 70 cm" },
      { label: "Paper", value: "Bright 300g" },
      { label: "Edition", value: "of 25" },
    ],
    price: "$64",
  },
  // Add more cards...
];

<CardStack
  cards={cards}
  accent="#3b82f6"
  actionLabel="Add to cart"
  onChange={(card) => console.log("top card", card.title)}
  onAction={(card) => console.log("action", card.title)}
/>

onChange fires after a throw or cycle settles with the new top card, and onAction fires when the detail view's primary button is pressed. Use className to adjust width or height:

<CardStack className="max-w-lg" />

Theming

Cards are rounded-2xl border-zinc-200 bg-white with a layered shadow, inverting to dark:border-zinc-800 dark:bg-zinc-900 in dark mode. Shadows deepen while dragging or flying. The detail panel is a white (or dark:bg-zinc-950) overlay, the specs use a three-column grid, and the close and nav buttons are circular zinc controls.

accent is written as a CSS custom property --deck-accent on the wrapper. It paints the demo artwork and the primary action button; the default is #f97316:

<CardStack accent="#10b981" actionLabel="Buy now" />

Your own art nodes control their own palette. Focus rings are a 2px zinc-400 ring with a 4px offset, and the active card shows a grab cursor.

Animations

  • Resting pile: cards sit at 0°, -2°, 1.5°, and -1°, each offset 9px down and scaled down 3.5% per slot.
  • Drag: rotation follows the pointer up to 18°; the pivot flips depending on whether you grabbed the top or bottom half. A mostly vertical touch drag is left to the browser for page scrolling.
  • Throw: velocity over 0.5px/ms or a drag past 35% of the card width commits a throw. The card flies until it clears the deck, then turns around and springs into the last slot.
  • Springs: home is k=420, c=30, flight is k=150, c=15, and return is k=240, c=26, integrated in 1/240s sub-steps; the RAF loop sleeps once everything settles.
  • Rise: the card beneath lifts toward the vacated slot as the drag approaches the throw threshold.
  • Details: the card and artwork use shared-layout transitions (0.45s spring, no bounce) into the modal; text fades in with a 0.25s ease after an 0.08s delay.
  • Reduced motion: throws and layout transitions snap to their targets and MotionConfig reducedMotion="user" is respected.

How it works

Each card is a physics body with position, rotation, scale, and velocity. stepBody applies a spring on every axis, and write pushes the resulting transform and z-index straight to the DOM node. clearance calculates how far a card must travel in the throw direction before it no longer covers the deck.

On release, releaseVelocity samples only the last 90ms of the gesture. A flick back toward the deck is treated as a change of mind and springs home; a fast or far release calls throwTop, sends the card along its arc, and rotates the order array so the next card becomes top. bringBack does the reverse for the previous button and left arrow.

Only the top card is focusable and interactive; the rest are inert. The detail view is a modal role="dialog" with focus moved to the close button, Escape to dismiss, and focus returned to the new top card. The footer counter is an aria-live region so screen readers announce the active card.

Props

Prop

Type

DeckCard

Prop

Type

DeckSpec

Prop

Type

Registry Bundle

Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: motion (throws, shared-layout details, reduced-motion handling) and lucide-react (chevron and close icons).

Contact

If you find any bug or issue, feel free to raise an issue.

Last updated on