{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rolling-counter","type":"registry:ui","title":"Rolling Counter","description":"An odometer counter whose digits roll the shortest way, with separators, decimals and negatives.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/rolling-counter.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\";\nimport { useEffect, type ReactNode } from \"react\";\n\ntype RollingCounterProps = {\n  value: number;\n  /** Font size in pixels. */\n  fontSize?: number;\n  /** Integer digits to show: a number pads with zeros, \"auto\" uses as many as needed. */\n  places?: number | \"auto\";\n  /** Digits after the decimal point. */\n  decimals?: number;\n  /** Thousands separator, such as \",\" or \" \". Empty for none. */\n  separator?: string;\n  decimalPoint?: string;\n  prefix?: ReactNode;\n  suffix?: ReactNode;\n  /** Roll duration per digit, in seconds. */\n  duration?: number;\n  /** Delay between neighbouring digits, in seconds, starting from the right. */\n  stagger?: number;\n  color?: string;\n  className?: string;\n};\n\ntype Token =\n  | { kind: \"digit\"; key: string; digit: number; order: number }\n  | { kind: \"text\"; key: string; text: string };\n\nconst STRIP = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0];\nconst ROW = 1.15;\n\n/** An odometer: each digit column rolls the shortest way to its new value, one after another. */\nexport function RollingCounter({\n  value,\n  fontSize = 48,\n  places = \"auto\",\n  decimals = 0,\n  separator = \"\",\n  decimalPoint = \".\",\n  prefix,\n  suffix,\n  duration = 0.8,\n  stagger = 0.04,\n  color = \"#f4ede3\",\n  className = \"\",\n}: RollingCounterProps) {\n  const reduce = useReducedMotion() ?? false;\n  const safe = Number.isFinite(value) ? value : 0;\n  const fixed = Math.abs(safe).toFixed(Math.max(0, decimals));\n  const [intPart, fracPart = \"\"] = fixed.split(\".\");\n  const negative = safe < 0 && Number(fixed) !== 0;\n  const intDigits = typeof places === \"number\" ? intPart.padStart(places, \"0\") : intPart;\n\n  // Tokens are keyed by place value, so a new leading digit grows in without disturbing the others.\n  const tokens: Token[] = [];\n  if (negative) tokens.push({ kind: \"text\", key: \"sign\", text: \"−\" });\n  intDigits.split(\"\").forEach((ch, i) => {\n    const place = intDigits.length - 1 - i;\n    tokens.push({ kind: \"digit\", key: `i${place}`, digit: Number(ch), order: place + fracPart.length });\n    if (separator && place > 0 && place % 3 === 0) tokens.push({ kind: \"text\", key: `s${place}`, text: separator });\n  });\n  if (fracPart) {\n    tokens.push({ kind: \"text\", key: \"point\", text: decimalPoint });\n    fracPart.split(\"\").forEach((ch, k) => {\n      tokens.push({ kind: \"digit\", key: `f${k}`, digit: Number(ch), order: fracPart.length - 1 - k });\n    });\n  }\n\n  const readable = `${negative ? \"-\" : \"\"}${intDigits.replace(/\\B(?=(\\d{3})+(?!\\d))/g, separator)}${fracPart ? decimalPoint + fracPart : \"\"}`;\n  const grow = reduce ? { duration: 0 } : { duration: 0.3, ease: [0.22, 1, 0.36, 1] as const };\n\n  return (\n    <span\n      className={`inline-flex items-baseline font-display font-black leading-none tabular-nums ${className}`}\n      style={{ fontSize, color }}\n    >\n      {prefix ? <span className=\"mr-[0.08em]\">{prefix}</span> : null}\n      <span className=\"sr-only\">{readable}</span>\n      <span aria-hidden=\"true\" className=\"inline-flex\">\n        <AnimatePresence initial={false}>\n          {tokens.map((t) =>\n            t.kind === \"digit\" ? (\n              <motion.span\n                key={t.key}\n                className=\"inline-block overflow-hidden\"\n                initial={{ width: 0, opacity: 0 }}\n                animate={{ width: \"auto\", opacity: 1 }}\n                exit={{ width: 0, opacity: 0 }}\n                transition={grow}\n              >\n                <Digit digit={t.digit} delay={reduce ? 0 : t.order * stagger} duration={duration} reduce={reduce} />\n              </motion.span>\n            ) : (\n              <motion.span\n                key={t.key}\n                className=\"inline-block overflow-hidden whitespace-pre\"\n                style={{ height: `${ROW}em`, lineHeight: `${ROW}em` }}\n                initial={{ width: 0, opacity: 0 }}\n                animate={{ width: \"auto\", opacity: 1 }}\n                exit={{ width: 0, opacity: 0 }}\n                transition={grow}\n              >\n                {t.text}\n              </motion.span>\n            ),\n          )}\n        </AnimatePresence>\n      </span>\n      {suffix ? <span className=\"ml-[0.08em]\">{suffix}</span> : null}\n    </span>\n  );\n}\n\nfunction Digit({ digit, delay, duration, reduce }: { digit: number; delay: number; duration: number; reduce: boolean }) {\n  // An unbounded position: 9 → 0 goes to 10, not back down through 8…1.\n  const pos = useMotionValue(digit);\n  const y = useTransform(pos, (p) => `${-((((p % 10) + 10) % 10) / STRIP.length) * 100}%`);\n\n  useEffect(() => {\n    const current = pos.get();\n    const from = ((current % 10) + 10) % 10;\n    let delta = digit - from;\n    if (delta > 5) delta -= 10;\n    else if (delta < -5) delta += 10;\n    if (Math.abs(delta) < 1e-6) return;\n    const controls = animate(pos, current + delta, reduce ? { duration: 0 } : { duration, delay, ease: [0.16, 1, 0.3, 1] });\n    return () => controls.stop();\n  }, [digit, delay, duration, reduce, pos]);\n\n  return (\n    <span className=\"relative block overflow-hidden\" style={{ height: `${ROW}em` }}>\n      <motion.span className=\"block\" style={{ y }}>\n        {STRIP.map((d, i) => (\n          <span key={i} className=\"block text-center\" style={{ height: `${ROW}em`, lineHeight: `${ROW}em` }}>\n            {d}\n          </span>\n        ))}\n      </motion.span>\n    </span>\n  );\n}\n"}]}