{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"animated-counter","type":"registry:ui","title":"Animated counter","description":"Number that counts up when scrolled into view, with prefix, suffix, decimals and Intl formatting.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/items/animated-counter/animated-counter.tsx","type":"registry:ui","target":"components/facade/animated-counter.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { animate, useInView, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface AnimatedCounterProps extends Omit<React.HTMLAttributes<HTMLSpanElement>, \"children\"> {\n  /** Target value. */\n  value: number;\n  /** Starting value. Defaults to 0. */\n  from?: number;\n  /** Text before the number, e.g. \"$\". */\n  prefix?: string;\n  /** Text after the number, e.g. \"+\", \"M\", \"%\". */\n  suffix?: string;\n  /** Fixed number of decimals. Defaults to 0. */\n  decimals?: number;\n  /** BCP 47 locale for Intl.NumberFormat. Defaults to \"en-US\". */\n  locale?: string;\n  /** Extra Intl.NumberFormat options (merged after decimals). */\n  formatOptions?: Intl.NumberFormatOptions;\n  /** Animation duration in seconds. Defaults to 1.8. */\n  duration?: number;\n  /** Delay in seconds once in view. Defaults to 0. */\n  delay?: number;\n  /** Animate only the first time it enters the viewport. Defaults to true. */\n  once?: boolean;\n  className?: string;\n}\n\nexport function AnimatedCounter({\n  value,\n  from = 0,\n  prefix = \"\",\n  suffix = \"\",\n  decimals = 0,\n  locale = \"en-US\",\n  formatOptions,\n  duration = 1.8,\n  delay = 0,\n  once = true,\n  className,\n  \"aria-label\": ariaLabel,\n  ...rest\n}: AnimatedCounterProps) {\n  const ref = React.useRef<HTMLSpanElement>(null);\n  const inView = useInView(ref, { once, margin: \"0px 0px -10% 0px\" });\n  const reduce = useReducedMotion();\n\n  // Compare `formatOptions` by value so an inline object does not restart the animation on every parent render.\n  const formatOptionsKey = JSON.stringify(formatOptions ?? null);\n  const formatter = React.useMemo(() => {\n    const extra = JSON.parse(formatOptionsKey) as Intl.NumberFormatOptions | null;\n    return new Intl.NumberFormat(locale, {\n      minimumFractionDigits: decimals,\n      maximumFractionDigits: decimals,\n      ...extra,\n    });\n  }, [locale, decimals, formatOptionsKey]);\n\n  const format = React.useCallback((n: number) => `${prefix}${formatter.format(n)}${suffix}`, [formatter, prefix, suffix]);\n\n  React.useEffect(() => {\n    const node = ref.current;\n    if (!node) return;\n    if (!inView) {\n      if (!once) node.textContent = format(from);\n      return;\n    }\n    if (reduce) {\n      node.textContent = format(value);\n      return;\n    }\n    const controls = animate(from, value, {\n      duration,\n      delay,\n      ease: [0.16, 1, 0.3, 1],\n      onUpdate: (v) => {\n        node.textContent = format(v);\n      },\n    });\n    return () => controls.stop();\n  }, [inView, from, value, duration, delay, reduce, once, format]);\n\n  return (\n    <span {...rest} className={cn(\"tabular-nums\", className)}>\n      {/* Screen readers get the final value; the animated text is decorative. */}\n      <span className=\"sr-only\">{ariaLabel ?? format(value)}</span>\n      <span ref={ref} aria-hidden>\n        {format(from)}\n      </span>\n    </span>\n  );\n}\n\nexport default AnimatedCounter;\n"}],"categories":["data"],"meta":{"tier":"free","demo":"import { Star } from \"lucide-react\";\nimport { AnimatedCounter } from \"./animated-counter\";\n\ninterface Stat {\n  value: number;\n  prefix?: string;\n  suffix?: string;\n  decimals?: number;\n  label: string;\n  note: string;\n  star?: boolean;\n}\n\nconst stats: Stat[] = [\n  { value: 2.4, prefix: \"$\", suffix: \"M\", decimals: 1, label: \"Saved for homeowners\", note: \"Since 2019\" },\n  { value: 1240, suffix: \"+\", label: \"Homes serviced\", note: \"Across the Austin metro\" },\n  { value: 4.9, decimals: 1, label: \"Average rating\", note: \"386 Google reviews\", star: true },\n  { value: 45, suffix: \" min\", label: \"Average response\", note: \"Seven days a week\" },\n];\n\nexport default function Demo() {\n  return (\n    <section className=\"w-full max-w-3xl\">\n      <p className=\"text-[13px] font-medium uppercase tracking-[0.08em] text-muted-foreground\">Lone Star Plumbing Co.</p>\n      <h3 className=\"mt-2 font-display text-[28px] font-semibold tracking-[-0.03em] sm:text-[34px]\">Numbers our neighbors trust</h3>\n      <div className=\"mt-8 grid grid-cols-2 gap-3 lg:grid-cols-4\">\n        {stats.map((s) => (\n          <div key={s.label} className=\"flex flex-col rounded-[24px] bg-muted p-5 sm:p-6\">\n            <span className=\"flex items-baseline gap-1.5\">\n              <AnimatedCounter\n                value={s.value}\n                prefix={s.prefix}\n                suffix={s.suffix}\n                decimals={s.decimals}\n                className=\"font-display text-[32px] font-semibold tracking-[-0.03em] sm:text-[36px]\"\n              />\n              {s.star && <Star className=\"size-4 translate-y-[-2px] fill-star text-star\" aria-hidden />}\n            </span>\n            <span className=\"mt-4 text-sm font-medium\">{s.label}</span>\n            <span className=\"mt-0.5 text-[13px] text-muted-foreground\">{s.note}</span>\n          </div>\n        ))}\n      </div>\n    </section>\n  );\n}\n"}}