A number that counts to its new value on a wheel of digits, like an odometer.
Digits roll up as the number grows and back down as it shrinks, fading in and out at the top and bottom of each window rather than being cut off. A number that keeps changing spins instead of stuttering. The width always matches the number: a place that is gained slides in, one that is lost fades out while the rest close up, and the separators glide along with them.
Options you can pass to customize this component.
value*numberThe number to show. Each change animates from whatever is on screen, so it can be updated as often as you like.
decimalsnumberHow many decimal places to keep, up to 15. The value is rounded to that precision, so the wheels always land on a whole digit.
durationnumberRoughly how many seconds a digit takes to settle on its new face. Lower it when the value updates continuously, so the wheels stay close to the number.
padStartnumberFewest whole digits to show, up to 24, padded with leading zeros. Use it to hold the width steady when the number crosses a power of ten.
separatorstringCharacter placed every three whole digits. Pass an empty string to group nothing.
decimalSeparatorstringCharacter between the whole and decimal digits.
groupingwesternindianWhere the separators land. Western groups every three digits, 1,234,567. Indian groups the last three then pairs, 12,34,567.
prefixReactNodeRendered before the number, for a currency mark or an icon. It sits outside the wheels and never animates.
suffixReactNodeRendered after the number, for a unit or a percent sign.
classNamestringExtra classes merged onto the root element (data-slot="animated-counter"). Size, color, font and weight are all inherited, so style it like text. The digit columns carry data-slot="animated-counter-digit" and the separators data-slot="animated-counter-mark", for reaching either from the root.
npx shadcn@latest add swamimalode07/rare-ui/animated-counter"use client"import { AnimatedCounter } from "@/components/ui/animated-counter"export function Revenue({ total }: { total: number }) {return <AnimatedCounter value={total} className="text-6xl font-medium" />}// money: two decimals and a currency mark that stays put// <AnimatedCounter value={total} decimals={2} prefix={<span>₹</span>} grouping="indian" />// fixed width, so a timer never shifts as it rolls over// <AnimatedCounter value={seconds} padStart={4} separator="" />
Click the code icon in the top-right corner to view the source code.
Most components here are recreations of great work from around the web. I don't claim to be the original creator - this is my attempt to reverse-engineer, replicate, and often add a few extra features. I've tried to credit everyone; if I missed someone, let me know.
1