Skip to content
← Components

Animated Counter

Count toward a target the first time it enters view.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/animated-counter.json

Use it

Scroll horizontally for long lines
"use client";
import { AnimatedCounter } from "@/components/jez-ui/ui/animated-counter";

export default function Example() {
  return (
    <>
      <div className="w-full rounded-xl bg-accent p-9 text-foreground md:p-14">
        <AnimatedCounter
          target={2048}
          className="font-display text-7xl tracking-tight md:text-8xl"
        />
        <h3 className="mt-6 text-2xl">Small steps. Real progress.</h3>
        <p className="mt-2 text-sm">Counts into view, once you're here.</p>
      </div>
    </>
  );
}

Props & types

Scroll horizontally for long lines
Omit<
  React.ComponentProps<"span">,
  keyof {
    target: number;
    duration?: number;
    className?: string;
  }
> & {
  target: number;
  duration?: number;
  className?: string;
}

Source

View 2 source files

registry/ui/animated-counter.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import { animate, useInView, useReducedMotion } from "motion/react";
export function AnimatedCounter({
  ref: forwardedRef,
  children,
  target,
  duration = 1.4,
  className,
  ...rootProps
}: Omit<
  React.ComponentProps<"span">,
  keyof {
    target: number;
    duration?: number;
    className?: string;
  }
> & {
  target: number;
  duration?: number;
  className?: string;
}) {
  const ref = React.useRef<HTMLSpanElement>(null);
  const visible = useInView(ref, { once: true });
  const reduce = useReducedMotion();
  const [value, setValue] = React.useState(0);
  React.useEffect(() => {
    if (!visible) return;
    const animation = animate(0, target, {
      duration: reduce ? 0 : duration,
      onUpdate: (v) => setValue(Math.round(v)),
    });
    return () => animation.stop();
  }, [visible, target, duration, reduce]);
  React.useImperativeHandle(forwardedRef, () => ref.current!, []);
  return (
    <span
      {...rootProps}
      ref={ref}
      className={cn("tabular-nums", className)}
      aria-label={String(target)}
    >
      {children !== undefined ? (
        children
      ) : (
        <>
          <span aria-hidden="true">{value.toLocaleString()}</span>
        </>
      )}
    </span>
  );
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

Dependencies

motion@13.2.0 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

Respect reduced-motion preferences. Decorative effects must not carry essential information. WebGL scenes include a static fallback and only render while visible.

Read the accessibility and performance guide →