Introducing Motion-Primitives Pro - Advanced components and templates to help you build a website that stands out.

Animated Number

Easily animate numbers.

Examples

Basic

0

Basic counter

1,000

With useInView

0

Code

'use client';
import { cn } from '@/lib/utils';
import { motion, SpringOptions, useSpring, useTransform } from 'framer-motion';
import { useEffect } from 'react';

type AnimatedNumber = {
  value: number;
  className?: string;
  springOptions?: SpringOptions;
};

export function AnimatedNumber({
  value,
  className,
  springOptions,
}: AnimatedNumber) {
  const spring = useSpring(value, springOptions);
  const display = useTransform(spring, (current) =>
    Math.round(current).toLocaleString()
  );

  useEffect(() => {
    spring.set(value);
  }, [spring, value]);

  return (
    <motion.span className={cn('tabular-nums', className)}>
      {display}
    </motion.span>
  );
}

Please add:

Component API

PropTypeDefaultDescription
valuenumberThe number to be animated.
classNamestringOptional CSS class for styling the number item container.
springOptionsSpringOptionsSpring options from framer-motion for the animation.