{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-ticker",
  "title": "Number Ticker",
  "description": "A number that counts up to its value when it comes into view, formatted for the locale.",
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/use-entrance"
  ],
  "files": [
    {
      "path": "registry/default/ui/number-ticker.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useMemo, useRef, type ComponentProps, type Ref } from \"react\";\nimport { useEntrance, type EntranceTrigger } from \"@/registry/default/hooks/use-entrance\";\nimport { cn } from \"@/registry/default/lib/utils\";\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === \"function\") ref(node);\n      else if (ref) ref.current = node;\n    }\n  };\n}\n\nfunction prefersReducedMotion() {\n  return window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches === true;\n}\n\n/** Digits after the decimal point, so 12.5 counts in tenths rather than in noisy thousandths. */\nfunction decimalsOf(value: number) {\n  if (!Number.isFinite(value)) return 0;\n  const [, fraction = \"\"] = String(value).split(\".\");\n  return Math.min(fraction.length, 20);\n}\n\n/**\n * Options for the formatter. Without any digit settings of their own, plain\n * numbers keep the precision of `value` and `from` on every frame, so the\n * width does not jump between frames.\n */\nfunction formatOptions(value: number, from: number, format?: Intl.NumberFormatOptions) {\n  const style = format?.style ?? \"decimal\";\n  const hasDigits =\n    format?.minimumFractionDigits !== undefined ||\n    format?.maximumFractionDigits !== undefined ||\n    format?.minimumSignificantDigits !== undefined ||\n    format?.maximumSignificantDigits !== undefined;\n  if (hasDigits || (style !== \"decimal\" && style !== \"unit\")) return format;\n  const decimals = Math.max(decimalsOf(value), decimalsOf(from));\n  return { minimumFractionDigits: decimals, maximumFractionDigits: decimals, ...format };\n}\n\nconst easeOutCubic = (t: number) => 1 - (1 - t) ** 3;\n\ninterface NumberTickerProps extends Omit<ComponentProps<\"span\">, \"children\"> {\n  /** The number it counts to. */\n  value: number;\n  /** The number it counts from. */\n  from?: number;\n  /** Length of the count, in ms. */\n  duration?: number;\n  /** Wait before it starts, in ms. */\n  delay?: number;\n  /** Locale for the digits, separators and currency. */\n  locale?: string;\n  /** Passed to `Intl.NumberFormat`: currency, percent, compact notation, digits… */\n  format?: Intl.NumberFormatOptions;\n  /** \"load\" counts as soon as the page runs; \"in-view\" when the number scrolls into view. */\n  trigger?: EntranceTrigger;\n  /** In-view: count the first time only. */\n  once?: boolean;\n  /** Takes over from `trigger`: `false` holds it at `from`, `true` counts. */\n  play?: boolean;\n}\n\n/**\n * A number that counts up (or down) to its value with an ease-out. It\n * writes straight to the text once per frame instead of re-rendering\n * React. The server HTML holds the final, formatted value for search\n * engines and browsers without JavaScript; screen readers only ever hear\n * the final value, since the moving digits are hidden from them; and with\n * `prefers-reduced-motion` the final value just shows. Digits are tabular,\n * so the width holds still while it counts.\n *\n * @example\n * <NumberTicker value={12840} format={{ style: \"currency\", currency: \"USD\" }} />\n */\nfunction NumberTicker({\n  value,\n  from = 0,\n  duration = 1600,\n  delay = 0,\n  locale = \"en-US\",\n  format,\n  trigger = \"in-view\",\n  once = true,\n  play,\n  className,\n  ref,\n  ...props\n}: NumberTickerProps) {\n  const [observe, phase] = useEntrance<HTMLSpanElement>({ trigger, once, play });\n  const mergedRef = useMemo(() => mergeRefs(ref, observe), [ref, observe]);\n  const output = useRef<HTMLSpanElement>(null);\n\n  // A new `format` object on every render must not restart the count.\n  const optionsKey = JSON.stringify(formatOptions(value, from, format) ?? null);\n  const formatter = useMemo(\n    () =>\n      new Intl.NumberFormat(\n        locale,\n        (JSON.parse(optionsKey) as Intl.NumberFormatOptions | null) ?? undefined\n      ),\n    [locale, optionsKey]\n  );\n  const finalText = formatter.format(value);\n\n  useEffect(() => {\n    const node = output.current;\n    if (!node) return;\n    // Change the value of React's own text node rather than replacing it,\n    // so React's later updates still land on the node that is on screen.\n    const write = (text: string) => {\n      const child = node.firstChild;\n      if (child?.nodeType === Node.TEXT_NODE) child.nodeValue = text;\n      else node.textContent = text;\n      node.style.visibility = \"visible\";\n    };\n    const settle = () => write(formatter.format(value));\n    if (phase === \"static\" || prefersReducedMotion()) return settle();\n    if (phase === \"armed\") return write(formatter.format(from));\n\n    const length = Math.max(duration, 1);\n    write(formatter.format(from));\n    // Timed from the first frame's own timestamp, so it never mixes clocks.\n    let start: number | undefined;\n    let id = 0;\n    const tick = (now: number) => {\n      start ??= now + delay;\n      const progress = Math.min(Math.max((now - start) / length, 0), 1);\n      if (progress >= 1) return settle();\n      write(formatter.format(from + (value - from) * easeOutCubic(progress)));\n      id = requestAnimationFrame(tick);\n    };\n    id = requestAnimationFrame(tick);\n    return () => cancelAnimationFrame(id);\n  }, [phase, formatter, value, from, duration, delay]);\n\n  return (\n    <span\n      ref={mergedRef}\n      data-slot=\"number-ticker\"\n      data-state={phase}\n      className={cn(\"inline-block tabular-nums\", className)}\n      {...props}\n    >\n      <span className=\"sr-only\">{finalText}</span>\n      {/* Hidden until the script takes over when it is about to play, so the final value does not flash first; shown as is without JavaScript. */}\n      <span\n        ref={output}\n        aria-hidden=\"true\"\n        data-slot=\"number-ticker-value\"\n        className={cn(phase === \"play\" && \"[@media(scripting:enabled)]:invisible\")}\n      >\n        {finalText}\n      </span>\n    </span>\n  );\n}\n\nexport { NumberTicker, type NumberTickerProps };\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}