Components65 / 118
Fan Stack
A stack of cards that fans out like files when you come near. Closed, the ones behind peek over the top; bring the cursor in and they fan out sideways in 3D, the card under the cursor lifting toward you with its label while its neighbours lean away. Click a card to bring it to the front, or drag one out and flick it to send it round to the back of the deck. A glossy sheen slides over them as they turn.
Card stackCard stack3D fan3D fanFlick to shuffleFlick to shuffleSpring physicsSpring physicsSheenSheenGSAPGSAP
Hover to fan, then over a cardClick to bring forward, flick to send back
Tweak
LIVELIVECards
240px
Number the labels
Fan
0.58
30°
Usage
Follows your tweaks.
usage.tsx
import { FanStack } from "@/components/inreact/fan-stack";
const cards = [
{ src: "/work/camera.jpg", label: "Camera Background" },
{ src: "/work/car.jpg", label: "Car Background" },
{ src: "/work/player.jpg", label: "Player Background" },
];
export function Backgrounds() {
return <FanStack cards={cards} onSelect={(card) => console.log(card.label)} />;
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/fan-stack.jsonOr copy the source
Install the dependencies, then save this as components/inreact/fan-stack.tsx.
terminal
npm install gsapcomponents/inreact/fan-stack.tsx
"use client";
import { gsap } from "gsap";
import { useEffect, useRef, useState } from "react";
/**
* Fan Stack: a stack of cards that fans out like files when you come near.
*
* Closed, the cards sit in a neat stack, the ones behind peeking over the top.
* Bring the cursor in and they fan out sideways in 3D, each turned like a file
* on a desk; the one under the cursor lifts toward you with its label, and its
* neighbours lean away. Click a card to bring it to the front; drag one out
* and flick it, and it sails round to the back of the deck. A glossy sheen
* slides over the cards as they turn. Everything runs on springs.
*
* <FanStack cards={[{ src: "/a.jpg", label: "Camera" }, { src: "/b.jpg", label: "Car" }, ...]} />
*/
export interface FanCard {
src: string;
label?: string;
alt?: string;
}
export interface FanStackProps {
cards: FanCard[];
/** Card width, px. */
width?: number;
/** Width / height. */
aspect?: number;
/** How far the fan spreads (fraction of a card width per card). */
spread?: number;
/** Degrees each card turns when fanned. */
turn?: number;
/** Number the labels (01, 02...). */
numbered?: boolean;
onSelect?: (card: FanCard, index: number) => void;
className?: string;
}
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
type Spring = { v: number; vel: number; t: number };
const sp = (v: number): Spring => ({ v, vel: 0, t: v });
export function FanStack({ cards, width = 260, aspect = 1.3, spread = 0.58, turn = 30, numbered = true, onSelect, className }: FanStackProps) {
const rootRef = useRef<HTMLDivElement>(null);
const els = useRef<(HTMLDivElement | null)[]>([]);
const sheens = useRef<(HTMLDivElement | null)[]>([]);
const tags = useRef<(HTMLDivElement | null)[]>([]);
const n = cards.length;
const h = Math.round(width / aspect);
// order[k] = which card sits in slot k (0 = front)
const [order, setOrder] = useState(() => cards.map((_, i) => i));
const orderRef = useRef(order);
orderRef.current = order;
const cb = useRef(onSelect);
cb.current = onSelect;
useEffect(() => setOrder(cards.map((_, i) => i)), [n]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const still = reduced();
const S = cards.map(() => ({ x: sp(0), y: sp(0), z: sp(0), ry: sp(0), rz: sp(0), s: sp(1), lift: sp(0) }));
let open = false;
let hover = -1;
let ptrX = 0.5;
// gx, gy: where on the card it was grabbed; sx, sy: where the pointer went down
const drag = { i: -1, gx: 0, gy: 0, sx: 0, sy: 0, x: 0, y: 0, vx: 0, vy: 0, lx: 0, ly: 0, lt: 0, moved: false };
const targets = () => {
const ord = orderRef.current;
const fanW = width * spread * (n - 1);
ord.forEach((ci, k) => {
const c = S[ci];
if (ci === drag.i && drag.moved) return;
if (!open) {
// the stack: the ones behind peek over the top
c.x.t = 0;
c.y.t = -k * h * 0.075;
c.z.t = -k * 46;
c.ry.t = 0;
c.rz.t = 0;
c.s.t = 1 - k * 0.055;
c.lift.t = 0;
} else {
// the fan: spread right, each turned like a file; the hovered one lifts, its neighbours lean away
const hk = hover >= 0 ? ord.indexOf(hover) : -1;
const away = hk >= 0 && k !== hk ? Math.sign(k - hk) * width * 0.07 * Math.max(0, 1 - Math.abs(k - hk) * 0.35) : 0;
c.x.t = k * width * spread - fanW / 2 + away;
c.y.t = 0;
c.z.t = -k * 62;
c.ry.t = -turn;
c.rz.t = 0;
c.s.t = 1;
c.lift.t = ci === hover ? 1 : 0;
}
});
};
const K = 170, C = 18;
const step = (s: Spring, dt: number, k = K, c = C) => {
const a = k * (s.t - s.v) - c * s.vel;
s.vel += a * dt;
s.v += s.vel * dt;
return Math.abs(s.t - s.v) > 0.01 || Math.abs(s.vel) > 0.01;
};
let awake = true;
const wake = () => (awake = true);
const tick = (_t: number, dms: number) => {
if (!awake) return;
const dt = Math.min(0.033, dms / 1000);
targets();
let moving = drag.i >= 0;
const ord = orderRef.current;
S.forEach((c, i) => {
if (i === drag.i && drag.moved) {
// held: the card follows the hand and tilts with its speed
c.x.t = drag.x;
c.y.t = drag.y;
c.z.t = 60;
c.ry.t = gsap.utils.clamp(-35, 35, drag.vx * 0.04);
c.rz.t = gsap.utils.clamp(-14, 14, drag.vx * 0.012);
c.s.t = 1.04;
}
for (const s of [c.x, c.y, c.z, c.ry, c.rz, c.s]) {
if (still) {
s.v = s.t;
s.vel = 0;
} else if (step(s, dt, i === drag.i ? 420 : K, i === drag.i ? 30 : C)) moving = true;
}
if (still) c.lift.v = c.lift.t;
else if (step(c.lift, dt, 220, 19)) moving = true;
const el = els.current[i];
if (!el) return;
const k = ord.indexOf(i);
const lift = c.lift.v;
el.style.transform = `translate3d(${c.x.v}px, ${c.y.v - lift * h * 0.13}px, ${c.z.v + lift * 40}px) rotateY(${c.ry.v * (1 - lift * 0.25)}deg) rotateZ(${c.rz.v}deg) scale(${c.s.v + lift * 0.035})`;
el.style.zIndex = String(i === drag.i ? 100 : 50 - k + Math.round(lift * 20));
// a glossy sheen that slides as the card turns and the cursor moves
const sh = sheens.current[i];
if (sh) sh.style.backgroundPosition = `${50 + c.ry.v * 1.6 + (ptrX - 0.5) * 40}% 0`;
const tag = tags.current[i];
if (tag) {
tag.style.opacity = String(Math.max(0, Math.min(1, lift * 1.6 - 0.2)));
tag.style.transform = `translateY(${(1 - lift) * 8}px)`;
}
});
if (!moving) awake = false;
};
gsap.ticker.add(tick);
const local = (e: PointerEvent) => {
const r = root.getBoundingClientRect();
return { x: e.clientX - (r.left + r.width / 2), y: e.clientY - (r.top + r.height / 2) };
};
let closeLater: gsap.core.Tween | null = null;
const onEnter = () => {
closeLater?.kill();
open = true;
wake();
};
const onLeave = () => {
if (drag.i >= 0) return;
open = false;
hover = -1;
wake();
};
const onMove = (e: PointerEvent) => {
const r = root.getBoundingClientRect();
ptrX = (e.clientX - r.left) / r.width;
const card = (e.target as HTMLElement).closest("[data-card]") as HTMLElement | null;
const hi = card ? Number(card.dataset.card) : -1;
if (hi !== hover && drag.i < 0) hover = hi;
if (drag.i >= 0) {
const l = local(e);
const now = performance.now();
const dt = Math.max(1, now - drag.lt);
drag.vx = drag.vx * 0.6 + ((e.clientX - drag.lx) / dt) * 1000 * 0.4;
drag.vy = drag.vy * 0.6 + ((e.clientY - drag.ly) / dt) * 1000 * 0.4;
drag.lx = e.clientX;
drag.ly = e.clientY;
drag.lt = now;
if (!drag.moved && Math.hypot(l.x - drag.sx, l.y - drag.sy) > 6) drag.moved = true;
drag.x = l.x - drag.gx;
drag.y = l.y - drag.gy;
}
wake();
};
const onDown = (e: PointerEvent) => {
const card = (e.target as HTMLElement).closest("[data-card]") as HTMLElement | null;
if (!card) return;
const i = Number(card.dataset.card);
const l = local(e);
Object.assign(drag, { i, gx: l.x - S[i].x.v, gy: l.y - S[i].y.v, sx: l.x, sy: l.y, x: S[i].x.v, y: S[i].y.v, vx: 0, vy: 0, lx: e.clientX, ly: e.clientY, lt: performance.now(), moved: false });
e.preventDefault();
wake();
};
const onUp = (e: PointerEvent) => {
if (drag.i < 0) return;
const i = drag.i;
const l = local(e);
const flicked = drag.moved && (Math.hypot(drag.vx, drag.vy) > 650 || Math.hypot(l.x - drag.sx, l.y - drag.sy) > width * 0.6);
const clicked = !drag.moved;
const ord = orderRef.current.slice();
const at = ord.indexOf(i);
ord.splice(at, 1);
if (flicked) ord.push(i); // flicked: round to the back
else if (clicked) ord.unshift(i); // clicked: to the front
else ord.splice(at, 0, i);
// keep the throw's energy as it flies to its new place
S[i].x.vel = drag.vx * 0.6;
S[i].y.vel = drag.vy * 0.6;
drag.i = -1;
drag.moved = false;
orderRef.current = ord;
setOrder(ord);
if (clicked) cb.current?.(cards[i], i);
// released outside: let the throw play out, then fold the fan (unless the pointer comes back)
const r = root.getBoundingClientRect();
if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) {
closeLater?.kill();
closeLater = gsap.delayedCall(0.9, onLeave);
}
wake();
};
root.addEventListener("pointerenter", onEnter);
root.addEventListener("pointerleave", onLeave);
root.addEventListener("pointermove", onMove);
root.addEventListener("pointerdown", onDown);
window.addEventListener("pointerup", onUp);
// keyboard focus fans it too
const onFocusIn = () => onEnter();
const onFocusOut = (e: FocusEvent) => !root.contains(e.relatedTarget as Node) && onLeave();
root.addEventListener("focusin", onFocusIn);
root.addEventListener("focusout", onFocusOut);
const onFocusCard = (e: FocusEvent) => {
const card = (e.target as HTMLElement).closest?.("[data-card]") as HTMLElement | null;
hover = card ? Number(card.dataset.card) : -1;
wake();
};
root.addEventListener("focusin", onFocusCard);
return () => {
gsap.ticker.remove(tick);
closeLater?.kill();
root.removeEventListener("pointerenter", onEnter);
root.removeEventListener("pointerleave", onLeave);
root.removeEventListener("pointermove", onMove);
root.removeEventListener("pointerdown", onDown);
window.removeEventListener("pointerup", onUp);
root.removeEventListener("focusin", onFocusIn);
root.removeEventListener("focusout", onFocusOut);
root.removeEventListener("focusin", onFocusCard);
};
// the deck's shape; cards are read by index
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [n, width, h, spread, turn]);
const fanW = width * (1 + spread * (n - 1));
return (
<div
ref={rootRef}
role="list"
aria-label="Cards"
className={["relative touch-none select-none", className].filter(Boolean).join(" ")}
// a hit area as wide as the open fan, so it doesn't snap shut at its edges
style={{ width: fanW + width * 0.2, height: h * 1.5, perspective: 900 }}
>
<div className="absolute top-1/2 left-1/2" style={{ transformStyle: "preserve-3d", width: 0, height: 0 }}>
{cards.map((c, i) => (
<div
key={c.src + i}
ref={(el) => {
els.current[i] = el;
}}
data-card={i}
role="listitem"
tabIndex={0}
aria-label={c.label ?? c.alt ?? `Card ${i + 1}`}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
const ord = order.filter((x) => x !== i);
setOrder([i, ...ord]);
onSelect?.(c, i);
}
}}
className="absolute cursor-grab outline-none focus-visible:ring-2 focus-visible:ring-white/60"
style={{ width, height: h, left: -width / 2, top: -h / 2, borderRadius: Math.round(width * 0.045), willChange: "transform", transformStyle: "preserve-3d" }}
>
<div
className="absolute inset-0 overflow-hidden"
style={{
borderRadius: "inherit",
boxShadow: "0 1px 2px rgba(0,0,0,.18), 0 6px 14px rgba(0,0,0,.22), 0 18px 40px rgba(0,0,0,.28)",
background: "#1b1b1d",
}}
>
<img src={c.src} alt={c.alt ?? ""} draggable={false} className="pointer-events-none h-full w-full object-cover" />
{/* glossy sheen */}
<div
ref={(el) => {
sheens.current[i] = el;
}}
className="pointer-events-none absolute inset-0"
style={{
backgroundImage: "linear-gradient(115deg, rgba(255,255,255,0) 30%, rgba(255,255,255,.16) 46%, rgba(255,255,255,.04) 54%, rgba(255,255,255,0) 64%)",
backgroundSize: "260% 100%",
mixBlendMode: "screen",
}}
/>
</div>
{c.label && (
<div
ref={(el) => {
tags.current[i] = el;
}}
aria-hidden="true"
className="pointer-events-none absolute -top-9 left-2 whitespace-nowrap rounded-full bg-white px-3 py-1 text-[11px] font-medium text-black opacity-0"
style={{ boxShadow: "0 2px 8px rgba(0,0,0,.25)" }}
>
{numbered ? `${String(i + 1).padStart(2, "0")} ` : ""}
{c.label}
</div>
)}
</div>
))}
</div>
</div>
);
}
export default FanStack;
One prompt, any AI agent
Paste into Claude Code, Codex, Cursor or Windsurf. It carries your tweaks and the full source, so the agent installs it exactly.
Props
8 propscards{ src; label?; alt? }[]The cards: an image and an optional label.widthnumberCard width, px.default 260default 260aspectnumberWidth / height.default 1.3default 1.3spreadnumberHow far the fan spreads (card widths per card).default 0.58default 0.58turnnumberDegrees each card turns when fanned.default 30default 30numberedbooleanNumber the labels (01, 02...).default truedefault trueonSelect(card, index) => voidCalled when a card is clicked (it also comes to the front).classNamestringExtra classes.