{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spec-numeral-tile","type":"registry:component","title":"Spec Numeral Tile","description":"A big bold numeral in a spec-sheet voice whose digits roll into place like an odometer, set in a hairline bento tile.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/spec-numeral-tile.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\n\n/**\n * SpecNumeral / SpecNumeralTile — a big numeral in a spec-sheet voice, whose\n * digits roll into place like an odometer the first time it scrolls into view.\n *\n * Every digit is its own column of 0–9 that rolls to its value in 650ms;\n * the columns start 45ms apart from the right, so the right-most lands first\n * and the most significant digit last, and a six-digit figure has settled\n * inside 900ms. It reads as landing rather than ticking. Separators, the prefix and the unit stay put, and the digits\n * are tabular, so the width never jumps while it counts. The real value is\n * in the text for assistive tech and search; the rolling columns are\n * decoration.\n *\n * The numeral is set in the page's own grotesque, bold, with tabular figures,\n * the way keynote recap slides and live dashboards set their numbers; mono\n * is kept for the small metadata label, never the figure itself (a mono\n * headline number reads as a terminal, not a spec sheet).\n *\n * SpecNumeralTile sets it the bento way: a tile on a near-black canvas with a\n * 1px hairline and no shadow, a mono metadata label, the numeral, its unit in\n * a lighter weight, and an optional delta chip in the one accent colour.\n *\n * Under prefers-reduced-motion the number simply appears. `replayKey` rolls it\n * again when it changes. The label's mono comes from --font-mono (or the\n * site's --font-geist-mono) with a system fallback.\n *\n * Needs Tailwind v4 (or v3.4+). No dependencies beyond React.\n */\n\nconst MONO = \"var(--font-mono, var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace))\";\nconst EASE = \"cubic-bezier(0.16, 1, 0.3, 1)\";\n\n/** \"#rrggbb\" if valid, otherwise the fallback. Never interpolated unchecked. */\nfunction safeHex(hex: string | undefined, fallback: string): string {\n  return /^#[0-9a-f]{6}$/i.test(hex || \"\") ? (hex as string) : fallback;\n}\n\nfunction format(value: number, decimals: number, group: boolean): string {\n  const v = Number.isFinite(value) ? value : 0;\n  return v.toLocaleString(\"en-US\", {\n    minimumFractionDigits: decimals,\n    maximumFractionDigits: decimals,\n    useGrouping: group,\n  });\n}\n\nexport function SpecNumeral({\n  value,\n  decimals = 0,\n  group = true,\n  prefix = \"\",\n  unit,\n  duration = 650,\n  replayKey,\n  className = \"\",\n  style,\n}: {\n  value: number;\n  decimals?: number;\n  /** Thousands separators. */\n  group?: boolean;\n  /** Before the digits, e.g. \"$\" or \"+\". */\n  prefix?: string;\n  /** After the digits, in a lighter weight, e.g. \"ms\", \"%\", \"×\". */\n  unit?: string;\n  /** Roll time of each column, in ms (they start 45ms apart). */\n  duration?: number;\n  /** Change it to roll the number again. */\n  replayKey?: string | number;\n  className?: string;\n  style?: CSSProperties;\n}) {\n  const ref = useRef<HTMLSpanElement>(null);\n  const [shown, setShown] = useState(false);\n  const [armedKey, setArmedKey] = useState(replayKey);\n  // A new replayKey resets to zero; the observer below rolls it again.\n  if (armedKey !== replayKey) {\n    setArmedKey(replayKey);\n    setShown(false);\n  }\n  const text = format(value, decimals, group);\n\n  useEffect(() => {\n    const el = ref.current;\n    if (!el || shown) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      const t = window.setTimeout(() => setShown(true), 0);\n      return () => window.clearTimeout(t);\n    }\n    let t = 0;\n    const io = new IntersectionObserver(\n      (entries) => {\n        if (entries[entries.length - 1].isIntersecting) {\n          io.disconnect();\n          // One frame at zero first, so the columns have somewhere to roll from.\n          t = window.setTimeout(() => setShown(true), 40);\n        }\n      },\n      { threshold: 0.4 }\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      window.clearTimeout(t);\n    };\n  }, [shown, armedKey]);\n\n  const chars = text.split(\"\");\n  const digitCount = chars.filter((c) => c >= \"0\" && c <= \"9\").length;\n  let seen = 0;\n\n  return (\n    <span\n      ref={ref}\n      className={\"inline-flex items-baseline leading-none \" + className}\n      style={{ fontVariantNumeric: \"tabular-nums\", letterSpacing: \"-0.045em\", ...style }}\n    >\n      <span className=\"sr-only\">{prefix + text + (unit ? \" \" + unit : \"\")}</span>\n      <span aria-hidden className=\"inline-flex items-baseline\">\n        {prefix && <span>{prefix}</span>}\n        {chars.map((c, i) => {\n          if (c < \"0\" || c > \"9\") return <span key={\"p\" + (chars.length - 1 - i)}>{c}</span>;\n          const d = Number(c);\n          // The right-most digit starts first; the most significant lands last.\n          const order = seen++;\n          const delay = (digitCount - 1 - order) * 45;\n          return (\n            // Keyed by place value from the right, so a column keeps its\n            // identity when the number grows or shrinks by a digit.\n            <span key={\"p\" + (chars.length - 1 - i)} className=\"relative inline-block overflow-hidden\" style={{ height: \"1em\", lineHeight: 1 }}>\n              <span className=\"invisible\">0</span>\n              <span\n                className=\"absolute left-0 top-0 flex flex-col motion-reduce:!transition-none\"\n                style={{\n                  transform: \"translateY(\" + (shown ? -d : 0) + \"em)\",\n                  transition: shown ? \"transform \" + duration + \"ms \" + EASE + \" \" + delay + \"ms\" : \"none\",\n                }}\n              >\n                {\"0123456789\".split(\"\").map((n) => (\n                  <span key={n} style={{ height: \"1em\", lineHeight: 1 }}>\n                    {n}\n                  </span>\n                ))}\n              </span>\n            </span>\n          );\n        })}\n        {unit && (\n          <span className=\"ml-[0.12em] font-normal\" style={{ fontSize: \"0.42em\", letterSpacing: \"0\", opacity: 0.6 }}>\n            {unit}\n          </span>\n        )}\n      </span>\n    </span>\n  );\n}\n\nexport function SpecNumeralTile({\n  label,\n  value,\n  decimals,\n  prefix,\n  unit,\n  caption,\n  delta,\n  accent = \"#ff5a1f\",\n  size = \"md\",\n  replayKey,\n  className = \"\",\n}: {\n  /** Mono metadata label, e.g. \"Latency · p95\". */\n  label: string;\n  value: number;\n  decimals?: number;\n  prefix?: string;\n  unit?: string;\n  caption?: ReactNode;\n  /** A short change note in the accent, e.g. \"−38% vs 2025\". */\n  delta?: string;\n  /** The one accent colour, \"#rrggbb\". */\n  accent?: string;\n  /** \"md\" for a 1x1 tile, \"lg\" for the 2x2 hero tile. */\n  size?: \"md\" | \"lg\";\n  replayKey?: string | number;\n  className?: string;\n}) {\n  const a = safeHex(accent, \"#ff5a1f\");\n  const lg = size === \"lg\";\n  return (\n    <div\n      className={\"@container relative flex h-full flex-col justify-between overflow-hidden rounded-[20px] text-white \" + (lg ? \"p-6 \" : \"p-4 \") + className}\n      style={{ background: \"#0f1012\", boxShadow: \"inset 0 0 0 1px rgba(255,255,255,0.08)\" }}\n    >\n      <div className=\"flex items-start justify-between gap-3\">\n        <p className=\"text-[11px] uppercase leading-[16px] tracking-[0.12em]\" style={{ fontFamily: MONO, color: \"rgba(255,255,255,0.5)\" }}>\n          {label}\n        </p>\n        {delta && (\n          <span\n            className=\"shrink-0 rounded-full px-2 text-[11px] font-medium leading-[18px]\"\n            style={{ fontFamily: MONO, color: a, background: a + \"1f\", boxShadow: \"inset 0 0 0 1px \" + a + \"40\" }}\n          >\n            {delta}\n          </span>\n        )}\n      </div>\n      <div>\n        <SpecNumeral\n          value={value}\n          decimals={decimals}\n          prefix={prefix}\n          unit={unit}\n          replayKey={replayKey}\n          className=\"font-bold\"\n          // Sized to the tile (container units), so any value fits.\n          style={{ fontSize: lg ? \"clamp(48px, 24cqw, 112px)\" : \"clamp(28px, 22cqw, 44px)\" }}\n        />\n        {caption && (\n          <p className=\"mt-3 text-[13px]\" style={{ color: \"rgba(255,255,255,0.6)\" }}>\n            {caption}\n          </p>\n        )}\n      </div>\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/spec-numeral-tile.tsx"}],"meta":{"tier":"free","tags":["numeral","count up","odometer","stat","bento","tile","tabular figures"],"href":"https://viberdy.dev/library/spec-numeral-tile","aiPrompt":"Create a React + Tailwind client component file exporting SpecNumeral and SpecNumeralTile: a big numeral in a bento spec-sheet voice whose digits roll into place like an odometer the first time it scrolls into view.\n\nSpecNumeral props: value (number), decimals (default 0), group (thousands separators, default true), prefix, unit, duration (ms per column, default 650, so a six-digit figure settles inside 900ms), replayKey (changing it rolls the number again), className, style. Format with toLocaleString(\"en-US\") (a non-finite value becomes 0). Render a visually hidden span with the full text (prefix, number, unit) for assistive tech, and an aria-hidden visual: the prefix, then one element per character. Separators stay static; each digit is an inline-block, 1em tall, overflow hidden, holding an invisible \"0\" for width and an absolutely positioned column of the digits 0–9 (each 1em tall, line-height 1). Rolling: translateY(−digit em) with a transition of `duration` ms on cubic-bezier(0.16, 1, 0.3, 1), delayed 45ms per position counted from the right, so the right-most digit lands first and the most significant digit lands last. Key each column by its place value from the right so it keeps its identity when the digit count changes. Add motion-reduce:!transition-none to the column so reduced motion never rolls. Before it rolls, every column sits at 0 with no transition. The unit follows in a lighter weight at 0.42em and 60% opacity. The numeral is set in the page grotesque (not mono: a mono headline number reads as a terminal), bold, with tabular numbers, letter-spacing −0.045em and line-height 1. Mono (var(--font-mono, var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace))) is for the metadata label only.\n\nTrigger: an IntersectionObserver (threshold 0.4, read the last entry) that, once the numeral is in view, disconnects and flips a `shown` state 40ms later so the columns roll from zero. Under prefers-reduced-motion, show it at once. Reset on a new replayKey by adjusting state during render (compare against the last key), which re-arms the observer.\n\nSpecNumeralTile props: label (a mono metadata label), value, decimals, prefix, unit, caption?, delta? (a short change note), accent (\"#rrggbb\", regex-validated, default #ff5a1f), size (\"md\" for a 1x1 tile, \"lg\" for the 2x2 hero tile), replayKey, className. The tile is the bento kit's: a Tailwind `@container`, rounded 20px, background #0f1012, a 1px hairline as inset 0 0 0 1px rgba(255,255,255,0.08), no drop shadow, padding 16px (24px for lg), a flex column spaced between. Top row: the label (11px, uppercase, +0.12em, mono, white 50%) and the delta chip (mono 11px in the accent, the accent at 12% behind it and a 25% accent hairline). Bottom: the numeral (bold) sized to the tile with container units (lg clamp(48px, 24cqw, 112px); md clamp(28px, 22cqw, 44px)) so any value fits, and an optional caption (13px, white 60%).\n\nOutput one self-contained \"use client\" TSX file with no dependencies beyond React. Build strings with + rather than template literals.","license":"https://viberdy.dev/license"}}