Number 78: Countdown that turns live
Specification
- Catalogue
- No. 78 of 110
- Section
- Charts and numbers
- Interaction
- Text Animation
- Element
- Counter
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2, framer-motion 12.40.0
- Added
Notes
Days, hours, minutes and seconds to an event, with rolling digits, and a list of its sessions with their own short countdowns. At zero the clock gives way to the live state.
Workshops, launches, webinars and open days. The ticking shows a state that ends, and the sessions below say what happens next without a second clock to read.
Digits roll on tabular figures (NumberFlow), so the row never changes width. At zero the clock shrinks to 0.96 into a 4px blur over 200ms and the live state settles in from 0.94 and a 6px blur over 400ms cubic-bezier(.16, 1, .3, 1); each session switches to Now on its own time. The value is fixed on the server and in a resting card, the clock starts only after the page loads, and it stops for good at zero. Skip to the last seconds shows the change without waiting. The clock is a timer for screen readers and the live state is announced. Crossfades only under reduced motion.
The event, its date and sessions are sample content, and the target time is set from when the page loads.
Code
React and plain CSS"use client";
import { useEffect, useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { EASE_ENTER } from "@/components/pieces/kit/physical-spring";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import { Num } from "@/components/pieces/shared";
import { useStageMode } from "@/components/pieces/stage/mode";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./countdown-live.css";
/** Time left when the page loads: 2 days, 4 hours, 17 minutes, 30 seconds. The same on the server and in the first render. */
const START_LEFT = ((2 * 24 + 4) * 60 + 17) * 60 + 30;
const SESSIONS = [
{ name: "Wheel taster", after: 0 },
{ name: "Glaze talk", after: 45 * 60 },
{ name: "Kiln opening", after: 2 * 60 * 60 },
];
const TWO = { minimumIntegerDigits: 2 } as const;
function parts(secs: number) {
const s = Math.max(0, secs);
return { d: Math.floor(s / 86400), h: Math.floor((s % 86400) / 3600), m: Math.floor((s % 3600) / 60), s: Math.floor(s % 60) };
}
function short(secs: number): string {
if (secs <= 0) return "Now on";
const { d, h, m, s } = parts(secs);
if (d) return `in ${d}d ${h}h`;
if (h) return `in ${h}h ${m}m`;
if (m) return `in ${m}m ${s}s`;
return `in ${s}s`;
}
function CountdownDemo() {
const reduce = useSafeReducedMotion();
const { still, context } = useStageMode();
const [left, setLeft] = useState(START_LEFT);
const [endsAt, setEndsAt] = useState<number | null>(null);
const live = left <= 0;
// The clock starts after mount, never in a resting card, and stops for good at zero.
useEffect(() => {
if (still || live) return;
const end = endsAt ?? Date.now() + START_LEFT * 1000;
const tick = () => setLeft(Math.max(0, Math.ceil((end - Date.now()) / 1000)));
const id = window.setInterval(tick, 250);
return () => window.clearInterval(id);
}, [still, live, endsAt]);
const skipTo = context === "card" ? 3 : 10;
const skip = () => {
setEndsAt(Date.now() + skipTo * 1000);
setLeft(skipTo);
};
const { d, h, m, s } = parts(left);
const units = [
{ v: d, label: "days", fmt: undefined },
{ v: h, label: "hours", fmt: TWO },
{ v: m, label: "minutes", fmt: TWO },
{ v: s, label: "seconds", fmt: TWO },
];
const fade = reduce ? { duration: 0 } : { duration: 0.3, ease: EASE_ENTER };
return (
<div className="dm cl">
<div className="cl-head">
<p className="cl-event">Open studio evening</p>
<p className="cl-when">Friday 12 December, 6pm</p>
</div>
<div className="cl-stage">
<AnimatePresence mode="popLayout" initial={false}>
{!live ? (
<motion.div key="count" className="cl-count" role="timer" aria-label={`Starts in ${d} days, ${h} hours, ${m} minutes`} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96, filter: "blur(4px)", transition: { duration: 0.2 } }} transition={fade}>
{units.map((u) => (
<span key={u.label} className="cl-unit" aria-hidden="true">
<span className="cl-num">
<Num value={u.v} format={u.fmt} />
</span>
<span className="cl-label">{u.label}</span>
</span>
))}
</motion.div>
) : (
<motion.div key="live" className="cl-live" role="status" initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.94, filter: "blur(6px)" }} animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }} transition={reduce ? { duration: 0 } : { duration: 0.4, ease: EASE_ENTER, delay: 0.08 }}>
<span className="cl-on">Doors are open</span>
<span className="cl-way">Come in through the yard gate on Mill Lane</span>
</motion.div>
)}
</AnimatePresence>
</div>
<ul className="cl-sessions" aria-label="Sessions">
{SESSIONS.map((x) => {
const t = left + x.after;
return (
<li key={x.name} data-on={t <= 0 || undefined}>
<span>{x.name}</span>
<span className="cl-in">{short(t)}</span>
</li>
);
})}
</ul>
{!live && (
<button type="button" className="cl-skip" onClick={skip}>
Skip to the last {skipTo} seconds
</button>
)}
</div>
);
}
// What a hovered card shows: a jump to three seconds before the doors open, then the change to live.
const TOUR: Tour = [{ click: ".cl-skip", wait: 4800 }];
export default function CountdownLiveEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Countdown that turns live" tour={TOUR} sample>
<CountdownDemo />
</PieceStage>
);
}