Count Up
A number that counts up when it first comes into view, pauses off-screen and never starts over.
$npx shadcn@latest add https://framebrick.co/r/count-up.jsonFrom the Tustel template, see it in context.
- Years of experience
- 80+
- Projects completed
- 120+
- Happy clients
- 45+
- Awards received
- 18
import { CountUp } from "@/components/ui/count-up";
const facts = [
{ value: 80, suffix: "+", label: "Years of experience" },
{ value: 120, from: 40, suffix: "+", label: "Projects completed" },
{ value: 45, suffix: "+", label: "Happy clients" },
{ value: 18, label: "Awards received" },
];
export default function CountUpDemo() {
return (
<dl className="grid w-full max-w-sm grid-cols-2 gap-x-10 gap-y-8">
{facts.map((fact) => (
<div key={fact.label} className="flex flex-col gap-1">
<dt className="order-2 text-sm text-muted-foreground">{fact.label}</dt>
<dd className="text-4xl font-semibold tracking-tight">
<CountUp value={fact.value} from={fact.from} />
{fact.suffix}
</dd>
</div>
))}
</dl>
);
}Examples
Formatted, over a set time
A big number takes as long as a small one with `duration`, and `format` adds the separators.
12,400
Visitors this month
import { CountUp } from "@/components/ui/count-up";
/* A big number takes as long as a small one with `duration` (Framer's rate would take a minute), and `format` adds separators. */
export default function CountUpFormattedDemo() {
return (
<div className="flex flex-col items-center gap-1 text-center">
<p className="text-5xl font-semibold tracking-tight">
<CountUp value={12400} duration={1.6} format={{ useGrouping: true }} />
</p>
<p className="text-sm text-muted-foreground">Visitors this month</p>
</div>
);
}<CountUp value={12400} duration={1.6} format={{ useGrouping: true }} className="text-5xl font-semibold" />A decimal
A rating counts 0.0, 0.1 … 4.9 by the decimals it has, and the digits keep their fraction.
4.9
Average rating from 2,400 reviews
import { CountUp } from "@/components/ui/count-up";
/* A decimal counts by the decimals it has: 4.9 runs 0.0, 0.1 … 4.9, not 0 → 5 and a jump. `step` sets the size of a step. */
export default function CountUpDecimalDemo() {
return (
<div className="flex flex-col items-center gap-1 text-center">
<p className="flex items-center gap-2 text-5xl font-semibold tracking-tight">
<CountUp value={4.9} />
<svg aria-hidden viewBox="0 0 24 24" className="size-8 fill-current text-primary">
<path d="M12 2.5l2.9 6.1 6.6.8-4.9 4.6 1.3 6.6L12 17.3l-5.9 3.3 1.3-6.6L2.5 9.4l6.6-.8L12 2.5z" />
</svg>
</p>
<p className="text-sm text-muted-foreground">Average rating from 2,400 reviews</p>
</div>
);
}<CountUp value={4.9} className="text-5xl font-semibold" />
// the step can be set: 0 → 5 in halves
<CountUp value={5} step={0.5} />Installation
$ npx shadcn@latest add https://framebrick.co/r/count-up.json1. Install the dependencies.
npm install motion clsx tailwind-merge2. Add
cn()tolib/utils.ts(skip if shadcn already created it).import { clsx, type ClassValue } from "clsx"; import { extendTailwindMerge } from "tailwind-merge"; /* tailwind-merge must know the site's type scale (globals.css) is a font size, not a text color — otherwise cn("text-h6 text-muted-foreground") drops text-h6. */ const twMerge = extendTailwindMerge({ extend: { classGroups: { "font-size": [{ text: ["display", "h1", "h2", "h3", "h4", "h5", "h6"] }] } }, }); export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); }3. Copy the component into
components/ui/count-up.tsx."use client"; import * as React from "react"; import { cn } from "@/lib/utils"; /* * Count Up — a number that counts up once its first pixel comes into view (from the Tustel "Number Counter", a Framer * code component). Framer adds 1 every 5 ms, so 0 → 80 takes 0.4 s and 40 → 120 the same; it pauses while the number * is off-screen and carries on from where it was, and it never starts over. `duration` gives a number a fixed time * instead, which is what a four-digit figure needs. * * It counts by time, one frame at a time, not by a timer per step: a 5 ms interval is clamped and throttled by the * browser, a clock is not. The digits are tabular, so the width does not jitter while they change. * * A decimal counts by the decimals it has (Catchup's "4.9" rating, Framer's Number counter with a decimal): 4.9 runs * 0.0, 0.1 … 4.9, 49 steps at `interval` each, and the digits keep their fraction (0.0, not 0). `step` sets the size of * a step. It was a whole-number counter first, and a frame's rounding showed 0 … 5 and then jumped to 4.9. * * What a reader gets without the animation: the final number is always in the page, in a second element — it is what * screen readers read (the moving digits are aria-hidden, so they never announce 41, 42, 43…) and what shows with * reduced motion, in print and without JavaScript. Markup never depends on reduced motion (the server cannot know it); * CSS swaps the two elements, and the effect simply does not run. */ export type CountUpProps = { /** Where it ends. */ value: number; /** Where it starts. */ from?: number; /** The size of a step, above 0. Whole numbers count by 1; a decimal counts by the decimals `value` and `from` have (4.9 by 0.1), and the digits keep them. */ step?: number; /** Milliseconds per step, as in Framer: 1 → +1 every ms; 5 → +1 every 5 ms. */ interval?: number; /** Total time in seconds, instead of a rate: a big number then takes as long as a small one. */ duration?: number; /** Intl.NumberFormat options, e.g. { useGrouping: true } or { notation: "compact" }. Plain digits by default. */ format?: Intl.NumberFormatOptions; /** Locale for `format`. Always explicit, so the server and the browser agree on the digits. */ locale?: string; className?: string; }; /* How many decimals a number is written with: 4.9 → 1, 4.95 → 2, 7 → 0 (1e-7 → 7). */ const decimalsOf = (n: number) => { const [mantissa, exponent = "0"] = String(n).split("e"); return Math.max(0, (mantissa.split(".")[1]?.length ?? 0) - Number(exponent)); }; export function CountUp({ value, from = 0, step: stepProp, interval = 5, duration, format, locale = "en-US", className }: CountUpProps) { const ref = React.useRef<HTMLSpanElement>(null); const [progress, setProgress] = React.useState(0); const step = stepProp && stepProp > 0 ? stepProp : 10 ** -Math.max(decimalsOf(value), decimalsOf(from)); const decimals = decimalsOf(step); /* Whole numbers keep the plain digits (String). With decimals the digits keep their fraction (0.0, 0.1 …) so the width does not jump at 3.0, unless `format` sets the fraction digits itself; the final element draws the same way. */ const options = format ? JSON.stringify(format) : ""; const formatter = React.useMemo(() => { if (!options && !decimals) return null; const own: Intl.NumberFormatOptions = options ? JSON.parse(options) : { useGrouping: false }; const sets = own.minimumFractionDigits !== undefined || own.maximumFractionDigits !== undefined; return new Intl.NumberFormat(locale, decimals && !sets ? { minimumFractionDigits: decimals, maximumFractionDigits: decimals, ...own } : own); }, [locale, options, decimals]); const text = (n: number) => (formatter ? formatter.format(n) : String(n)); /* The steps it takes, and the one it is on. A step that does not divide the distance evenly never carries the number past `value`: the last frame sets `value` itself. */ const steps = +(Math.abs(value - from) / step).toFixed(6); const total = duration !== undefined ? duration * 1000 : steps * interval; const done = Math.min(Math.floor(steps), Math.round(steps * progress)); const current = progress >= 1 ? value : +(from + (value < from ? -1 : 1) * step * done).toFixed(decimals); React.useEffect(() => { const element = ref.current; if (!element || total <= 0 || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; let elapsed = 0; let last = -1; let frame = 0; /* The first frame after (re)starting only sets the clock: a frame's timestamp can be earlier than the moment it was asked for, and counting from that would run backwards (it showed -2 first). */ const tick = (now: number) => { elapsed = Math.min(total, elapsed + (last < 0 ? 0 : Math.max(0, now - last))); last = now; setProgress(elapsed / total); if (elapsed < total) frame = requestAnimationFrame(tick); }; /* Counts while any part of it is on screen; off-screen it waits, and the clock is not running while it waits. */ const observer = new IntersectionObserver(([entry]) => { cancelAnimationFrame(frame); last = -1; if (entry.isIntersecting && elapsed < total) frame = requestAnimationFrame(tick); }); observer.observe(element); return () => { observer.disconnect(); cancelAnimationFrame(frame); }; }, [total]); return ( <span ref={ref} className={cn("tabular-nums", className)}> <span aria-hidden data-count-up="" className="print:hidden motion-reduce:hidden"> {text(current)} </span> <span data-count-up-final="" className="sr-only print:not-sr-only motion-reduce:not-sr-only"> {text(value)} </span> {/* Without JavaScript the digits would stay at `from`: this swaps in the final number, and never applies when scripts run. */} <noscript> <style>{`[data-count-up]{display:none}[data-count-up-final]{position:static;width:auto;height:auto;margin:0;overflow:visible;clip-path:none;white-space:normal}`}</style> </noscript> </span> ); }
Usage
import { CountUp } from "@/components/ui/count-up";
<p className="text-5xl font-semibold">
<CountUp value={120} from={40} />+
</p>
// a four-digit figure: a fixed time instead of Framer's rate, and separators
<CountUp value={12400} duration={1.6} format={{ useGrouping: true }} />
// a decimal counts by the decimals it has: 0.0, 0.1 … 4.9
<CountUp value={4.9} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | — | Where it ends. |
| from | number | 0 | Where it starts. |
| step | number | 1, or 0.1 for one decimal | The size of a step, above 0. Left out, it follows the decimals `value` and `from` have: 4.9 counts by 0.1 (49 steps), 4.95 by 0.01, whole numbers by 1. The digits keep the step's fraction (0.0, 0.1 …) unless `format` sets the fraction digits. |
| interval | number | 5 | Milliseconds per step, as in Framer: +1 every 5 ms, so 0 → 80 takes 0.4 s, and 0 → 4.9 by 0.1 takes 0.25 s. |
| duration | number | — | Total time in seconds, instead of the rate: a big number then takes as long as a small one. |
| format | Intl.NumberFormatOptions | — | Formatting, e.g. { useGrouping: true } or { notation: "compact" }. Plain digits by default, as in Framer. |
| locale | string | "en-US" | Locale for `format`. Always explicit, so the server and the browser agree on the digits. |
| className | string | — | Classes for the number: its size, weight and colour. |
- It counts once its first pixel is in view, carries on from where it was after being off-screen, and never starts over.
- It counts by time, not with a timer per step: a 5 ms interval is clamped and throttled by the browser, a clock is not. The digits are tabular, so the width does not jitter while they change.
- The final number is always in the page, in a second element. Screen readers read it (the moving digits are aria-hidden, so 41, 42, 43 are never announced), and it is what shows with reduced motion, in print and without JavaScript. CSS swaps the two, so the markup never depends on reduced motion.
- Framer's rate suits small numbers: 80 takes 0.4 s, 12 000 would take a minute. Give big ones a `duration`.
- A decimal counts by the decimals it has, as Framer's Number counter does with a decimal: a 4.9 rating runs 0.0, 0.1 … 4.9 in 49 steps and keeps the one fraction digit all the way, so the width does not jump when it passes 3.0. Rounding every frame to a whole number would show 0 … 5 and then jump to 4.9.
- `step` sets the size of a step and the rate with it (distance ÷ step × `interval`). A step that does not divide the distance evenly never carries the number past `value`: the last frame sets `value` itself.
- For tests: with JavaScript on, the element's textContent holds the moving digits and the final number together. Query by role and name, or read the final element (data-count-up-final).
AI prompt
Paste into v0, Lovable, Bolt or Claude to rebuild this component in your own project.
Create a React + Tailwind CSS component called CountUp (no animation library).
- Props: value, from (0), step (the size of a step, above 0; when missing, 10 ** -d where d is the larger count of
decimals that value and from are written with, so 4.9 counts by 0.1 and whole numbers by 1), interval (5 —
milliseconds per step, as in Framer), duration (seconds; replaces the rate), format (Intl.NumberFormat options; plain
digits when absent), locale ("en-US", always explicit so the server and the browser agree).
- Count by time, not with a timer per step: keep the elapsed milliseconds in a closure, advance it by the frame's delta in
requestAnimationFrame, and put the progress (0 to 1) in state. With steps = |value - from| / step (cleaned with
toFixed(6)), the shown number is from ± step * min(floor(steps), round(steps * progress)), rounded to the step's
decimals with +x.toFixed(decimals); at progress 1 it is exactly value. The total time is steps * interval, or
duration * 1000.
- Digits: whole numbers print with String(n). With decimals, print through Intl.NumberFormat with minimumFractionDigits and
maximumFractionDigits = the step's decimals (and useGrouping false when there is no format), unless format itself sets
a fraction digit option, so 3.0 keeps its zero and the width does not jump. Draw the final number the same way.
- An IntersectionObserver on the element starts the frame loop when any part of it is on screen and cancels it when it
leaves. The elapsed time is kept, so it carries on from where it was and never starts over.
- Render two spans inside one span with tabular-nums: the moving digits (aria-hidden, print:hidden, motion-reduce:hidden)
and the final number (sr-only, print:not-sr-only, motion-reduce:not-sr-only). Add a <noscript><style> that hides the
moving digits and shows the final number, so a page without JavaScript shows the real value, not the start.
- Do not start the loop when matchMedia("(prefers-reduced-motion: reduce)") matches — read it inside the effect, never
while rendering: the markup must not depend on it, because the server cannot know it.










