"use client"; import { animate, useInView } from "motion/react"; import { useEffect, useRef, useState } from "react"; import { cn } from "@/lib/utils"; export interface AnimatedNumberProps { value: number; duration?: number; format?: (n: number) => string; className?: string; startOnView?: boolean; } export function AnimatedNumber({ value, duration = 1.2, format = (n) => Math.round(n).toLocaleString(), className, startOnView = true, }: AnimatedNumberProps) { const ref = useRef(null); const inView = useInView(ref, { once: true, amount: 0.6 }); const [display, setDisplay] = useState(0); const fromRef = useRef(0); useEffect(() => { if (startOnView && !inView) return; const controls = animate(fromRef.current, value, { duration, ease: [0.16, 1, 0.3, 1], onUpdate: (v) => setDisplay(v), }); fromRef.current = value; return () => controls.stop(); }, [value, duration, inView, startOnView]); return ( {format(display)} ); }