Number 60: Cards that stack as you scroll
Specification
- Catalogue
- No. 60 of 110
- Section
- Cards and accordions
- Interaction
- Scroll
- Element
- Card
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Each stage of a process pins at the top and the next one slides over it, leaving the edges in view.
A real sequence reads as a pile that grows: the reader sees how many stages came before. CSS sticky does the stacking, so the scroll stays the visitor's own.
position: sticky with tops 12px apart; each covered card scales to 0.965 per card above it, 250ms. Each card gets 34% of the frame's height of scrolling before the next arrives. Changed from the library: CSS sticky instead of a scroll library, no blur on the cards below. Here it scrolls inside its own frame; on a full page use screen heights instead.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import { layoutScale } from "@/components/pieces/motion-helpers";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/styles/scroll-frame.css";
import "./scroll-stack.css";
function Frame({ label, frame, children, hint }: { label: string; frame: RefObject<HTMLDivElement | null>; children: ReactNode; hint: string }) {
return (
<div className="dm dm-scroll">
<div ref={frame} className="sf" tabIndex={0} role="region" aria-label={`${label}. Scroll inside this frame`}>
{children}
</div>
<p className="dm-hint">{hint}</p>
</div>
);
}
const WEEKS = [
{ when: "Week 1", title: "Assessment", body: "How you move, what hurts, what you want from the next three months." },
{ when: "Weeks 2 to 4", title: "Foundations", body: "Three short sessions a week, technique before weight." },
{ when: "Weeks 5 to 8", title: "Build", body: "Heavier, fewer, with a plan you can follow on your own." },
{ when: "Week 9", title: "Review", body: "We compare with week one and set the next block together." },
];
const STACK_TOP = 14;
const STACK_STEP = 12;
function ScrollStackDemo() {
const frame = useRef<HTMLDivElement>(null);
const cards = useRef<(HTMLLIElement | null)[]>([]);
const reduced = useSafeReducedMotion();
const [depth, setDepth] = useState<number[]>(() => WEEKS.map(() => 0));
useEffect(() => {
const f = frame.current;
if (!f || reduced) return;
let raf = 0;
const measure = () => {
raf = 0;
const top = f.getBoundingClientRect().top;
const s = layoutScale(f);
const stuck = cards.current.map((c, i) => (c ? (c.getBoundingClientRect().top - top) / s <= STACK_TOP + i * STACK_STEP + 1 : false));
setDepth(WEEKS.map((_, i) => stuck.slice(i + 1).filter(Boolean).length));
};
const onScroll = () => {
if (!raf) raf = requestAnimationFrame(measure);
};
measure();
f.addEventListener("scroll", onScroll, { passive: true });
return () => {
f.removeEventListener("scroll", onScroll);
if (raf) cancelAnimationFrame(raf);
};
}, [reduced]);
return (
<Frame label="A coaching programme in four stages" frame={frame} hint="Scroll inside: each stage stacks on the last">
<div className="stk">
<p className="ss-intro">Your first nine weeks</p>
<ol>
{WEEKS.map((w, i) => (
<li
key={w.title}
ref={(el) => {
cards.current[i] = el;
}}
className="stk-card"
style={{ top: STACK_TOP + i * STACK_STEP, scale: String(1 - (depth[i] ?? 0) * 0.035) }}
>
<span className="stk-when">{w.when}</span>
<h3>{w.title}</h3>
<p>{w.body}</p>
</li>
))}
</ol>
<p className="ss-outro">Then the page carries on.</p>
</div>
</Frame>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ scroll: ".sf", to: 1, ms: 3600, wait: 1000 },
{ scroll: ".sf", to: 0, ms: 900, wait: 400 },
];
export default function ScrollStackEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Cards that stack as you scroll" sample tour={TOUR}>
<ScrollStackDemo />
</PieceStage>
);
}