/Data display
Data display • React
Count Up Number
A stat that counts up from zero the first time it scrolls into view, easing out so the last digits settle slowly; it is sized to the finished number, so nothing around it shifts while it climbs.
Installation
Terminal
npx shadcn@latest add @microkit/count-up-numberCode
1"use client";23import { useEffect, useRef, useState } from "react";45export function CountUpNumber({6value = 48290,7duration = 2000,8decimals = 0,9prefix = "",10suffix = "",11locale = "en-US",12className = "",13}) {14const ref = useRef<HTMLSpanElement>(null);15const shown = useRef(0);16const [display, setDisplay] = useState(0);17const [inView, setInView] = useState(false);18const formatter = new Intl.NumberFormat(locale, {19minimumFractionDigits: decimals,20maximumFractionDigits: decimals,21});2223useEffect(() => {24const node = ref.current;25if (!node) return;26// The first report arrives on observe and says "intersecting" for a sliver,27// so the ratio is what decides the number has actually been seen.28const observer = new IntersectionObserver(29([entry]) => {30if (entry.intersectionRatio < 0.5) return;31observer.disconnect();32setInView(true);33},34{ threshold: 0.5 },35);36observer.observe(node);37return () => observer.disconnect();38}, []);3940useEffect(() => {41if (!inView) return;42// Counting from what is on screen lets a new value pick up mid-count instead of restarting at zero.43const from = shown.current;44const length = window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : duration;45const start = performance.now();46let frame = 0;4748const step = () => {49const progress = length > 0 ? Math.min(1, (performance.now() - start) / length) : 1;50// Expo-out covers most of the distance at once and spends the rest settling the last digits.51const eased = progress === 1 ? 1 : 1 - Math.pow(2, -10 * progress);52shown.current = from + (value - from) * eased;53setDisplay(shown.current);54if (progress < 1) frame = requestAnimationFrame(step);55};5657frame = requestAnimationFrame(step);58return () => cancelAnimationFrame(frame);59}, [inView, value, duration]);6061return (62<span ref={ref} className={`inline-grid whitespace-nowrap tabular-nums ${className}`}>63{/* The settled value sizes the box and is what screen readers and copy-paste get, but it is never painted. */}64<span className="text-transparent [grid-area:1/1] motion-reduce:text-inherit">65{`${prefix}${formatter.format(value)}${suffix}`}66</span>67{/* Right-aligned over the settled value, each digit climbs in the column it will finish in. */}68<span69className="pointer-events-none select-none justify-self-end [grid-area:1/1] motion-reduce:invisible"70aria-hidden="true"71>72{`${prefix}${formatter.format(display)}${suffix}`}73</span>74</span>75);76}7778export function CountUpStat() {79return (80<figure className="m-0 grid justify-items-center gap-[14px] text-center text-[light-dark(#14181e,#f2f4f6)]">81<CountUpNumber value={48290} className="text-[64px] font-semibold leading-none tracking-[-.045em]" />82<figcaption className="inline-flex items-center gap-[10px] text-[14px] font-medium leading-none text-[light-dark(#5d6673,#8d939d)]">83Monthly active users84<CountUpNumber85value={12.4}86decimals={1}87prefix="+"88suffix="%"89className="rounded-full bg-[light-dark(#ffedd5,rgba(249,115,22,.14))] px-[8px] py-[5px] text-[12px] font-semibold text-[light-dark(#c2410c,#fb923c)]"90/>91</figcaption>92</figure>93);94}