Data Display
Animated Number
Counts up to its value once scrolled into view; locale-formatted by default.
Motioncount-up via animate() with tween.draw easing
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { AnimatedNumber } from "kineto/animated-number";Preview
$0
Average deal value
tsx
"use client";
import * as React from "react";
import { animate, useInView, useReducedMotion } from "motion/react";
export interface AnimatedNumberProps
extends Omit<React.HTMLAttributes<HTMLSpanElement>, "children"> {
value: number;
/** Format the in-flight value (default: rounded + locale-grouped). */
format?: (value: number) => string;
/** Seconds for the count-up. */
duration?: number;
}
/**
* Counts up to `value` once it scrolls into view. Honors reduced motion by rendering
* the final value immediately. Uses `tween.draw`-style easing for a settled finish.
*/
const AnimatedNumber = React.forwardRef<HTMLSpanElement, AnimatedNumberProps>(
({ value, format, duration = 1.1, ...props }, forwardedRef) => {
const innerRef = React.useRef<HTMLSpanElement>(null);
React.useImperativeHandle(forwardedRef, () => innerRef.current as HTMLSpanElement);
const inView = useInView(innerRef, { once: true, margin: "-10%" });
const reduced = useReducedMotion();
const formatRef = React.useRef(format);
formatRef.current = format;
const fmt = React.useCallback(
(n: number) => (formatRef.current ?? ((x) => Math.round(x).toLocaleString()))(n),
[]
);
const [display, setDisplay] = React.useState(() => fmt(0));
React.useEffect(() => {
if (!inView) return;
if (reduced) {
setDisplay(fmt(value));
return;
}
const controls = animate(0, value, {
duration,
ease: [0.16, 1, 0.3, 1],
onUpdate: (latest) => setDisplay(fmt(latest)),
});
return () => controls.stop();
}, [inView, value, duration, reduced, fmt]);
return (
<span ref={innerRef} {...props}>
{display}
</span>
);
}
);
AnimatedNumber.displayName = "AnimatedNumber";
export { AnimatedNumber };src/components/ui/animated-number.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | — | Target value. |
| format | (value: number) => string | — | Format the in-flight value. |
| duration | number | 1.1 | Seconds for the count-up. |