Swipe card stack
A fanned deck of cards you can fling left or right; the top card flies off with spin and the rest of the deck steps forward.
"use client";
import {
useRef,
useState,
type CSSProperties,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./card-stack.css";
export type StackCard = {
title: string;
text: string;
meta: string;
color: string;
ink?: string;
};
type CardStackProps = { cards: StackCard[]; theme?: "light" | "dark" };
/**
* CardStack
* - The top card follows your finger or mouse; it rotates a little with the drag.
* - Let go past 30% of the card width and it flies off, then rejoins at the back.
* - Arrow keys and the buttons do the same for keyboard users.
*/
export function CardStack({ cards, theme = "light" }: CardStackProps) {
const [order, setOrder] = useState(cards.map((_, i) => i));
const [fling, setFling] = useState<-1 | 0 | 1>(0);
const drag = useRef<{ x: number; dx: number; id: number } | null>(null);
const top = useRef<HTMLDivElement>(null);
const send = (dir: -1 | 1) => {
if (fling) return;
setFling(dir);
// After the fly-out animation, move the card to the back of the deck.
setTimeout(() => {
setOrder((o) => [...o.slice(1), o[0]]);
setFling(0);
top.current?.style.removeProperty("--dx");
}, 380);
};
const down = (e: ReactPointerEvent<HTMLDivElement>) => {
if (fling) return;
e.currentTarget.setPointerCapture(e.pointerId);
drag.current = { x: e.clientX, dx: 0, id: e.pointerId };
e.currentTarget.dataset.dragging = "";
};
const move = (e: ReactPointerEvent<HTMLDivElement>) => {
if (!drag.current) return;
drag.current.dx = e.clientX - drag.current.x;
e.currentTarget.style.setProperty("--dx", `${drag.current.dx}px`);
};
const up = (e: ReactPointerEvent<HTMLDivElement>) => {
if (!drag.current) return;
const { dx } = drag.current;
drag.current = null;
delete e.currentTarget.dataset.dragging;
if (Math.abs(dx) > e.currentTarget.offsetWidth * 0.3) send(dx > 0 ? 1 : -1);
else e.currentTarget.style.setProperty("--dx", "0px");
};
const current = cards[order[0]];
return (
<div className="gu-stack" data-theme={theme}>
<div
className="gu-stack__deck"
role="group"
aria-roledescription="card deck"
aria-label={`${current.title}, card ${order[0] + 1} of ${cards.length}`}
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "ArrowRight") send(1);
if (e.key === "ArrowLeft") send(-1);
}}
>
{order
.slice(0, 4)
.map((cardIndex, depth) => {
const c = cards[cardIndex];
const isTop = depth === 0;
return (
<div
key={cardIndex}
ref={isTop ? top : undefined}
className="gu-stack__card"
data-fling={isTop && fling ? fling : undefined}
aria-hidden={!isTop}
style={
{
"--depth": depth,
"--c": c.color,
"--ink": c.ink ?? "#0e0e10",
zIndex: 10 - depth,
} as CSSProperties
}
onPointerDown={isTop ? down : undefined}
onPointerMove={isTop ? move : undefined}
onPointerUp={isTop ? up : undefined}
onPointerCancel={isTop ? up : undefined}
>
<p className="gu-stack__meta">{c.meta}</p>
<h3>{c.title}</h3>
<p className="gu-stack__text">{c.text}</p>
</div>
);
})
.reverse()}
</div>
<div className="gu-stack__controls">
<button
type="button"
onClick={() => send(-1)}
aria-label="Previous card, send left"
>
←
</button>
<span aria-live="polite">
{order[0] + 1} / {cards.length}
</span>
<button
type="button"
onClick={() => send(1)}
aria-label="Next card, send right"
>
→
</button>
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Swipe card stack
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A deck of colorful cards stacked and fanned behind each other. Drag the top card sideways and release past 30% of its width to fling it off; it then rejoins at the back while the next cards move forward. Arrow buttons and keys do the same.
## Layout and look
- Deck: max 360px wide, 3:4 ratio (max 420px tall), radius 26px.
- Cards: solid color backgrounds (orange #ff5a1f, lime #d7ff3a, ultramarine #2b35ff with cream text, cream #e9e2d3), padding 26px, a small bold meta line at the top, a big headline at the bottom (35px, weight 900, -0.045em), and a short paragraph.
- Each card behind is offset 10px right, 12px up, rotated 3°, and scaled down 5% per level. Only 4 are rendered.
- Controls under the deck: two 48px round dark buttons (← →) around a "1 / 4" counter.
## Behavior and motion
1. Pointer down on the top card captures the pointer; --dx follows the drag; rotation = dx × 0.05deg. No transition while dragging.
2. Release: if |dx| > 30% of width, add a fling class: translate ±140%, rotate ±24°, 380ms ease-in, then move the card to the end of the order. Otherwise spring back (500ms cubic-bezier(0.16, 1, 0.3, 1)).
3. Remaining cards animate to their new depth positions with the same easing.
## Touch
touch-action: pan-y so vertical page scroll still works while horizontal drags move the card.
## Accessibility
- The deck is a focusable group with aria-roledescription "card deck" and a label naming the current card; ArrowLeft/ArrowRight fling.
- Hidden cards are aria-hidden; the counter is aria-live.
- prefers-reduced-motion: fade instead of fly, no rotation.
## Props
cards ({ title, text, meta, color, ink? }[]), theme.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### CardStack.tsx
```tsx
"use client";
import {
useRef,
useState,
type CSSProperties,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./card-stack.css";
export type StackCard = {
title: string;
text: string;
meta: string;
color: string;
ink?: string;
};
type CardStackProps = { cards: StackCard[]; theme?: "light" | "dark" };
/**
* CardStack
* - The top card follows your finger or mouse; it rotates a little with the drag.
* - Let go past 30% of the card width and it flies off, then rejoins at the back.
* - Arrow keys and the buttons do the same for keyboard users.
*/
export function CardStack({ cards, theme = "light" }: CardStackProps) {
const [order, setOrder] = useState(cards.map((_, i) => i));
const [fling, setFling] = useState<-1 | 0 | 1>(0);
const drag = useRef<{ x: number; dx: number; id: number } | null>(null);
const top = useRef<HTMLDivElement>(null);
const send = (dir: -1 | 1) => {
if (fling) return;
setFling(dir);
// After the fly-out animation, move the card to the back of the deck.
setTimeout(() => {
setOrder((o) => [...o.slice(1), o[0]]);
setFling(0);
top.current?.style.removeProperty("--dx");
}, 380);
};
const down = (e: ReactPointerEvent<HTMLDivElement>) => {
if (fling) return;
e.currentTarget.setPointerCapture(e.pointerId);
drag.current = { x: e.clientX, dx: 0, id: e.pointerId };
e.currentTarget.dataset.dragging = "";
};
const move = (e: ReactPointerEvent<HTMLDivElement>) => {
if (!drag.current) return;
drag.current.dx = e.clientX - drag.current.x;
e.currentTarget.style.setProperty("--dx", `${drag.current.dx}px`);
};
const up = (e: ReactPointerEvent<HTMLDivElement>) => {
if (!drag.current) return;
const { dx } = drag.current;
drag.current = null;
delete e.currentTarget.dataset.dragging;
if (Math.abs(dx) > e.currentTarget.offsetWidth * 0.3) send(dx > 0 ? 1 : -1);
else e.currentTarget.style.setProperty("--dx", "0px");
};
const current = cards[order[0]];
return (
<div className="gu-stack" data-theme={theme}>
<div
className="gu-stack__deck"
role="group"
aria-roledescription="card deck"
aria-label={`${current.title}, card ${order[0] + 1} of ${cards.length}`}
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "ArrowRight") send(1);
if (e.key === "ArrowLeft") send(-1);
}}
>
{order
.slice(0, 4)
.map((cardIndex, depth) => {
const c = cards[cardIndex];
const isTop = depth === 0;
return (
<div
key={cardIndex}
ref={isTop ? top : undefined}
className="gu-stack__card"
data-fling={isTop && fling ? fling : undefined}
aria-hidden={!isTop}
style={
{
"--depth": depth,
"--c": c.color,
"--ink": c.ink ?? "#0e0e10",
zIndex: 10 - depth,
} as CSSProperties
}
onPointerDown={isTop ? down : undefined}
onPointerMove={isTop ? move : undefined}
onPointerUp={isTop ? up : undefined}
onPointerCancel={isTop ? up : undefined}
>
<p className="gu-stack__meta">{c.meta}</p>
<h3>{c.title}</h3>
<p className="gu-stack__text">{c.text}</p>
</div>
);
})
.reverse()}
</div>
<div className="gu-stack__controls">
<button
type="button"
onClick={() => send(-1)}
aria-label="Previous card, send left"
>
←
</button>
<span aria-live="polite">
{order[0] + 1} / {cards.length}
</span>
<button
type="button"
onClick={() => send(1)}
aria-label="Next card, send right"
>
→
</button>
</div>
</div>
);
}
```
### card-stack.css
```css
/* CardStack: card colors come from each card's `color` (and optional `ink`). */
.gu-stack {
--text: #0e0e10;
--muted: #57534b;
--btn: #0e0e10;
--btn-text: #f3eee3;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
display: grid;
justify-items: center;
gap: 22px;
width: min(100%, 360px);
font-family: system-ui, sans-serif;
color: var(--text);
}
.gu-stack[data-theme="dark"] {
--text: #f3eee3;
--muted: #aea89c;
--btn: #f3eee3;
--btn-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-stack__deck {
position: relative;
width: 100%;
aspect-ratio: 3 / 4;
max-height: 420px;
border-radius: 26px;
outline: none;
}
.gu-stack__deck:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 8px;
}
.gu-stack__card {
--dx: 0px;
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
padding: 26px;
border-radius: 26px;
background: var(--c);
color: var(--ink);
box-shadow: 0 24px 40px -24px rgb(14 14 16 / 0.55);
cursor: grab;
touch-action: pan-y;
user-select: none;
/* Cards behind the top one are smaller, lower and fanned */
transform: translate(
calc(var(--dx) + var(--depth) * 10px),
calc(var(--depth) * -12px)
)
rotate(calc(var(--dx) * 0.05deg + var(--depth) * 3deg))
scale(calc(1 - var(--depth) * 0.05));
transition: transform 0.5s var(--ease);
}
.gu-stack__card[data-dragging] {
cursor: grabbing;
transition: none;
}
/* Fly-off to either side */
.gu-stack__card[data-fling="1"] {
transform: translate(140%, 20px) rotate(24deg);
transition: transform 0.38s ease-in;
}
.gu-stack__card[data-fling="-1"] {
transform: translate(-140%, 20px) rotate(-24deg);
transition: transform 0.38s ease-in;
}
.gu-stack__meta {
margin: 0;
font-size: 0.8rem;
font-weight: 800;
opacity: 0.75;
}
.gu-stack__card h3 {
margin: auto 0 0;
font-size: 2.2rem;
font-weight: 900;
line-height: 0.95;
letter-spacing: -0.045em;
}
.gu-stack__text {
margin: 12px 0 0;
font-size: 0.95rem;
line-height: 1.45;
opacity: 0.85;
}
.gu-stack__controls {
display: flex;
align-items: center;
gap: 18px;
color: var(--muted);
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.gu-stack__controls button {
width: 48px;
height: 48px;
border: 0;
border-radius: 50%;
background: var(--btn);
color: var(--btn-text);
font-size: 1.2rem;
cursor: pointer;
transition: transform 0.2s;
}
.gu-stack__controls button:active {
transform: scale(0.92);
}
.gu-stack__controls button:focus-visible {
outline: 2px solid var(--focus);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.gu-stack__card {
transform: translate(calc(var(--depth) * 10px), calc(var(--depth) * -12px))
scale(calc(1 - var(--depth) * 0.05));
transition: opacity 0.2s;
}
.gu-stack__card[data-fling] {
transform: none;
opacity: 0;
transition: opacity 0.2s;
}
}
```
You might also want
Next piece: Tag inputSnap carousel with progress
Components, intermediate
Draggable bottom sheet
Components, advanced
Kanban board
Components, advanced