{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"number-ticker","type":"registry:block","title":"Number ticker","description":"A large metric whose digits roll into place when scrolled into view, with switchable metrics, a delta and a replay control.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/number-ticker/number-ticker.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useRef, useState } from \"react\";\nimport {\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { ArrowDownRight, ArrowUpRight, RotateCcw } 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 NumberTickerMetric = {\n  id: string;\n  label: string;\n  value: number;\n  prefix?: string;\n  suffix?: string;\n  /** Decimals shown on the value. Defaults to 0. */\n  decimals?: number;\n  /** Signed change vs the previous period, e.g. 12.4 for +12.4%. */\n  delta?: number;\n  /** Context after the delta. */\n  note?: string;\n};\n\nexport type NumberTickerProps = {\n  className?: string;\n  title?: string;\n  metrics?: NumberTickerMetric[];\n  /** Metric selected on first render. Defaults to the first one. */\n  defaultMetricId?: string;\n  /** Seconds for the digits to settle. Defaults to 1.2. */\n  duration?: number;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_METRICS: NumberTickerMetric[] = [\n  {\n    id: \"mrr\",\n    label: \"MRR\",\n    value: 84260,\n    prefix: \"$\",\n    delta: 12.4,\n    note: \"vs. the previous 30 days\",\n  },\n  {\n    id: \"arr\",\n    label: \"ARR\",\n    value: 1011120,\n    prefix: \"$\",\n    delta: 18.2,\n    note: \"annualised run rate\",\n  },\n  {\n    id: \"customers\",\n    label: \"Customers\",\n    value: 2847,\n    delta: 4.8,\n    note: \"active accounts\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\nfunction formatValue(metric: NumberTickerMetric) {\n  const decimals = Math.max(0, metric.decimals ?? 0);\n  const number = metric.value.toLocaleString(\"en-US\", {\n    minimumFractionDigits: decimals,\n    maximumFractionDigits: decimals,\n  });\n  return `${metric.prefix ?? \"\"}${number}${metric.suffix ?? \"\"}`;\n}\n\nfunction formatDelta(delta: number) {\n  const sign = delta > 0 ? \"+\" : delta < 0 ? \"−\" : \"\";\n  return `${sign}${Math.abs(delta).toFixed(1)}%`;\n}\n\n/* ------------------------------------------------------------------ */\n/* one digit wheel: rolls through two loops and lands on the target    */\n/* ------------------------------------------------------------------ */\n\nfunction DigitWheel({\n  digit,\n  show,\n  reduce,\n  delay,\n  duration,\n}: {\n  digit: number;\n  show: boolean;\n  reduce: boolean;\n  delay: number;\n  duration: number;\n}) {\n  const count = 20 + digit + 1;\n  const digits = Array.from({ length: count }, (_, index) => index % 10);\n  /* one step = one digit = 1/count of the strip height */\n  const target = `${(-((count - 1) / count) * 100).toFixed(4)}%`;\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        {digit}\n      </span>\n    );\n  }\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      <motion.span\n        className=\"absolute inset-x-0 top-0 flex flex-col will-change-transform\"\n        initial={{ y: \"0%\" }}\n        animate={{ y: show ? target : \"0%\" }}\n        transition={\n          show\n            ? { duration, delay, ease: EASE }\n            : { duration: 0 }\n        }\n      >\n        {digits.map((value, index) => (\n          <span\n            key={index}\n            className=\"flex h-[1em] w-full items-center justify-center leading-none\"\n          >\n            {value}\n          </span>\n        ))}\n      </motion.span>\n    </span>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* the block                                                           */\n/* ------------------------------------------------------------------ */\n\nexport function NumberTicker({\n  className,\n  title = \"Revenue\",\n  metrics = DEFAULT_METRICS,\n  defaultMetricId,\n  duration = 1.2,\n}: NumberTickerProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const pillId = useId();\n\n  const safeMetrics = metrics.length > 0 ? metrics : DEFAULT_METRICS;\n  const [activeId, setActiveId] = useState(() => {\n    const initial =\n      safeMetrics.find((metric) => metric.id === defaultMetricId) ??\n      safeMetrics[0];\n    return initial?.id ?? \"\";\n  });\n  const [replayKey, setReplayKey] = useState(0);\n\n  const metric =\n    safeMetrics.find((item) => item.id === activeId) ?? safeMetrics[0];\n  const formatted = formatValue(metric);\n  const characters = formatted.split(\"\");\n  const show = inView;\n  const safeDuration = Math.max(0.2, duration);\n\n  const delta = metric.delta ?? 0;\n  const positive = delta > 0;\n  const neutral = delta === 0;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={ref}\n        initial={reduce ? false : { opacity: 0, y: 14 }}\n        animate={\n          reduce || show ? { opacity: 1, y: 0 } : { opacity: 0, y: 14 }\n        }\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        className={cn(\"w-full text-foreground\", className)}\n      >\n        <section className=\"rounded-xl border border-border bg-card p-4 text-card-foreground sm:p-6\">\n          <div className=\"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between\">\n            <h2 className=\"text-[15px] font-medium tracking-[-0.01em] text-foreground\">\n              {title}\n            </h2>\n\n            <div\n              role=\"group\"\n              aria-label=\"Choose a metric\"\n              className=\"inline-flex w-fit items-center rounded-lg border border-border bg-muted/50 p-0.5\"\n            >\n              {safeMetrics.map((item) => {\n                const active = item.id === metric.id;\n                return (\n                  <button\n                    key={item.id}\n                    type=\"button\"\n                    onClick={() => setActiveId(item.id)}\n                    aria-pressed={active}\n                    className={cn(\n                      \"relative rounded-[6px] px-2.5 py-1 text-[11.5px] font-medium transition-colors duration-200\",\n                      \"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n                      active\n                        ? \"text-foreground\"\n                        : \"text-muted-foreground hover:text-foreground\"\n                    )}\n                  >\n                    {active ? (\n                      <motion.span\n                        layoutId={`number-ticker-pill-${pillId}`}\n                        className=\"absolute inset-0 rounded-[6px] border border-border bg-card\"\n                        transition={{ duration: reduce ? 0 : 0.35, ease: EASE }}\n                      />\n                    ) : null}\n                    <span className=\"relative\">{item.label}</span>\n                  </button>\n                );\n              })}\n            </div>\n          </div>\n\n          <div\n            role=\"img\"\n            aria-label={`${metric.label}: ${formatted}`}\n            className=\"mt-5 flex min-h-[1em] items-center overflow-hidden sm:mt-6\"\n          >\n            <span\n              aria-hidden=\"true\"\n              className=\"flex items-center text-[40px] font-semibold leading-none tracking-[-0.03em] text-foreground sm:text-[56px] md:text-[64px]\"\n            >\n              {characters.map((character, index) => {\n                if (/\\d/.test(character)) {\n                  return (\n                    <DigitWheel\n                      key={`${metric.id}-${replayKey}-${index}`}\n                      digit={Number(character)}\n                      show={show}\n                      reduce={reduce}\n                      delay={index * 0.035}\n                      duration={safeDuration}\n                    />\n                  );\n                }\n\n                if (character === \",\" || character === \".\") {\n                  return (\n                    <span\n                      key={`${metric.id}-${replayKey}-${index}`}\n                      className={cn(\n                        \"self-end pb-[0.12em] text-[0.9em] tabular-nums\",\n                        character === \",\"\n                          ? \"text-foreground/70\"\n                          : \"text-foreground\"\n                      )}\n                    >\n                      {character}\n                    </span>\n                  );\n                }\n\n                return (\n                  <span\n                    key={`${metric.id}-${replayKey}-${index}`}\n                    className=\"inline-flex h-[1em] items-center tabular-nums\"\n                  >\n                    {character}\n                  </span>\n                );\n              })}\n            </span>\n          </div>\n\n          <div className=\"mt-4 flex flex-wrap items-center justify-between gap-3 border-t border-border pt-3.5\">\n            <div className=\"flex flex-wrap items-center gap-x-2 gap-y-1 text-[12px]\">\n              <span\n                className={cn(\n                  \"inline-flex items-center gap-0.5 font-medium tabular-nums\",\n                  neutral\n                    ? \"text-muted-foreground\"\n                    : positive\n                      ? \"text-success\"\n                      : \"text-destructive\"\n                )}\n              >\n                {neutral ? null : positive ? (\n                  <ArrowUpRight className=\"h-3 w-3\" aria-hidden=\"true\" />\n                ) : (\n                  <ArrowDownRight className=\"h-3 w-3\" aria-hidden=\"true\" />\n                )}\n                {formatDelta(delta)}\n              </span>\n              {metric.note ? (\n                <span className=\"text-muted-foreground\">{metric.note}</span>\n              ) : null}\n            </div>\n\n            {reduce ? null : (\n              <button\n                type=\"button\"\n                onClick={() => setReplayKey((value) => value + 1)}\n                className=\"inline-flex items-center gap-1.5 rounded-md px-1.5 py-1 text-[11.5px] font-medium text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n              >\n                <RotateCcw className=\"h-3 w-3\" aria-hidden=\"true\" />\n                Replay\n              </button>\n            )}\n          </div>\n        </section>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default NumberTicker;\n"}]}