On this page
A number that counts up as it scrolls into view makes a statistic feel earned. Done carelessly, it also re-renders React sixty times a second, jiggles the layout as digits change width, and reads out every intermediate number to screen readers. This guide builds a counter that avoids all three, in about thirty lines.
The counter
useInView starts the count once the number is on screen (the same trigger as other scroll animations), and Motion's animate function tweens from 0 to the target. Each frame writes the formatted value straight into the DOM, so React doesn't re-render while it counts. It's the same approach the Stats Section and Number Ticker components use:
"use client";
import { useEffect, useRef } from "react";
import { animate, useInView, useReducedMotion } from "motion/react";
export function CountUp({ to, decimals = 0, duration = 1.6 }: { to: number; decimals?: number; duration?: number }) {
const ref = useRef<HTMLSpanElement>(null);
const inView = useInView(ref, { once: true, amount: 0.6 });
const reduceMotion = useReducedMotion();
const format = new Intl.NumberFormat("en-GB", { minimumFractionDigits: decimals, maximumFractionDigits: decimals });
useEffect(() => {
const element = ref.current;
if (!element || !inView) return;
if (reduceMotion) {
element.textContent = format.format(to);
return;
}
const controls = animate(0, to, {
duration,
ease: [0.16, 1, 0.3, 1],
onUpdate: (value) => (element.textContent = format.format(value)),
});
return () => controls.stop();
}, [inView, reduceMotion, to, duration]);
return (
<span className="tabular-nums">
<span className="sr-only">{format.format(to)}</span>
<span ref={ref} aria-hidden>
{format.format(0)}
</span>
</span>
);
}Why each detail is there
- Writing to
textContentinstead of state: a state update every frame re-renders the component and its children; a direct write costs almost nothing. tabular-nums: gives every digit the same width, so the number doesn't jiggle as it counts and the text around it doesn't shift.Intl.NumberFormat: thousands separators and decimals as people expect them in their locale, at every step of the count.- An ease-out curve: fast at the start and slow at the end, so the final digits settle where people can read them.
once: true: counting again every time it scrolls past quickly becomes irritating.
Screen readers and reduced motion
A live-updating number would be announced over and over, or not at all. The counter instead gives screen readers the final value once, in a visually hidden span, and hides the animated one. People who ask for reduced motion see the final number straight away, because useReducedMotion skips the count.
A CSS-only version
Modern browsers can count with CSS alone, by registering a custom property as an integer and animating it into a counter:
@property --num {
syntax: "<integer>";
initial-value: 0;
inherits: false;
}
.count {
animation: count-up 2s cubic-bezier(0.16, 1, 0.3, 1) forwards;
counter-reset: num var(--num);
}
.count::after {
content: counter(num);
}
@keyframes count-up {
to { --num: 12400; }
}It's neat, but it runs on page load rather than on scroll, can't format thousands separators, and generated content isn't reliably read by screen readers, so the React version is the better choice for real statistics.
Questions
How do I start a count-up animation when it scrolls into view?
Use Motion's useInView on the element with once: true, and start the animation in an effect when it becomes true.
Why does my animated number make the layout jump?
Digits have different widths in most fonts. Add the tabular-nums class (font-variant-numeric: tabular-nums) so every digit is the same width.
Is a counting number accessible?
It can be: give screen readers the final value once in a visually hidden element, hide the animating one with aria-hidden, and skip the animation for people who prefer reduced motion.