{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rolling-number","type":"registry:block","title":"Rolling number","description":"A credit balance whose digits roll up or down with the direction of the change, driven by a stepper and tied to a live usage bar.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/rolling-number/rolling-number.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { Minus, Plus } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type RollingNumberProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  /** Starting value. Defaults to 13,860. */\n  defaultValue?: number;\n  /** Smallest value the stepper can reach. Defaults to 0. */\n  min?: number;\n  /** Largest value the stepper can reach. Defaults to 25,000. */\n  max?: number;\n  /** Amount added or removed by the stepper. Defaults to 500. */\n  step?: number;\n  /** Text after the number. Defaults to \"credits\". */\n  unit?: string;\n  /** Called after every successful change. */\n  onChange?: (value: number) => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\nfunction clamp(value: number, min: number, max: number) {\n  return Math.min(max, Math.max(min, value));\n}\n\nfunction formatNumber(value: number) {\n  return Math.round(value).toLocaleString(\"en-US\");\n}\n\n/* ------------------------------------------------------------------ */\n/* one rolling digit slot                                              */\n/* ------------------------------------------------------------------ */\n\nfunction Digit({\n  char,\n  index,\n  direction,\n  rollKey,\n  reduce,\n  animateIn,\n}: {\n  char: string;\n  index: number;\n  direction: 1 | -1;\n  rollKey: number;\n  reduce: boolean;\n  animateIn: boolean;\n}) {\n  if (!/\\d/.test(char)) {\n    return (\n      <span aria-hidden=\"true\" className=\"inline-block tabular-nums\">\n        {char}\n      </span>\n    );\n  }\n\n  if (reduce) {\n    return (\n      <span\n        aria-hidden=\"true\"\n        className=\"inline-flex h-[1em] w-[0.62em] items-center justify-center tabular-nums\"\n      >\n        {char}\n      </span>\n    );\n  }\n\n  /* the outgoing glyph leaves the way the value is moving, the incoming one\n     arrives from the opposite edge: a small physical wheel per column */\n  const enter = direction === 1 ? \"115%\" : \"-115%\";\n  const leave = direction === 1 ? \"-115%\" : \"115%\";\n\n  return (\n    <span\n      aria-hidden=\"true\"\n      className=\"relative inline-block h-[1em] w-[0.62em] overflow-hidden align-baseline tabular-nums\"\n    >\n      <AnimatePresence initial={false} mode=\"popLayout\">\n        <motion.span\n          key={`${char}-${rollKey}`}\n          className=\"absolute inset-0 flex items-center justify-center leading-none will-change-transform\"\n          initial={animateIn ? { y: enter } : false}\n          animate={{ y: \"0%\" }}\n          exit={{ y: leave }}\n          transition={{\n            duration: 0.52,\n            ease: EASE,\n            delay: Math.min(index * 0.025, 0.12),\n          }}\n        >\n          {char}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function RollingNumber({\n  className,\n  title = \"API credits\",\n  description = \"Monthly allowance, resets in 12 days\",\n  defaultValue = 13860,\n  min = 0,\n  max = 25000,\n  step = 500,\n  unit = \"credits\",\n  onChange,\n}: RollingNumberProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n\n  const [value, setValue] = useState(() => clamp(defaultValue, min, max));\n  const [direction, setDirection] = useState<1 | -1>(1);\n  const [rollKey, setRollKey] = useState(0);\n  const revealed = useRef(false);\n\n  /* roll every column once when the card enters the viewport */\n  useEffect(() => {\n    if (!inView || revealed.current || reduce) return;\n    revealed.current = true;\n    setDirection(1);\n    setRollKey((key) => key + 1);\n  }, [inView, reduce]);\n\n  const used = Math.max(0, max - value);\n  const usedPct = max > 0 ? clamp((used / max) * 100, 0, 100) : 0;\n  const characters = formatNumber(value).split(\"\");\n  const disabledDown = value <= min;\n  const disabledUp = value >= max;\n\n  const update = (next: number) => {\n    const clamped = clamp(next, min, max);\n    if (clamped === value) return;\n    setDirection(clamped > value ? 1 : -1);\n    setValue(clamped);\n    setRollKey((key) => key + 1);\n    onChange?.(clamped);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={ref}\n        initial={reduce ? false : { opacity: 0, y: 16 }}\n        animate={inView || reduce ? { opacity: 1, y: 0 } : { opacity: 0, y: 16 }}\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card p-5 text-card-foreground sm:p-6\",\n          className\n        )}\n      >\n        <div className=\"flex flex-wrap items-start justify-between gap-3\">\n          <div className=\"min-w-0\">\n            <h2 className=\"text-[15px] font-semibold tracking-tight\">{title}</h2>\n            <p className=\"mt-0.5 text-[13px] text-muted-foreground\">{description}</p>\n          </div>\n\n          <span className=\"shrink-0 rounded-full border border-border px-2.5 py-1 text-[11.5px] font-medium tabular-nums text-muted-foreground\">\n            {Math.round(usedPct)}% used\n          </span>\n        </div>\n\n        <div className=\"mt-7 flex flex-col gap-5 sm:flex-row sm:items-end sm:justify-between\">\n          <div>\n            <div\n              role=\"status\"\n              aria-live=\"polite\"\n              aria-label={`${formatNumber(value)} ${unit} left`}\n              className=\"flex flex-wrap items-baseline gap-x-2 gap-y-1\"\n            >\n              <span\n                aria-hidden=\"true\"\n                className=\"inline-flex items-baseline text-[44px] font-semibold leading-none tracking-[-0.04em] text-foreground sm:text-[56px]\"\n              >\n                {characters.map((character, index) => (\n                  <Digit\n                    key={index}\n                    char={character}\n                    index={index}\n                    direction={direction}\n                    rollKey={rollKey}\n                    reduce={reduce}\n                    animateIn={inView}\n                  />\n                ))}\n              </span>\n              <span className=\"text-[12.5px] font-medium text-muted-foreground\">\n                {unit} left\n              </span>\n            </div>\n          </div>\n\n          <div\n            role=\"group\"\n            aria-label=\"Adjust credit balance\"\n            className=\"flex items-center gap-2\"\n          >\n            <button\n              type=\"button\"\n              onClick={() => update(value - step)}\n              disabled={disabledDown}\n              aria-label={`Remove ${formatNumber(step)} credits`}\n              className=\"inline-flex h-9 w-9 items-center justify-center rounded-lg border border-border bg-background text-foreground transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:pointer-events-none disabled:opacity-40 motion-reduce:transition-none\"\n            >\n              <Minus aria-hidden=\"true\" className=\"h-4 w-4\" />\n            </button>\n            <button\n              type=\"button\"\n              onClick={() => update(value + step)}\n              disabled={disabledUp}\n              aria-label={`Add ${formatNumber(step)} credits`}\n              className=\"inline-flex h-9 w-9 items-center justify-center rounded-lg border border-border bg-foreground text-background transition-colors hover:bg-foreground/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:pointer-events-none disabled:opacity-40 motion-reduce:transition-none\"\n            >\n              <Plus aria-hidden=\"true\" className=\"h-4 w-4\" />\n            </button>\n          </div>\n        </div>\n\n        <div className=\"mt-7 border-t border-border pt-4\">\n          <div className=\"flex items-center justify-between gap-3 text-[12px]\">\n            <span className=\"text-muted-foreground\">\n              <span className=\"tabular-nums text-foreground\">{formatNumber(used)}</span> of{\" \"}\n              <span className=\"tabular-nums text-foreground\">{formatNumber(max)}</span> used\n            </span>\n            <span className=\"tabular-nums text-muted-foreground\">\n              {formatNumber(value)} left\n            </span>\n          </div>\n\n          <div\n            role=\"progressbar\"\n            aria-label=\"Credits used this cycle\"\n            aria-valuemin={0}\n            aria-valuemax={100}\n            aria-valuenow={Math.round(usedPct)}\n            aria-valuetext={`${Math.round(usedPct)}% used`}\n            className=\"relative mt-2.5 h-1.5 w-full rounded-full bg-muted\"\n          >\n            <motion.span\n              aria-hidden=\"true\"\n              className=\"absolute inset-0 rounded-full bg-foreground/80\"\n              style={{ transformOrigin: \"0% 50%\" }}\n              initial={{ scaleX: reduce ? usedPct / 100 : 0 }}\n              animate={{ scaleX: inView || reduce ? usedPct / 100 : 0 }}\n              transition={{ duration: reduce ? 0 : 0.7, ease: EASE }}\n            />\n            <span\n              aria-hidden=\"true\"\n              className=\"absolute top-1/2 h-2.5 w-px -translate-y-1/2 bg-border\"\n              style={{ left: \"80%\" }}\n            />\n            <span\n              aria-hidden=\"true\"\n              className=\"absolute top-1/2 h-1 w-1 -translate-x-1/2 -translate-y-1/2 rounded-full bg-background ring-1 ring-foreground/70\"\n              style={{ left: `${usedPct}%` }}\n            />\n          </div>\n\n          <p className=\"mt-2 text-[11px] text-muted-foreground\">\n            We email you when the balance falls below 20%.\n          </p>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default RollingNumber;\n"}]}