Skip to content

Animated Number

Animated number counter: odometer digits roll to each new value, with Intl locale and currency formatting and a gain or loss flash.

  • Text Effects
  • Fintech / Crypto
  • React only · 0 deps
  • 1 file
Full screen

Browser support · All evergreen browsers. Reduced motion swaps digits without rolling.

Install. One command, or copy the file.

$npx shadcn@latest add https://freedesignmd.com/r/animated-number.json
  • Byte-identical to the preview
  • No Tailwind or config needed
  • SSR-safe · keyboard accessible
  • Respects reduced motion & transparency
components/text/animated-number.tsx
1/**
2 * AnimatedNumber, an odometer. Each digit is its own drum that rolls to the
3 * new value; rises roll upward and falls roll downward (wrapping through 0
4 * the way a real counter does). Formatting is Intl.NumberFormat, so locale
5 * separators, currencies, percents and compact notation all just work.
6 * On change the number flashes green (up) or red (down).
7 *
8 * Props
9 * - value: number
10 * - locale?: string Default "en-US".
11 * - format?: Intl.NumberFormatOptions e.g. { style: "currency", currency: "EUR" }.
12 * - duration?: number Roll duration in ms. Default 700.
13 * - flash?: boolean Colour flash on change. Default true.
14 * - signColor?: boolean Keep text green/red by sign (for deltas). Default false.
15 * - className / style Typography is yours; use tabular figures.
16 * CSS vars: --fd-an-up (#16A34A), --fd-an-down (#DC2626), --fd-an-dur.
17 *
18 * Accessibility: the formatted value is real text in a visually hidden span;
19 * the drums are aria-hidden. Don't put aria-live on fast tickers, announce
20 * summaries instead (see the Demo).
21 * Reduced motion: digits swap without rolling; the flash is a short fade.
22 * Browser support: all evergreen browsers.
23 */
24import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
25import type { CSSProperties } from "react";
26
27const useIso = typeof window === "undefined" ? useEffect : useLayoutEffect;
28const STRIP = Array.from({ length: 30 }, (_, i) => i % 10);
29
30function Digit({ d, dir, duration }: { d: number; dir: number; duration: number }) {
31 const strip = useRef<HTMLSpanElement>(null);
32 const pos = useRef(10 + d);
33 const timer = useRef(0);
34 useIso(() => {
35 const el = strip.current;
36 if (!el) return;
37 const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
38 const cur = pos.current % 10;
39 if (cur === d) return;
40 window.clearTimeout(timer.current);
41 if (reduced) {
42 el.style.transition = "none";
43 pos.current = 10 + d;
44 el.style.transform = `translate3d(0,${-pos.current}em,0)`;
45 return;
46 }
47 // normalise to the middle copy without animating, then roll in `dir`
48 el.style.transition = "none";
49 el.style.transform = `translate3d(0,${-(10 + cur)}em,0)`;
50 void el.offsetHeight;
51 let target = 10 + d;
52 if (dir > 0 && d <= cur) target = 20 + d;
53 if (dir < 0 && d >= cur) target = d;
54 el.style.transition = `transform ${duration}ms cubic-bezier(.22,1,.36,1)`;
55 el.style.transform = `translate3d(0,${-target}em,0)`;
56 pos.current = target;
57 timer.current = window.setTimeout(() => {
58 el.style.transition = "none";
59 pos.current = 10 + d;
60 el.style.transform = `translate3d(0,${-pos.current}em,0)`;
61 }, duration + 30);
62 }, [d, dir, duration]);
63 useEffect(() => () => window.clearTimeout(timer.current), []);
64 return (
65 <span className="fd-an-d">
66 <span className="fd-an-ghost">0</span>
67 <span className="fd-an-strip" ref={strip} style={{ transform: `translate3d(0,${-(10 + d)}em,0)` }}>
68 {STRIP.map((n, i) => (
69 <span key={i} className="fd-an-n">{n}</span>
70 ))}
71 </span>
72 </span>
73 );
74}
75
76export interface AnimatedNumberProps {
77 value: number;
78 locale?: string;
79 format?: Intl.NumberFormatOptions;
80 duration?: number;
81 flash?: boolean;
82 signColor?: boolean;
83 className?: string;
84 style?: CSSProperties;
85}
86
87export function AnimatedNumber({ value, locale = "en-US", format, duration = 700, flash = true, signColor = false, className, style }: AnimatedNumberProps) {
88 const fmt = useMemo(() => new Intl.NumberFormat(locale, format), [locale, JSON.stringify(format)]); // eslint-disable-line react-hooks/exhaustive-deps
89 const text = fmt.format(value);
90 // derive direction during render so digits know it on the same commit
91 const [prev, setPrev] = useState(value);
92 const [dir, setDir] = useState(0);
93 const [tick, setTick] = useState(0);
94 if (value !== prev) {
95 setPrev(value);
96 setDir(value > prev ? 1 : -1);
97 setTick((t) => t + 1);
98 }
99
100 // key characters from the right so columns persist when the length changes
101 const chars = Array.from(text);
102 const n = chars.length;
103 return (
104 <span
105 className={`fd-an${className ? " " + className : ""}`}
106 style={{ ["--fd-an-dur" as string]: `${duration}ms`, ...style }}
107 data-dir={dir > 0 ? "up" : dir < 0 ? "down" : undefined}
108 data-sign={signColor ? (value > 0 ? "pos" : value < 0 ? "neg" : "zero") : undefined}
109 >
110 <style>{css}</style>
111 <span className="fd-an-sr">{text}</span>
112 <span className="fd-an-row" aria-hidden="true" data-flash={flash && tick > 0 ? (tick % 2 ? "a" : "b") : undefined}>
113 {chars.map((ch, i) => {
114 const k = n - i;
115 return /\d/.test(ch) ? (
116 <Digit key={`d${k}`} d={Number(ch)} dir={dir} duration={duration} />
117 ) : (
118 <span key={`s${k}-${ch}`} className="fd-an-s">{ch}</span>
119 );
120 })}
121 </span>
122 </span>
123 );
124}
125
126const css = `
127.fd-an,.fd-an *{box-sizing:border-box}
128.fd-an{--fd-an-up:#16A34A;--fd-an-down:#DC2626;display:inline-block;margin:0;padding:0;font-variant-numeric:tabular-nums;font-feature-settings:"tnum";white-space:nowrap;vertical-align:baseline}
129.fd-an-sr{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);clip-path:inset(50%);white-space:nowrap;border:0}
130.fd-an-row{display:inline-flex;margin:0;padding:0;line-height:1;color:inherit}
131.fd-an[data-sign="pos"] .fd-an-row{color:var(--fd-an-up)}
132.fd-an[data-sign="neg"] .fd-an-row{color:var(--fd-an-down)}
133.fd-an[data-dir="up"] .fd-an-row[data-flash="a"]{animation:fd-an-up-a calc(var(--fd-an-dur) + 500ms) cubic-bezier(.22,1,.36,1)}
134.fd-an[data-dir="up"] .fd-an-row[data-flash="b"]{animation:fd-an-up-b calc(var(--fd-an-dur) + 500ms) cubic-bezier(.22,1,.36,1)}
135.fd-an[data-dir="down"] .fd-an-row[data-flash="a"]{animation:fd-an-down-a calc(var(--fd-an-dur) + 500ms) cubic-bezier(.22,1,.36,1)}
136.fd-an[data-dir="down"] .fd-an-row[data-flash="b"]{animation:fd-an-down-b calc(var(--fd-an-dur) + 500ms) cubic-bezier(.22,1,.36,1)}
137@keyframes fd-an-up-a{0%,30%{color:var(--fd-an-up)}}
138@keyframes fd-an-up-b{0%,30%{color:var(--fd-an-up)}}
139@keyframes fd-an-down-a{0%,30%{color:var(--fd-an-down)}}
140@keyframes fd-an-down-b{0%,30%{color:var(--fd-an-down)}}
141.fd-an-d{position:relative;display:inline-block;margin:0;padding:0;height:1em;overflow:hidden;
142 -webkit-mask-image:linear-gradient(transparent 0,#000 18%,#000 82%,transparent 100%);mask-image:linear-gradient(transparent 0,#000 18%,#000 82%,transparent 100%)}
143.fd-an-ghost{visibility:hidden;display:inline-block;margin:0;padding:0}
144.fd-an-strip{position:absolute;left:0;right:0;top:0;display:flex;flex-direction:column;margin:0;padding:0;will-change:transform}
145.fd-an-n{display:block;height:1em;margin:0;padding:0;text-align:center}
146.fd-an-s{display:inline-block;margin:0;padding:0;white-space:pre}
147@media (prefers-reduced-motion:reduce){
148 .fd-an[data-dir] .fd-an-row[data-flash]{animation-duration:120ms}
149 .fd-an-d{-webkit-mask-image:none;mask-image:none}
150}
151`;
152
153/* ───────────────────────────── demo ───────────────────────────── */
154
155export function Demo() {
156 const [value, setValue] = useState(12480.5);
157 const seed = useRef(11);
158 const step = () => {
159 seed.current = (seed.current * 16807) % 2147483647;
160 const r = seed.current / 2147483647 - 0.5;
161 setValue((v) => Math.max(1000, Number((v + r * 4200).toFixed(2))));
162 };
163
164 useEffect(() => {
165 if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;
166 const id = window.setInterval(() => {
167 if (!document.hidden) step();
168 }, 2600);
169 return () => window.clearInterval(id);
170 }, []);
171
172 return (
173 <div className="fd-and">
174 <style>{demoCss}</style>
175 <AnimatedNumber value={value} format={{ style: "currency", currency: "USD", minimumFractionDigits: 2, maximumFractionDigits: 2 }} className="fd-and-num" duration={900} />
176 <button type="button" className="fd-and-btn" onClick={step}>Update</button>
177 </div>
178 );
179}
180
181const F = `ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif`;
182const FD = `"SF Pro Display",ui-sans-serif,-apple-system,"Inter Display","Inter",sans-serif`;
183
184const demoCss = `
185.fd-and,.fd-and *,.fd-and *::before,.fd-and *::after{box-sizing:border-box}
186.fd-and{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:40px;min-height:100vh;width:100%;margin:0;padding:32px 20px;overflow:hidden;background:#f2f2f2;color:#1d1d1f;font:400 15px/1.4 ${F};letter-spacing:normal;-webkit-font-smoothing:antialiased}
187.fd-and .fd-and-num{margin:0;padding:0;color:#1d1d1f;font:600 clamp(44px,11vw,88px)/1 ${FD};letter-spacing:-.04em}
188.fd-and-btn{appearance:none;-webkit-appearance:none;margin:0;height:34px;padding:0 16px;border:0;border-radius:9999px;background:rgba(0,0,0,.06);color:#3c3c43;font:500 13px/1 ${F};letter-spacing:normal;cursor:pointer;transition:background-color 200ms}
189.fd-and-btn:hover{background:rgba(0,0,0,.1)}
190.fd-and-btn:focus-visible{outline:2px solid rgba(10,132,255,.8);outline-offset:2px}
191`;

More Text Effects. Same file contract.

Split Text, text preview: Split text animation for React: split a string into characters, words or lines and reveal it with rise, wave,…

Split Text

Split text animation for React: split a string into characters, words or lines and reveal it with rise, wave, blur or typewriter.

Text Morph, text preview: Text morph animation: cycle through words with a letter-level morph where shared letters glide into place and…

Text Morph

Text morph animation: cycle through words with a letter-level morph where shared letters glide into place and the rest blur away.

Text Repel, text preview: Text repel effect: a headline whose letters drift away from or toward your cursor and spring home, each on…

Text Repel

Text repel effect: a headline whose letters drift away from or toward your cursor and spring home, each on its own spring.

Questions about Animated Number

Run npx shadcn@latest add https://freedesignmd.com/r/animated-number.json, or open the page and copy the file into your project. It has zero dependencies besides React. What you copy is the exact file that renders the live demo.

Animated number counter: odometer digits roll to each new value, with Intl locale and currency formatting and a gain or loss flash. It is a free, MIT-licensed React text effects component from the freedesignmd library.

Yes. It is free and MIT-licensed, use, modify and ship it in personal and commercial projects without attribution.

All evergreen browsers. Reduced motion swaps digits without rolling.

Yes. It is a plain React component with its own styles, so it runs in Next.js, Vite, Remix and Lovable projects, and it needs no Tailwind setup. It has zero dependencies besides React.