Number 98: Card stack that deals the next picture
Specification
- Catalogue
- No. 98 of 110
- Section
- Media and galleries
- Interaction
- Press, Layout Animation
- Element
- Media
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Four pictures stacked in depth, each one further back sitting up and to the right, the whole stack turned a little away. Press it and the front picture dips toward you, then goes to the back while the others step forward.
The dip is what makes it read as dealing rather than shuffling: for 220ms the front card drops and tilts on its own, and only then does the order change, so the card visibly leaves before it reappears at the back. Each place in the stack is one transform built from its index (--i), so moving a card is a change of number and the transition does the rest. The name pill belongs to the front card and fades in after it lands.
The pictures are the site's own sample drawings.
Code
React and plain CSS"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { useReducedMotion } from "@/lib/client/hooks";
import styles from "./card-swap.module.css";
const PICTURES = [
{ src: "/samples/studio-vases.svg", label: "Vases" },
{ src: "/samples/studio-flowers.svg", label: "Flowers" },
{ src: "/samples/kitchen-oak.svg", label: "Oak kitchen" },
{ src: "/samples/studio-bowls.svg", label: "Bowls" },
];
/** How long the front card dips before it goes to the back. */
const DIP_MS = 220;
const DEMO_EVERY = 1500;
const INITIAL = PICTURES.map((_, i) => i);
export default function CardSwap({ active, context }: LivePreviewProps) {
const card = context === "card";
const reduced = useReducedMotion();
const [order, setOrder] = useState(INITIAL);
const [dipping, setDipping] = useState(false);
const timer = useRef(0);
const swap = () => {
if (dipping) return;
if (reduced) {
setOrder((o) => [...o.slice(1), o[0] ?? 0]);
return;
}
setDipping(true);
timer.current = window.setTimeout(() => {
setOrder((o) => [...o.slice(1), o[0] ?? 0]);
setDipping(false);
}, DIP_MS);
};
useEffect(() => () => window.clearTimeout(timer.current), []);
// On a hovered card the stack deals itself, starting again from the first picture each time.
const demo = card && active;
const [dealing, setDealing] = useState(demo);
if (dealing !== demo) {
setDealing(demo);
setOrder(INITIAL);
setDipping(false);
}
useEffect(() => {
if (!demo) return;
const id = window.setInterval(() => {
setDipping(true);
timer.current = window.setTimeout(() => {
setOrder((o) => [...o.slice(1), o[0] ?? 0]);
setDipping(false);
}, DIP_MS);
}, DEMO_EVERY);
return () => {
window.clearInterval(id);
window.clearTimeout(timer.current);
};
}, [demo]);
const front = PICTURES[order[0] ?? 0];
return (
<div className={styles.area}>
<button
type="button"
className={styles.stack}
tabIndex={card ? -1 : 0}
aria-label={`${front?.label ?? "Picture"}. Show the next picture`}
onClick={swap}
>
{PICTURES.map((picture, id) => {
const place = order.indexOf(id);
const style = { "--i": place, zIndex: PICTURES.length - place } as CSSProperties;
return (
<span key={picture.src} className={styles.card} style={style} data-front={place === 0 || undefined} data-dip={(place === 0 && dipping) || undefined}>
<img src={picture.src} alt="" className={styles.picture} draggable={false} />
<span className={styles.label} aria-hidden="true">
{picture.label}
</span>
</span>
);
})}
</button>
</div>
);
}