Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/counting-number.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import {
animate,
useMotionValue,
useMotionValueEvent,
type Transition,
} from "motion/react";
import { cn } from "@/lib/utils";
import {
srOnlyStyle,
useReduceMotion,
useSeen,
useTempoTransition,
} from "./motion";
type Format = {
decimals: number;
decimalSeparator: string;
thousandSeparator?: string;
padLength: number;
};
function formatNumber(value: number, options: Format) {
const { decimals, decimalSeparator, thousandSeparator, padLength } = options;
const fixed = Math.abs(value).toFixed(decimals);
const sign = value < 0 && Number(fixed) !== 0 ? "-" : "";
const [integer = "0", fraction] = fixed.split(".");
const padded = integer.padStart(padLength, "0");
const grouped =
thousandSeparator === undefined
? padded
: padded.replace(/\B(?=(\d{3})+$)/g, thousandSeparator);
return fraction
? `${sign}${grouped}${decimalSeparator}${fraction}`
: `${sign}${grouped}`;
}
const countSpring: Transition = {
type: "spring",
stiffness: 90,
damping: 50,
restDelta: 0.001,
restSpeed: 0.01,
};
export type CountingNumberProps = Omit<React.ComponentProps<"span">, "children"> & {
number: number;
fromNumber?: number;
padStart?: boolean;
decimalPlaces?: number;
decimalSeparator?: string;
thousandSeparator?: string;
inView?: boolean;
inViewOnce?: boolean;
delay?: number;
initiallyStable?: boolean;
transition?: Transition;
onCompleted?: () => void;
};
export function CountingNumber({
number,
fromNumber = 0,
padStart = false,
decimalPlaces = 0,
decimalSeparator = ".",
thousandSeparator,
inView = false,
inViewOnce = true,
delay = 0,
initiallyStable = false,
transition,
onCompleted,
className,
...props
}: CountingNumberProps) {
const ref = React.useRef<HTMLSpanElement>(null);
const visualRef = React.useRef<HTMLSpanElement>(null);
const seen = useSeen(ref, inView, inViewOnce);
const reduce = useReduceMotion();
const spring = useTempoTransition(transition ?? countSpring);
const value = useMotionValue(initiallyStable ? number : fromNumber);
const format = React.useMemo<Format>(
() => ({
decimals: decimalPlaces,
decimalSeparator,
thousandSeparator,
padLength: padStart ? Math.floor(Math.abs(number)).toString().length : 0,
}),
[decimalPlaces, decimalSeparator, number, padStart, thousandSeparator],
);
const write = React.useCallback(
(latest: number) => {
const node = visualRef.current;
if (node) node.textContent = formatNumber(latest, format);
},
[format],
);
useMotionValueEvent(value, "change", write);
React.useLayoutEffect(() => {
write(value.get());
}, [write, value]);
const springRef = React.useRef(spring);
React.useEffect(() => {
springRef.current = spring;
}, [spring]);
React.useEffect(() => {
if (!seen) return;
if (reduce) {
value.jump(number);
onCompleted?.();
return;
}
let controls: ReturnType<typeof animate> | undefined;
const timer = window.setTimeout(() => {
controls = animate(value, number, {
...springRef.current,
onComplete: onCompleted,
} as Transition);
}, delay);
return () => {
window.clearTimeout(timer);
controls?.stop();
};
}, [delay, number, onCompleted, reduce, seen, value]);
React.useEffect(() => () => value.stop(), [value]);
const finalText = formatNumber(number, format);
return (
<span
ref={ref}
data-slot="counting-number"
className={cn("inline-block tabular-nums", className)}
{...props}
>
<span aria-hidden="true" ref={visualRef} />
<span className="sr-only" style={srOnlyStyle()}>
{finalText}
</span>
</span>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/counting-number.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { CountingNumber } from "@/components/prequelui/counting-number";
export default function Demo() {
return (
<CountingNumber
number={76}
fromNumber={0}
thousandSeparator=","
className="text-5xl font-medium"
/>
);
}import { CountingNumber } from "@/components/prequelui/counting-number";
export function Variant() {
return (
<CountingNumber tempo={1.2}>
…
</CountingNumber>
);
}Animation
The value is animated as a number, never as a string. Intermediate frames are written straight into the visual span outside React, so the digits update at frame rate while the component itself does not re-render.
- Spring: stiffness 90, damping 50, restDelta 0.001, restSpeed 0.01.
- The final value is exposed once to assistive technology in a visually hidden span.
- Digits use tabular numerals, so the width never shifts while counting.
- Reduced motion writes the target value immediately without intermediate frames.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
CountingNumber
| Prop | Type | Default |
|---|---|---|
| number | number | 0 |
| fromNumber | number | 0 |
| inView | boolean | false |
| inViewOnce | boolean | true |
| delay | number | 0 |
| decimalPlaces | number | 0 |
| decimalSeparator | string | "." |
| thousandSeparator | string | undefined | — |
| padStart | boolean | false |
| initiallyStable | boolean | false |
| onCompleted | () => void | — |
| transition | Transition | component spring |
| className | string | — |