"use client"; import { useEffect, useRef, useState } from "react"; export function AnimatedNumber({ value, duration = 1200, format = (n: number) => Math.round(n).toLocaleString(), }: { value: number; duration?: number; format?: (n: number) => string; }) { const [n, setN] = useState(value); const fromRef = useRef(value); const startRef = useRef(null); const prevValueRef = useRef(value); // Flash key bumps whenever the input value actually changes, so the // wrapping span can drive a one-shot CSS animation on its own without // colliding with the counter-up rAF loop. const [flashKey, setFlashKey] = useState(0); const flashTone = useRef<"up" | "down" | null>(null); useEffect(() => { // Effect deps are [value, duration] — but a parent re-render that // hands us an identical number still re-runs the effect when the // component itself re-renders (not when deps change). Guard against // that: if `value` already matches both the animation target and // the displayed number, skip the rAF loop entirely. if (fromRef.current === value && prevValueRef.current === value) { return; } // Reduced-motion: a CSS rule can't stop a JS rAF loop, so honour // the preference here — snap straight to the target, no count-up. if ( typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches ) { prevValueRef.current = value; fromRef.current = value; setN(value); return; } if (prevValueRef.current !== value) { flashTone.current = value > prevValueRef.current ? "up" : "down"; setFlashKey((k) => k + 1); prevValueRef.current = value; } const from = fromRef.current; const target = value; startRef.current = null; let raf = 0; const tick = (t: number) => { if (startRef.current == null) startRef.current = t; const p = Math.min(1, (t - startRef.current) / duration); const eased = 1 - (1 - p) ** 3; const next = from + (target - from) * eased; setN(next); fromRef.current = next; if (p < 1) raf = requestAnimationFrame(tick); else fromRef.current = target; }; raf = requestAnimationFrame(tick); return () => cancelAnimationFrame(raf); }, [value, duration]); return ( 0 ? `animated-number-flash animated-number-flash-${flashTone.current ?? "up"}` : undefined } key={flashKey} > {format(n)} ); }