{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "count-up",
  "type": "registry:ui",
  "title": "Count Up",
  "description": "A number that counts to its value the first time it scrolls into view: \"120 sources\", \"4.8k readers\", \"98%\". The figure is the content, so the component is built around reading it, not around the motion.",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "data",
    "effect"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/effects/count-up.tsx",
      "target": "components/ui/effects/count-up.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/count-up v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/count-up\n// What changed since: https://ds.interlace.tools/c/count-up#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — CountUp\n *\n * A number that counts to its value the first time it scrolls into view:\n * \"120 sources\", \"4.8k readers\", \"98%\". The figure is the content, so the\n * component is built around reading it, not around the motion.\n *\n * ## The number is always there\n *\n * The server renders the final value, so a page without scripts, a crawler\n * and a screen reader all get the real figure. On the client, before the\n * first paint, the visible digits drop to `from` and wait; when the number\n * enters the viewport they count up. Assistive tech reads one stable label\n * (the final value, in an `sr-only` span) and never the moving digits, which\n * are `aria-hidden`.\n *\n * ## No layout shift\n *\n * The final value is laid out invisibly in the same grid cell as the moving\n * one, so the box is as wide as the final figure from the start, and digits\n * use `tabular-nums` so they do not jitter while they change.\n *\n * ## Formatting\n *\n * `format` takes any `Intl.NumberFormatOptions` (decimals, compact notation,\n * percent, currency, units) and `locale` picks the locale. Every frame is\n * formatted with the same formatter, so \"4.8k\" counts as \"1.2k, 2.5k…\" and\n * never shows a raw \"4812.3\".\n *\n * ## Motion\n *\n * Under `prefers-reduced-motion: reduce` the final value shows at once.\n * Colour is inherited, so the number takes the colour of the text around it.\n *\n * | Rule | Concept                 | Where in this file                                   |\n * | ---- | ----------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el       | `span` props                                          |\n * | R6   | data-slot per part      | count-up / -label / -digits                           |\n * | R7   | cn + ...rest            | `cn(…, className)` + `{...props}`                     |\n * | R8   | Enums, not booleans     | `start`                                               |\n * | R19  | Tokens only             | inherits `currentColor`; no colour of its own          |\n * | R25  | Client component        | the count runs in the browser                         |\n * | R26  | A11y                    | one stable label; moving digits hidden                |\n */\n\nimport { cn } from '@/lib/utils';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\n\nexport interface CountUpProps extends Omit<React.ComponentPropsWithoutRef<'span'>, 'children'> {\n  /** The number to show. */\n  value: number;\n  /** Where the count starts. @default 0 */\n  from?: number;\n  /** Length of the count, in milliseconds. @default 1200 */\n  duration?: number;\n  /** How to write the number: any `Intl.NumberFormat` options. @default {} */\n  format?: Intl.NumberFormatOptions;\n  /** Locale for the formatter. @default the browser's */\n  locale?: string | string[];\n  /** Count when the number scrolls into view, or as soon as it mounts. @default 'visible' */\n  start?: 'visible' | 'mount';\n}\n\n/** Fast at first, settling into the final value. */\nconst easeOutCubic = (t: number) => 1 - (1 - t) ** 3;\n\nexport function CountUp({\n  value,\n  from = 0,\n  duration = 1200,\n  format,\n  locale,\n  start = 'visible',\n  className,\n  ...props\n}: CountUpProps) {\n  const reduced = useReducedMotion();\n  const rootRef = React.useRef<HTMLSpanElement>(null);\n  const digitsRef = React.useRef<HTMLSpanElement>(null);\n\n  // Keyed by value, so a new options object each render does not restart the count.\n  const localeKey = Array.isArray(locale) ? locale.join(',') : (locale ?? '');\n  const formatKey = JSON.stringify(format ?? {});\n  const formatter = React.useMemo(\n    () =>\n      new Intl.NumberFormat(\n        localeKey ? localeKey.split(',') : undefined,\n        JSON.parse(formatKey) as Intl.NumberFormatOptions,\n      ),\n    [localeKey, formatKey],\n  );\n  const finalText = formatter.format(value);\n\n  // Before paint: drop the visible digits to the starting value.\n  React.useLayoutEffect(() => {\n    const digits = digitsRef.current;\n    if (!digits || reduced) return;\n    digits.textContent = formatter.format(from);\n  }, [formatter, from, reduced]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const digits = digitsRef.current;\n    if (!root || !digits) return;\n    if (reduced || duration <= 0) {\n      digits.textContent = finalText;\n      return;\n    }\n\n    let frame = 0;\n    let started = 0;\n    const tick = (now: number) => {\n      if (!started) started = now;\n      const t = Math.min((now - started) / duration, 1);\n      digits.textContent = formatter.format(from + (value - from) * easeOutCubic(t));\n      if (t < 1) frame = requestAnimationFrame(tick);\n      else digits.textContent = finalText;\n    };\n    const run = () => {\n      frame = requestAnimationFrame(tick);\n    };\n\n    if (start === 'mount' || typeof IntersectionObserver === 'undefined') {\n      run();\n      return () => cancelAnimationFrame(frame);\n    }\n\n    const io = new IntersectionObserver(\n      (entries) => {\n        if (entries.some((e) => e.isIntersecting)) {\n          io.disconnect();\n          run();\n        }\n      },\n      { threshold: 0.5 },\n    );\n    io.observe(root);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(frame);\n    };\n  }, [formatter, finalText, from, value, duration, start, reduced]);\n\n  return (\n    <span\n      ref={rootRef}\n      data-slot=\"count-up\"\n      className={cn('inline-grid tabular-nums', className)}\n      {...props}\n    >\n      <span data-slot=\"count-up-label\" className=\"sr-only\">\n        {finalText}\n      </span>\n      <span aria-hidden className=\"invisible col-start-1 row-start-1\">\n        {finalText}\n      </span>\n      <span\n        ref={digitsRef}\n        aria-hidden\n        data-slot=\"count-up-digits\"\n        className=\"col-start-1 row-start-1 text-end\"\n      >\n        {finalText}\n      </span>\n    </span>\n  );\n}\n"
    }
  ],
  "meta": {
    "tier": "effect",
    "client": true,
    "minViewport": null,
    "loading": false,
    "version": "1.0.0",
    "since": null
  },
  "docs": "## @interlace/count-up\n\nInstalled to `components/ui/effects/count-up.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/effects/count-up';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/count-up\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
