Command Palette

Search for a command to run...

Animated Number

A dynamic component for smooth transitions between panels.

0

Installation

Install the following dependencies:

pnpm add motion

Create a file at components/motion/animated-number.tsx and paste the given code.

"use client";
import { cn } from "@/lib/utils";
import { motion, SpringOptions, useSpring, useTransform } from "motion/react";
import { JSX, useEffect } from "react";
 
export type AnimatedNumberProps = {
    value: number;
    className?: string;
    springOptions?: SpringOptions;
    as?: React.ElementType;
};
 
export function AnimatedNumber({
    value,
    className,
    springOptions,
    as = "span",
}: AnimatedNumberProps) {
    const MotionComponent = motion.create(as as keyof JSX.IntrinsicElements);
 
    const spring = useSpring(value, springOptions);
    const display = useTransform(spring, (current) =>
        Math.round(current).toLocaleString(),
    );
 
    useEffect(() => {
        spring.set(value);
    }, [spring, value]);
 
    return (
        <MotionComponent className={cn("tabular-nums", className)}>
            {display}
        </MotionComponent>
    );
}

Update the import paths to match your project setup.