{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-ticker",
  "type": "registry:component",
  "title": "Number Ticker",
  "description": "A number that counts up to its value when it scrolls into view, formatted with Intl.NumberFormat.",
  "categories": [
    "motion"
  ],
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn",
    "@rivier-ui/motion"
  ],
  "files": [
    {
      "path": "registry/motion/number-ticker.tsx",
      "type": "registry:component",
      "target": "components/motion/number-ticker.tsx",
      "content": "\"use client\";\n/**\n * @name number-ticker\n * @category motion\n * @description A number that counts up to its value when it scrolls into view, formatted with Intl.NumberFormat.\n * @when A headline statistic: customers, revenue, a percentage. Not for values that update live (render those directly), and not for more than a few on one screen.\n * @example\n * <NumberTicker value={12480} />\n * <NumberTicker value={0.9997} options={{ style: \"percent\", maximumFractionDigits: 2 }} />\n * <NumberTicker value={48000} locale=\"de-DE\" options={{ style: \"currency\", currency: \"EUR\", maximumFractionDigits: 0 }} />\n */\nimport { animate, useInView } from \"motion/react\";\nimport { useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, type ComponentProps } from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { DURATION, EASE, isOffscreen, useMotionSafe } from \"@/lib/motion\";\n\nexport type NumberTickerProps = Omit<ComponentProps<\"span\">, \"children\"> & {\n  /** The number to arrive at. This is what the server renders. */\n  value: number;\n  /** Where the count starts. Default 0. */\n  from?: number;\n  /** A BCP 47 locale. Default \"en-US\"; set it explicitly so the server and the browser format alike. */\n  locale?: string;\n  /** Intl.NumberFormat options: style, currency, notation, fraction digits. */\n  options?: Intl.NumberFormatOptions;\n  /** Seconds the count takes. Default: the scene duration (0.7). */\n  duration?: number;\n  /** Seconds to wait once it is in view. Default 0. */\n  delay?: number;\n  /** Also count when it is already in view as the page loads. Default false: the value is left as it is. */\n  playOnLoad?: boolean;\n};\n\nexport function NumberTicker({\n  className,\n  value,\n  from = 0,\n  locale = \"en-US\",\n  options,\n  duration = DURATION.scene,\n  delay = 0,\n  playOnLoad = false,\n  ref,\n  ...props\n}: NumberTickerProps) {\n  const rootRef = useRef<HTMLSpanElement>(null);\n  const valueRef = useRef<HTMLSpanElement>(null);\n  const countRef = useRef<HTMLSpanElement>(null);\n  useImperativeHandle(ref, () => rootRef.current as HTMLSpanElement, []);\n\n  // The options object is compared by content, not identity, so an inline object does not rebuild the formatter.\n  const optionsKey = JSON.stringify(options ?? null);\n  // eslint-disable-next-line react-hooks/exhaustive-deps -- optionsKey stands for options\n  const formatter = useMemo(() => new Intl.NumberFormat(locale, options), [locale, optionsKey]);\n\n  const safe = useMotionSafe();\n  const inView = useInView(rootRef, { once: true });\n  const latest = useRef({ value, from, formatter, duration, delay });\n  const phase = useRef<\"idle\" | \"armed\" | \"done\">(\"idle\");\n  useLayoutEffect(() => {\n    latest.current = { value, from, formatter, duration, delay };\n  });\n\n  // Before paint, and only if the visitor cannot see it yet (or playOnLoad): swap the value for the start of\n  // the count. The real value stays in the markup (it keeps the width and is what assistive technology\n  // reads); the count is drawn over it in an aria-hidden span, so nothing shifts as digits are added.\n  useLayoutEffect(() => {\n    const root = rootRef.current;\n    const final = valueRef.current;\n    const count = countRef.current;\n    if (!safe || !root || !final || !count) return;\n    if (playOnLoad || isOffscreen(root)) {\n      final.style.opacity = \"0\";\n      count.textContent = latest.current.formatter.format(latest.current.from);\n      phase.current = \"armed\";\n    }\n    return () => {\n      final.style.removeProperty(\"opacity\");\n      count.textContent = \"\";\n      phase.current = \"idle\";\n    };\n  }, [safe, playOnLoad]);\n\n  useEffect(() => {\n    const final = valueRef.current;\n    const count = countRef.current;\n    if (!safe || !inView || !final || !count || phase.current !== \"armed\") return;\n    phase.current = \"done\";\n    const current = latest.current;\n    let finished = false;\n    const animation = animate(current.from, current.value, {\n      duration: current.duration,\n      delay: current.delay,\n      ease: EASE,\n      onUpdate: (number) => {\n        count.textContent = latest.current.formatter.format(number);\n      },\n      onComplete: () => {\n        finished = true;\n        final.style.removeProperty(\"opacity\");\n        count.textContent = \"\";\n      },\n    });\n    return () => {\n      animation.stop();\n      // Interrupted (an effect re-run): leave it ready to count again rather than stuck part-way.\n      if (!finished && phase.current === \"done\") phase.current = \"armed\";\n    };\n  }, [safe, inView]);\n\n  return (\n    <span ref={rootRef} data-slot=\"number-ticker\" className={cn(\"relative inline-block whitespace-nowrap tabular-nums\", className)} {...props}>\n      <span ref={valueRef} data-slot=\"number-ticker-value\">\n        {formatter.format(value)}\n      </span>\n      <span ref={countRef} aria-hidden=\"true\" data-slot=\"number-ticker-count\" className=\"pointer-events-none absolute inset-y-0 start-0\" />\n    </span>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "motion",
    "when": "A headline statistic: customers, revenue, a percentage. Not for values that update live (render those directly), and not for more than a few on one screen.",
    "example": "<NumberTicker value={12480} />\n<NumberTicker value={0.9997} options={{ style: \"percent\", maximumFractionDigits: 2 }} />\n<NumberTicker value={48000} locale=\"de-DE\" options={{ style: \"currency\", currency: \"EUR\", maximumFractionDigits: 0 }} />",
    "client": true
  }
}
