Specification
- Catalogue
- No. 83 of 110
- Section
- Charts and numbers
- Interaction
- Text Animation
- Element
- Counter
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Each digit is a column of 0 to 9 inside a one-line window. Changing the value only changes a custom property, --d, and the column slides to translateY(calc(var(--d) * -1em)).
A small delay per column (45ms) makes the change ripple from left to right. A soft mask at the top and bottom of each window keeps the passing digits from looking cut off.
Screen readers get the formatted number once, in a visually hidden live region, while the columns are hidden from them.
Code
React and plain CSS"use client";
import { useEffect, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./rolling-counter.module.css";
const DEMO_VALUES = [12480, 12936, 13512, 12804, 14250];
const MIN = 10000;
const MAX = 99999;
const DIGITS = "0123456789".split("");
/** One column of 0 to 9 that slides to the current digit. */
function Digit({ value, index }: { value: number; index: number }) {
return (
<span className={styles.digit} style={{ "--d": value, "--i": index } as CSSProperties} aria-hidden="true">
<span className={styles.column}>
{DIGITS.map((d) => (
<span key={d}>{d}</span>
))}
</span>
</span>
);
}
export default function RollingCounter({ active, context }: LivePreviewProps) {
const [value, setValue] = useState(DEMO_VALUES[0] ?? MIN);
const [lastChange, setLastChange] = useState(1);
const [step, setStep] = useState(0);
const demo = active && context === "card";
// Each time the demo starts, it starts from the first step.
const [demoRunning, setDemoRunning] = useState(demo);
if (demoRunning !== demo) {
setDemoRunning(demo);
setStep(0);
}
useEffect(() => {
if (!demo) return;
let id = window.setTimeout(function tick() {
setStep((s) => s + 1);
id = window.setTimeout(tick, 1500);
}, 250);
return () => window.clearTimeout(id);
}, [demo]);
const shown = context === "card" ? (demo ? (DEMO_VALUES[step % DEMO_VALUES.length] ?? MIN) : (DEMO_VALUES[0] ?? MIN)) : value;
// Direction of the latest change. In the card demo it compares with the previous demo value.
const previous = demo && step > 0 ? (DEMO_VALUES[(step - 1) % DEMO_VALUES.length] ?? shown) : shown;
const up = context === "card" ? shown >= previous : lastChange >= 0;
const digits = String(shown).split("").map(Number);
const formatted = shown.toLocaleString("en-US");
const change = (delta: number) => {
setLastChange(delta);
setValue((v) => Math.min(MAX, Math.max(MIN, v + delta)));
};
return (
<div className={styles.root}>
<p className={styles.label}>Balance</p>
<p className={styles.amount}>
<span className="visually-hidden" aria-live="polite">
${formatted}
</span>
<span className={styles.currency} aria-hidden="true">
$
</span>
{digits.map((d, i) => (
<span key={i} className={styles.group}>
{i === digits.length - 3 && (
<span className={styles.comma} aria-hidden="true">
,
</span>
)}
<Digit value={d} index={i} />
</span>
))}
</p>
<p className={styles.delta} data-direction={up ? "up" : "down"} aria-hidden="true">
{up ? "Up" : "Down"} since last week
</p>
{context === "detail" && (
<div className={styles.controls}>
<button type="button" className={styles.control} onClick={() => change(-640)} aria-label="Decrease by 640">
-640
</button>
<button type="button" className={styles.control} onClick={() => change(1250)} aria-label="Increase by 1,250">
+1,250
</button>
</div>
)}
</div>
);
}