odometer roll.
A rolling number readout where only the columns that changed move, and every column rolls the way the value went.
live component, try it
what to expect.
how it works.
It comes down to three rules, and each one fixes a bug you only see once you look for it. The first is that direction belongs to the value, not to the digit. 1299 to 1301 runs the tens from 9 to 0, whose own shortest path is downward, which would put one column travelling backwards inside a number that went up, so every column takes the sign of the whole. The second is about identity rather than motion. Columns keyed by their position in the string are handed their neighbour's digit the moment a place is added or dropped, so 1234 losing its thousands would roll all three survivors to show digits that never moved. Keyed by place value, nothing moves that did not move. The third is a fixed advance per column, without which the readout shivers where it stands.
take it with you.
Three steps, and the stage above runs in your own project.
Install
Nothing to add. It runs on React alone, 18 or newer.
Copy the files
4 files, kept together in one folder,
odometer/. The styles travel with them."use client"; import { useEffect, useRef, useState, type CSSProperties } from "react"; import { useReducedMotion } from "./motion"; import { ROWS, applyRoll, columnsOf, departed, digitAt, directionFor, withDeparted, type Column, type Direction, } from "./roll"; import "./odometer.css"; interface Props { value: number; // ms each wheel takes to roll duration?: number; // ms of delay added per place, so the ones lead and the thousands follow stagger?: number; // fixed width digits, so the readout never shifts sideways tabular?: boolean; // thousands separators group?: boolean; className?: string; } export default function Odometer({ value, duration = 460, stagger = 35, tabular = true, group = true, className, }: Props) { const reduced = useReducedMotion(); const [shown, setShown] = useState(value); const [direction, setDirection] = useState<Direction>("up"); const [departing, setDeparting] = useState<Column[]>([]); if (value !== shown) { setShown(value); setDirection(directionFor(shown, value)); setDeparting(departed(columnsOf(shown, { group }), columnsOf(value, { group }))); } const rendered = withDeparted(columnsOf(value, { group }), departing); const seen = useRef<Set<string> | null>(null); const isNew = (key: string) => seen.current !== null && !seen.current.has(key); useEffect(() => { seen.current = new Set(rendered.map((r) => `${r.column.kind}${r.column.place}`)); }); useEffect(() => { if (departing.length === 0) return; const longest = Math.max(...departing.map((c) => c.place)); const wait = reduced ? 0 : duration + stagger * longest; const id = window.setTimeout(() => setDeparting([]), wait); return () => window.clearTimeout(id); }, [departing, duration, stagger, reduced]); return ( <span className={["od-readout", tabular && "od-fixed", className].filter(Boolean).join(" ")}> <span className="od-wheels" style={ { "--roll": `${reduced ? 0 : duration}ms`, "--stagger": `${reduced ? 0 : stagger}ms`, } as CSSProperties } aria-hidden="true" > {rendered.map(({ column, leaving }) => column.kind === "sep" ? ( <Sep key={`s${column.place}`} char={column.char} place={column.place} leaving={leaving} entering={isNew(`sep${column.place}`)} /> ) : ( <Digit key={`d${column.place}`} digit={leaving ? 0 : column.digit} place={column.place} direction={direction} leaving={leaving} entering={isNew(`digit${column.place}`)} /> ), )} </span> <span className="od-sr" role="status"> {value.toLocaleString("en-US")} </span> </span> ); } function useEnter(entering: boolean) { const ref = useRef<HTMLSpanElement>(null); useEffect(() => { if (!entering) return; const el = ref.current; if (!el) return; const id = requestAnimationFrame(() => el.removeAttribute("data-enter")); return () => cancelAnimationFrame(id); }, [entering]); return ref; } function Sep({ char, place, leaving, entering, }: { char: string; place: number; leaving: boolean; entering: boolean; }) { return ( <span className="od-sep" ref={useEnter(entering)} data-leaving={leaving || undefined} data-enter={entering || undefined} style={{ "--place": place } as CSSProperties} > {char} </span> ); } function Digit({ digit, place, direction, leaving, entering, }: { digit: number; place: number; direction: Direction; leaving: boolean; entering: boolean; }) { const column = useEnter(entering); const strip = useRef<HTMLSpanElement>(null); const row = useRef(entering ? 0 : digit); useEffect(() => { const el = strip.current; if (!el) return; row.current = applyRoll(el, row.current, digit, direction); }, [digit, direction]); return ( <span className="od-col" ref={column} data-enter={entering || undefined} data-leaving={leaving || undefined} style={{ "--place": place } as CSSProperties} > <span className="od-strip" ref={strip} style={{ "--row": entering ? 0 : digit } as CSSProperties} > {Array.from({ length: ROWS }, (_, i) => ( <span key={i}>{digitAt(i)}</span> ))} </span> </span> ); }Use it
The stage above, exactly as it runs here. The comments mark what to change.
"use client"; import { useState } from "react"; import Odometer from "./odometer/Odometer"; const JUMPS = [-1000, 9000, -8000]; export default function OdometerExample({ start = 1298 }: { start?: number }) { const [value, setValue] = useState(start); const [delta, setDelta] = useState(0); const [jump, setJump] = useState(0); const step = (by: number) => { const next = Math.max(0, value + by); setDelta(next - value); setValue(next); }; return ( <div className="od-lab"> <Odometer value={value} duration={460} stagger={35} /> <p className="od-delta" aria-hidden="true" key={value} data-dir={delta < 0 ? "down" : "up"}> {delta === 0 ? " " : `${delta > 0 ? "+" : "−"}${Math.abs(delta).toLocaleString("en-US")}`} </p> <div className="od-keys"> <button type="button" className="od-key" onClick={() => step(-1)}> <span aria-hidden="true">−1</span> <span className="od-sr">take one away</span> </button> <button type="button" className="od-key" onClick={() => step(1)}> <span aria-hidden="true">+1</span> <span className="od-sr">add one</span> </button> <button type="button" className="od-key od-key--wide" onClick={() => { step(JUMPS[jump % JUMPS.length]!); setJump(jump + 1); }} > jump </button> </div> </div> ); }