Number ticker
Stats count up from zero with a soft ease out the first time they scroll into view.
It imports two small hooks from @/lib/motion and the colour tokens; the zip includes both.
Demo.tsx
"use client";
import { useEffect, useRef } from "react";
import { useInView, usePrefersReducedMotion } from "@/lib/motion";
import styles from "./demo.module.css";
const STATS = [
{ value: 1284, label: "nights booked in October", format: new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }) },
{ value: 4.9, label: "average guest rating", format: new Intl.NumberFormat("en-US", { minimumFractionDigits: 1, maximumFractionDigits: 1 }) },
{ value: 0.96, label: "occupancy in high season", format: new Intl.NumberFormat("en-US", { style: "percent" }) },
];
const DURATION = 1800;
const STAGGER = 150;
export default function Demo() {
const root = useRef<HTMLDivElement>(null);
const numbers = useRef<(HTMLSpanElement | null)[]>([]);
const played = useRef(false);
const inView = useInView(root);
const reduced = usePrefersReducedMotion();
useEffect(() => {
if (!inView || reduced || played.current) return;
played.current = true;
// Elements are read once here: a frame can still run after unmount, when refs are already null.
const els = [...numbers.current];
const write = (i: number, progress: number) => {
const eased = 1 - Math.pow(1 - progress, 4);
els[i]!.textContent = STATS[i].format.format(STATS[i].value * eased);
};
STATS.forEach((_, i) => write(i, 0));
const start = performance.now();
let raf = 0;
const frame = (now: number) => {
let running = false;
STATS.forEach((_, i) => {
const t = Math.min(1, Math.max(0, (now - start - i * STAGGER) / DURATION));
write(i, t);
if (t < 1) running = true;
});
if (running) raf = requestAnimationFrame(frame);
};
raf = requestAnimationFrame(frame);
return () => {
cancelAnimationFrame(raf);
STATS.forEach((_, i) => write(i, 1));
};
}, [inView, reduced]);
return (
<div ref={root} className={styles.root}>
<dl className={styles.stats}>
{STATS.map((stat, i) => (
<div key={stat.label} className={styles.stat}>
<dt className={styles.label}>{stat.label}</dt>
<dd className={styles.value}>
<span className={styles.sr}>{stat.format.format(stat.value)}</span>
<span aria-hidden="true" ref={(el) => { numbers.current[i] = el; }}>{stat.format.format(stat.value)}</span>
</dd>
</div>
))}
</dl>
</div>
);
}
demo.module.css
.root { container-type: size; width: 100%; height: 100%; display: grid; place-items: center; padding: 16px; }
.stats { margin: 0; width: min(100%, 760px); display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border-radius: 16px; background: var(--white); border: 1px solid var(--line); }
.stat { display: flex; flex-direction: column-reverse; justify-content: flex-end; gap: 6px; padding: clamp(14px, 5cqi, 32px) clamp(10px, 3.5cqi, 28px); }
.stat + .stat { border-left: 1px solid var(--line); }
.value { margin: 0; font-size: clamp(24px, min(8cqi, 15cqh), 60px); font-weight: 700; letter-spacing: -0.03em; line-height: 1; font-variant-numeric: tabular-nums; }
.label { font-size: clamp(12px, 2.8cqi, 15px); line-height: 1.35; color: var(--muted); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
Use it for
- A row of two to four proof points on a landing page
- Dashboard totals on first load
Avoid
- Numbers that update often, use a rolling digit or a plain swap instead
- Prices and anything the user compares, the moving value delays reading
How it works
An IntersectionObserver starts a requestAnimationFrame loop once. Each frame computes progress per stat with a 150ms stagger, eases it with 1 minus (1 minus t) to the fourth power and writes the formatted value through a ref. Intl.NumberFormat handles separators, decimals and percent, and tabular-nums keeps the digits from shifting while they count. The real values are in the HTML, so no JavaScript means no zeros.
Accessibility
A visually hidden copy carries the final value and the counting span is aria-hidden, so screen readers never hear intermediate numbers. Reduced motion skips the count and shows the final values. Leaving mid count snaps to the final numbers.