Number 8: Feature tour with visible timers
Specification
- Catalogue
- No. 8 of 110
- Section
- Media and galleries
- Interaction
- Loading
- Element
- Carousel
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
Each tab under the slide fills like a progress bar until the next one, so the autoplay is never a surprise.
The visitor sees how long each slide stays and can pick any tab. It pauses on hover, focus or off screen, has a pause button, and stops after one round on the first slide.
5s per slide; the tab's bar fills with the time. Pauses on hover, focus, off screen and in a hidden tab; a pause button; still under reduced motion. One round, then rests on the first slide; picking a tab ends the autoplay. Changed from the library: pause and stop added, real tablist.
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 CSSProperties } from "react";
import { tabKey } from "@/components/pieces/shared";
import { useOnScreen } from "@/components/pieces/motion-helpers";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "./progressive-carousel.css";
const TOUR = [
{ id: "book", tab: "Book online", title: "Clients book their own appointments", lines: ["Tue 2 Dec", "10:15 Cut and finish", "11:30 Colour"] },
{ id: "remind", tab: "Reminders", title: "A reminder goes out the day before", lines: ["Reminder: 10:15 tomorrow", "Reply C to cancel"] },
{ id: "pay", tab: "Pay on the day", title: "Card or contactless at the chair", lines: ["Cut and finish £48", "Paid by card"] },
];
const SLIDE_MS = 5000;
function ProgressiveCarouselDemo() {
const root = useRef<HTMLDivElement>(null);
const onScreen = useOnScreen(root);
const reduced = useSafeReducedMotion();
const [i, setI] = useState(0);
const [elapsed, setElapsed] = useState(0);
const [auto, setAuto] = useState(true);
const [paused, setPaused] = useState(false);
// Pointer over it and focus inside it are tracked apart: either one holds the tour.
const [hovered, setHovered] = useState(false);
const [focusIn, setFocusIn] = useState(false);
const uid = useId();
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const running = auto && !paused && !hovered && !focusIn && onScreen && !reduced;
useEffect(() => {
if (!running) return;
let raf = 0;
let last = performance.now();
const tick = (now: number) => {
const dt = now - last;
last = now;
setElapsed((e) => e + dt);
raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf);
}, [running]);
// One round, then rest on the first slide. Adjusted during render, so the
// next slide shows in the same frame the bar fills.
if (elapsed >= SLIDE_MS) {
setElapsed(0);
if (i === TOUR.length - 1) {
setI(0);
setAuto(false);
} else setI(i + 1);
}
const pick = (k: number) => {
setI(k);
setElapsed(0);
setAuto(false);
};
const s = TOUR[i]!;
return (
<div
className="dm"
ref={root}
onPointerEnter={(e) => e.pointerType === "mouse" && setHovered(true)}
onPointerLeave={() => setHovered(false)}
onFocus={() => setFocusIn(true)}
onBlur={(e) => !e.currentTarget.contains(e.relatedTarget as Node | null) && setFocusIn(false)}
>
<div className="pc">
<div className="pc-slide" id={`${uid}-p`} role="tabpanel" aria-labelledby={`${uid}-t${i}`}>
<div key={s.id} className="pc-body">
<h3>{s.title}</h3>
<div className="pc-mock" aria-hidden="true">
{s.lines.map((l, k) => (
<span key={l} data-first={k === 0 || undefined}>
{l}
</span>
))}
</div>
</div>
</div>
<div className="pc-tabs" role="tablist" aria-label="Tour">
{TOUR.map((t, k) => (
<button
key={t.id}
ref={(el) => {
tabs.current[k] = el;
}}
id={`${uid}-t${k}`}
type="button"
role="tab"
aria-selected={k === i}
aria-controls={`${uid}-p`}
tabIndex={k === i ? 0 : -1}
className="pc-tab"
onClick={() => pick(k)}
onKeyDown={(e) => {
const n = tabKey(e.key, k, TOUR.length);
if (n === null) return;
e.preventDefault();
pick(n);
tabs.current[n]?.focus();
}}
>
<span className="pc-fill" aria-hidden="true" style={{ "--f": k < i && auto ? 1 : k === i ? (auto ? elapsed / SLIDE_MS : 1) : 0 } as CSSProperties} />
{t.tab}
</button>
))}
</div>
<div className="pc-foot">
{reduced ? (
// Reduced motion: the tour never plays by itself, so there is nothing to pause.
<span className="dm-small">Pick any step.</span>
) : auto ? (
<button type="button" className="pc-pause" onClick={() => setPaused((p) => !p)} aria-pressed={paused}>
{paused ? "Play the tour" : "Pause the tour"}
</button>
) : (
<span className="dm-small">Tour finished. Pick any step.</span>
)}
</div>
</div>
</div>
);
}
export default function ProgressiveCarouselEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Feature tour with visible timers" sample replay>
<ProgressiveCarouselDemo />
</PieceStage>
);
}