Animated number
A number that rolls to its value and lands without bouncing. Use it for headline figures that change while the reader is looking: a total, a balance, a live count. While it rolls it keeps the width of the wider of the old and the new number, so it never runs over the text beside it.
Install
npx shadcn@latest add https://thrumui.dev/r/animated-number.jsonAdds components/ui/animated-number.tsx and installs motion. Nothing else in your project changes.
import { AnimatedNumber } from "@/components/ui/animated-number";Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | Required | The number to show. Changing it rolls from the number on screen to the new one. |
from | number | – | Where the first roll starts, usually 0. When set, the number rolls up the first time it scrolls into view. When left out, it simply appears and only rolls on change. |
format | Intl.NumberFormatOptions | Default compact | How the number is written. Plain data, so a server component can pass it. |
locale | string | Default "en-US" | Locale for number formatting. |
sound | boolean | Default true | Play a soft tone when a roll lands, if the visitor has sound on. |
className | string | – | Sets size and weight. Digits are always tabular. |
Motion and sound
Motion
- A roll takes about 400 ms on a spring with no overshoot: the number never passes its value and comes back.
- A new value during a roll continues from the number on screen. It does not restart.
- Each frame is written straight to the page. React does not re-render while the number rolls.
- Reduced motion: the new value appears at once.
Sound
- A short tone when a roll to a new value lands. Several numbers landing together play it once.
- The first roll, when the number scrolls into view, is silent.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- Screen readers get the real value as ordinary text. The rolling digits are hidden from them.
- It is not a live region: a number that changes many times a second must not be read out. Announce important changes yourself, in words.
- While it rolls the element is as wide as the wider of the old and the new number, so it never overlaps what stands beside it. It takes the new number's width when the roll lands.
Source
"use client";
import { useEffect, useRef, useState } from "react";
import { animate, useInView, useMotionValue, useMotionValueEvent } from "motion/react";
import { cn } from "@/lib/utils";
import { formatValue } from "@/lib/thrum-chart";
import { SPRING, useReducedMotion } from "@/lib/thrum-motion";
import { play } from "@/lib/thrum-sound";
export type AnimatedNumberProps = {
/** The number to show. Changing it rolls from the number on screen to the new one. */
value: number;
/**
* Where the first roll starts, usually 0. When set, the number rolls up to `value` the first time
* it scrolls into view. When left out, the number simply appears and only rolls when `value` changes.
*/
from?: number;
/** How the number is written. Example: `{ style: "currency", currency: "USD" }`. */
format?: Intl.NumberFormatOptions;
locale?: string;
/** Play a soft tone when a roll lands, if the visitor has sound on. */
sound?: boolean;
className?: string;
};
/**
* A number that rolls to its value like a meter needle: quick, and it never overshoots.
* While it rolls the element is as wide as the wider of the old and the new number, so it never runs over its neighbour.
*/
export function AnimatedNumber({ value, from, format, locale, sound = true, className }: AnimatedNumberProps) {
const root = useRef<HTMLSpanElement>(null);
const digits = useRef<HTMLSpanElement>(null);
const held = useRef<HTMLSpanElement>(null);
const reduced = useReducedMotion();
const inView = useInView(root, { once: true });
const current = useMotionValue(from ?? value);
const say = (v: number) => formatValue(v, format, locale);
const formatKey = (locale ?? "") + JSON.stringify(format ?? null);
// Render the real value on SSR and Frame 0 so initial paint and reduced motion never flash a placeholder zero.
const [firstText] = useState(() => say(value));
// One DOM write per frame. No React render per frame.
useMotionValueEvent(current, "change", (v) => {
if (digits.current) digits.current.textContent = say(v);
});
// The first roll, up from `from`, is the entrance, and nothing plays on entrance. Later rolls are news.
const entered = useRef(from === undefined);
useEffect(() => {
if (held.current) held.current.textContent = "";
if (current.get() === value) {
if (digits.current) digits.current.textContent = say(value);
return;
}
// With reduced motion there is no roll to wait for: the value is there at once, on screen or not.
if (reduced) {
current.set(value);
if (digits.current) digits.current.textContent = say(value);
entered.current = true;
return;
}
if (!inView) {
if (digits.current) digits.current.textContent = say(value);
return;
}
if (digits.current) digits.current.textContent = say(current.get());
// Until the roll lands the box is as wide as the wider of the number on screen and the new one,
// so a number that gets shorter never runs over what stands beside it.
// Reserves width for the start and end values only; compact formats that briefly widen mid-roll (e.g. "999.9K" between "950K" and "1M") can set a min-width on the caller.
if (held.current) held.current.textContent = say(current.get());
const delta = Math.abs(value - current.get());
const controls = animate(current, value, {
...SPRING.data,
restDelta: Math.min(0.01, delta * 1e-4 || 0.01),
onComplete: () => {
current.set(value);
if (digits.current) digits.current.textContent = say(value);
if (held.current) held.current.textContent = "";
if (sound && entered.current) play("countDone");
entered.current = true;
},
});
// Stopping keeps the number where it is, so an interrupted roll continues from there.
return () => controls.stop();
// `say` changes only when `formatKey` does.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value, inView, reduced, sound, formatKey]);
return (
<span ref={root} data-slot="animated-number" className={cn("relative inline-block whitespace-nowrap tabular-nums", className)}>
{/* Screen readers get the real value once. It is not a live region: a rolling number must not be read out. */}
<span className="sr-only">{say(value)}</span>
{/* Invisible copies hold the width: the final number, and during a roll the number it started from, in the same cell. */}
<span aria-hidden="true" className="invisible inline-grid">
<span className="[grid-area:1/1]">{say(value)}</span>
<span ref={held} className="[grid-area:1/1]" />
</span>
{/* The rolling number, laid over that space. It follows the surrounding text alignment. */}
<span ref={digits} aria-hidden="true" className="absolute inset-0" suppressHydrationWarning>
{firstText}
</span>
</span>
);
}Files and dependencies
- Adds
components/ui/animated-number.tsx- Packages
motion- Brings along
- Chart foundationsMotion tokensSound engine
Changelog
- 1.0.0 · 2026-12-02First release.