"use client" import { useEffect, useRef, useState } from "react" /** Counts from 0 to `value` once it scrolls into view. */ export function CountUp({ value, duration = 1400 }: { value: number; duration?: number }) { const [shown, setShown] = useState(0) const ref = useRef(null) useEffect(() => { const el = ref.current if (!el) return if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) { setShown(value) return } let frame = 0 const observer = new IntersectionObserver(([entry]) => { if (!entry.isIntersecting) return observer.disconnect() const start = performance.now() const tick = (now: number) => { const t = Math.min(1, (now - start) / duration) setShown(Math.round(value * (1 - Math.pow(1 - t, 3)))) if (t < 1) frame = requestAnimationFrame(tick) } frame = requestAnimationFrame(tick) }) observer.observe(el) return () => { observer.disconnect() cancelAnimationFrame(frame) } }, [value, duration]) return ( {shown.toLocaleString("fr-CA")} ) }