Back to NumbersYou are on Counting Number.

Counting Number

A number that counts to its target on a heavily damped spring.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
counting-number.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/counting-number.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/counting-number.json
npmthe default on every machine

First 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.

5 runtimes
counting-number.tsx
"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.json
npmthe default on every machine

Dependencies

motion lucide-react

The 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.

02

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.

counting-number.tsx
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>
  );
}
03

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.
spring 90 / 50restDelta 0.001tabular-numsdirect DOM write
04

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

PropTypeDefault
numbernumber0
fromNumbernumber0
inViewbooleanfalse
inViewOncebooleantrue
delaynumber0
decimalPlacesnumber0
decimalSeparatorstring"."
thousandSeparatorstring | undefined—
padStartbooleanfalse
initiallyStablebooleanfalse
onCompleted() => void—
transitionTransitioncomponent spring
classNamestring—

Next component

KPI Counter

A metric card whose number counts, delta lands and sparkline draws itself.