Number 21: Section rail for long pages
Specification
- Catalogue
- No. 21 of 110
- Section
- Navigation
- Interaction
- Scroll
- Element
- Navigation
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
A tick for each heading at the edge of a long page shows where the reader is and jumps to any section.
Long guides and policies become easy to move around: the reader sees how far they are, and the name of each section appears on the tick. On a phone it becomes a Contents button with a progress line.
Active tick widens from 12px to 26px, 250ms; section names show on hover and focus in 150ms. Links are real anchors; the jump scrolls smoothly, instantly under reduced motion, and focuses the heading. Under 480px: a Contents button with the current section and a progress line. Here the guide scrolls inside its own frame; on a full page the anchors scroll the window.
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, useId, useRef, useState, type MouseEvent } from "react";
import { reducedNow } 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-index.css";
const GUIDE = [
{ id: "before", title: "Before you sell", body: "Gather the purchase completion statement, the costs of any improvements and the sale figures. Most of the work later depends on these papers." },
{ id: "cgt", title: "Capital gains tax", body: "Tax is due on the gain: the sale price less the purchase price and allowable costs. Your rate depends on your income in the year of the sale." },
{ id: "sixty", title: "Reporting in 60 days", body: "A UK residential property sale with tax to pay is reported, and the tax paid, within 60 days of completion, separately from your tax return." },
{ id: "costs", title: "Costs you can deduct", body: "Estate agent and solicitor fees count, and so do improvements such as an extension. Repairs and redecoration usually do not." },
{ id: "joint", title: "Joint owners", body: "Each owner reports their own share of the gain and uses their own allowance." },
{ id: "call", title: "When to call us", body: "Before you accept an offer is best: there is still time to plan the date of completion." },
];
function ScrollIndexDemo() {
const frame = useRef<HTMLDivElement>(null);
const [active, setActive] = useState(0);
const [progress, setProgress] = useState(0);
const [menu, setMenu] = useState(false);
const menuBtn = useRef<HTMLButtonElement>(null);
const uid = useId();
useEffect(() => {
const f = frame.current;
if (!f) return;
let raf = 0;
const read = () => {
raf = 0;
const heads = GUIDE.map((g) => f.querySelector<HTMLElement>(`#${CSS.escape(`${uid}-${g.id}`)}`));
const y = f.scrollTop + 48;
let a = 0;
heads.forEach((h, i) => {
if (h && h.offsetTop <= y) a = i;
});
const max = f.scrollHeight - f.clientHeight;
if (max > 0 && f.scrollTop >= max - 2) a = GUIDE.length - 1;
setActive(a);
setProgress(max > 0 ? f.scrollTop / max : 0);
};
const onScroll = () => {
if (!raf) raf = requestAnimationFrame(read);
};
read();
f.addEventListener("scroll", onScroll, { passive: true });
return () => {
f.removeEventListener("scroll", onScroll);
if (raf) cancelAnimationFrame(raf);
};
}, [uid]);
// Here the guide scrolls inside its frame; on a full page these are plain anchor links.
const jump = (e: MouseEvent<HTMLAnchorElement>, id: string) => {
e.preventDefault();
const f = frame.current;
const h = f?.querySelector<HTMLElement>(`#${CSS.escape(`${uid}-${id}`)}`);
if (!f || !h) return;
f.scrollTo({ top: h.offsetTop - 12, behavior: reducedNow() ? "auto" : "smooth" });
h.focus({ preventScroll: true });
setMenu(false);
};
return (
<div className="dm dm-scroll">
<div className="si">
<div
className="si-top"
onKeyDown={(e) => {
if (e.key === "Escape" && menu) {
e.stopPropagation();
setMenu(false);
menuBtn.current?.focus();
}
}}
>
<button ref={menuBtn} type="button" className="dm-btn si-menu" aria-expanded={menu} aria-controls={`${uid}-toc`} onClick={() => setMenu((m) => !m)}>
Contents: {GUIDE[active]?.title}
</button>
<span className="si-progress" aria-hidden="true">
<i style={{ scale: `${progress} 1` }} />
</span>
{menu && (
<ol className="si-toc" id={`${uid}-toc`}>
{GUIDE.map((g, i) => (
<li key={g.id}>
<a href={`#${uid}-${g.id}`} aria-current={i === active ? "location" : undefined} onClick={(e) => jump(e, g.id)}>
{g.title}
</a>
</li>
))}
</ol>
)}
</div>
<div ref={frame} className="sf si-frame" tabIndex={0} role="region" aria-label="Sample guide. Scroll inside this frame">
<article className="si-guide">
<h2>Selling a rental property</h2>
{GUIDE.map((g) => (
<section key={g.id}>
<h3 id={`${uid}-${g.id}`} tabIndex={-1}>
{g.title}
</h3>
<p>{g.body}</p>
</section>
))}
</article>
</div>
<nav className="si-rail" aria-label="Sections">
<ol>
{GUIDE.map((g, i) => (
<li key={g.id}>
<a href={`#${uid}-${g.id}`} aria-current={i === active ? "location" : undefined} onClick={(e) => jump(e, g.id)}>
<span className="si-label">{g.title}</span>
<i aria-hidden="true" />
</a>
</li>
))}
</ol>
</nav>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ scroll: ".si-frame", to: 1, ms: 3600, wait: 1000 },
{ scroll: ".si-frame", to: 0, ms: 900, wait: 400 },
];
export default function ScrollIndexEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Section rail for long pages" sample tour={TOUR}>
<ScrollIndexDemo />
</PieceStage>
);
}