{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-animated-number",
  "title": "Use Animated Number",
  "description": "A hook for animating a number.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/use-in-view.json"
  ],
  "files": [
    {
      "path": "registry/hooks/use-animated-number.tsx",
      "content": "'use client'\n\nimport { useInView } from '@/registry/hooks/use-in-view'\nimport { useCallback, useEffect, useRef, useState, useSyncExternalStore } from 'react'\n\ntype EasingFunction = (t: number) => number\n\nconst easings: Record<string, EasingFunction> = {\n  linear: (t) => t,\n  easeOut: (t) => 1 - Math.pow(1 - t, 3),\n  easeIn: (t) => t * t * t,\n  easeInOut: (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2),\n  spring: (t) => 1 - Math.pow(Math.cos(t * Math.PI * 0.5), 3),\n}\n\nconst REDUCED_MOTION = '(prefers-reduced-motion: reduce)'\n\nfunction subscribeToReducedMotion(onChange: () => void) {\n  const query = window.matchMedia(REDUCED_MOTION)\n  query.addEventListener('change', onChange)\n  return () => query.removeEventListener('change', onChange)\n}\n\nconst prefersReducedMotion = () => window.matchMedia(REDUCED_MOTION).matches\n\ninterface UseAnimatedNumberOptions {\n  /** Milliseconds, 1000 by default. */\n  duration?: number\n  /** @default 'easeOut' */\n  easing?: keyof typeof easings | EasingFunction\n  /** @default 0 */\n  from?: number\n  /** From 0 to 1, 0.5 by default. */\n  threshold?: number\n  /** @default '0px' */\n  rootMargin?: string\n  /** Milliseconds, 0 by default. */\n  delay?: number\n  reanimateOnView?: boolean\n}\n\nexport function useAnimatedNumber(to: number, options: UseAnimatedNumberOptions = {}) {\n  const {\n    delay = 0,\n    duration = 1000,\n    easing = 'easeOut',\n    from = 0,\n    reanimateOnView = false,\n    rootMargin = '0px',\n    threshold = 0.5,\n  } = options\n\n  const [value, setValue] = useState(from)\n  const [isAnimating, setIsAnimating] = useState(false)\n  const hasAnimated = useRef(false)\n  const displayedValueRef = useRef(from)\n  const previousTo = useRef(to)\n  const animationRef = useRef<number | null>(null)\n  const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)\n\n  const { isInView, ref } = useInView({ rootMargin, threshold })\n  const reducedMotion = useSyncExternalStore(\n    subscribeToReducedMotion,\n    prefersReducedMotion,\n    () => false,\n  )\n\n  const decimals = to.toString().split('.')[1]?.length || 0\n\n  const easingFn =\n    typeof easing === 'function' ? easing : easings[easing] || easings.easeOut\n\n  const cancel = useCallback(() => {\n    if (animationRef.current) {\n      cancelAnimationFrame(animationRef.current)\n      animationRef.current = null\n    }\n    if (timeoutRef.current) {\n      clearTimeout(timeoutRef.current)\n      timeoutRef.current = null\n    }\n  }, [])\n\n  const animate = useCallback(\n    (startValue: number = displayedValueRef.current) => {\n      cancel()\n\n      if (prefersReducedMotion()) {\n        displayedValueRef.current = to\n        setValue(to)\n        setIsAnimating(false)\n        return\n      }\n\n      const startTime = performance.now()\n\n      setIsAnimating(true)\n\n      const tick = (currentTime: number) => {\n        const elapsed = currentTime - startTime\n        const progress = Math.min(elapsed / duration, 1)\n        const easedProgress = easingFn(progress)\n        const currentValue = startValue + (to - startValue) * easedProgress\n        const rounded =\n          decimals > 0 ? Number(currentValue.toFixed(decimals)) : Math.round(currentValue)\n\n        displayedValueRef.current = rounded\n        setValue(rounded)\n\n        if (progress < 1) {\n          animationRef.current = requestAnimationFrame(tick)\n        } else {\n          setIsAnimating(false)\n          animationRef.current = null\n        }\n      }\n\n      animationRef.current = requestAnimationFrame(tick)\n    },\n    [cancel, to, duration, easingFn, decimals],\n  )\n\n  const reset = useCallback(() => {\n    cancel()\n    displayedValueRef.current = from\n    setValue(from)\n    setIsAnimating(false)\n    hasAnimated.current = false\n  }, [cancel, from])\n\n  const startAnimation = useCallback(\n    (startValue?: number) => {\n      if (delay > 0) {\n        timeoutRef.current = setTimeout(() => animate(startValue), delay)\n      } else {\n        animate(startValue)\n      }\n    },\n    [animate, delay],\n  )\n\n  const startAnimationRef = useRef(startAnimation)\n  useEffect(() => {\n    startAnimationRef.current = startAnimation\n  }, [startAnimation])\n\n  useEffect(() => {\n    if (!isInView) return\n    if (hasAnimated.current && !reanimateOnView) return\n\n    const frame = requestAnimationFrame(() => {\n      hasAnimated.current = true\n      startAnimationRef.current(reanimateOnView ? from : displayedValueRef.current)\n    })\n    return () => cancelAnimationFrame(frame)\n  }, [isInView, reanimateOnView, from])\n\n  useEffect(() => {\n    if (previousTo.current === to) return\n    previousTo.current = to\n    if (hasAnimated.current) animate(displayedValueRef.current)\n  }, [to, animate])\n\n  useEffect(() => cancel, [cancel])\n\n  return {\n    animate,\n    isAnimating: isAnimating && !reducedMotion,\n    isInView,\n    ref,\n    reset,\n    value: reducedMotion ? to : value,\n  }\n}\n",
      "type": "registry:hook",
      "target": "@hooks/use-animated-number.tsx"
    }
  ],
  "type": "registry:hook"
}
