{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"number-drift","type":"registry:component","title":"Number Drift","description":"Only the digits that changed need to move.","dependencies":[],"categories":["motion","numbers","digits","metrics","Intl"],"files":[{"path":"components/lab/number-drift/number-drift.tsx","target":"components/lab/number-drift/number-drift.tsx","type":"registry:component","content":"\"use client\";\n\n/*\n * The previous formatted string is a render-time diff snapshot. It is\n * intentionally a ref: the roll needs the old and new digits in the same\n * commit, and promoting it to state would render the new value once\n * without animation before the snapshot could be read.\n */\n/* eslint-disable react-hooks/refs */\n\nimport { useEffect, useMemo, useRef, type CSSProperties } from \"react\";\nimport \"./number-drift.css\";\n\nexport type NumberDriftProps = {\n  value: number;\n  locale?: string;\n  format?: Intl.NumberFormatOptions;\n  prefix?: string;\n  suffix?: string;\n  className?: string;\n};\n\nfunction sequence(\n  previous: number | null,\n  next: number,\n  direction: number,\n): { items: number[]; from: number; to: number; duration: number } {\n  if (previous === null || previous === next || direction === 0) {\n    return { items: [next], from: 0, to: 0, duration: 240 };\n  }\n  if (direction > 0) {\n    const items = [previous];\n    let digit = previous;\n    while (digit !== next) {\n      digit = (digit + 1) % 10;\n      items.push(digit);\n    }\n    return {\n      items,\n      from: 0,\n      to: -(items.length - 1),\n      duration: Math.min(420, 220 + items.length * 36),\n    };\n  }\n  const items = [next];\n  let digit = next;\n  while (digit !== previous) {\n    digit = (digit + 1) % 10;\n    items.push(digit);\n  }\n  return {\n    items,\n    from: -(items.length - 1),\n    to: 0,\n    duration: Math.min(420, 220 + items.length * 36),\n  };\n}\n\nfunction DriftDigit({\n  previous,\n  digit,\n  direction,\n}: {\n  previous: number | null;\n  digit: number;\n  direction: number;\n}) {\n  const built = sequence(previous, digit, direction);\n  const style = {\n    \"--nd-from\": `${built.from}em`,\n    \"--nd-to\": `${built.to}em`,\n    \"--nd-duration\": `${built.duration}ms`,\n  } as CSSProperties;\n\n  return (\n    <span className=\"nd-digit\">\n      <span className=\"nd-track\" style={style}>\n        {built.items.map((value, index) => (\n          <span className=\"nd-cell\" key={`${value}-${index}`}>\n            {value}\n          </span>\n        ))}\n      </span>\n    </span>\n  );\n}\n\n/**\n * NUMBER DRIFT\n *\n * Formats with Intl.NumberFormat, then aligns the result from the right\n * so separators and signs keep their place while digits roll. Each\n * changed digit runs a short directional strip; unchanged digits keep\n * their DOM and do not move. The visual is aria-hidden and the final\n * formatted value is exposed once, so assistive technology never hears\n * animation frames.\n */\nexport function NumberDrift({\n  value,\n  locale,\n  format,\n  prefix,\n  suffix,\n  className,\n}: NumberDriftProps) {\n  const formatKey = format ? JSON.stringify(format) : \"\";\n  const formatter = useMemo(\n    () => new Intl.NumberFormat(locale, format),\n    // The serialised options are the stable identity of the formatter.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n    [locale, formatKey],\n  );\n  const text = formatter.format(Number.isFinite(value) ? value : 0);\n  const previous = useRef({ text, value });\n  const old = previous.current;\n  const direction = value > old.value ? 1 : value < old.value ? -1 : 0;\n\n  useEffect(() => {\n    previous.current = { text, value };\n  }, [text, value]);\n\n  const oldChars = Array.from(old.text);\n  const chars = Array.from(text);\n  const visual = chars.map((char, index) => {\n    const fromRight = chars.length - index;\n    const oldIndex = oldChars.length - fromRight;\n    const oldChar = oldIndex >= 0 ? oldChars[oldIndex] : \"\";\n    if (/\\d/.test(char)) {\n      const nextDigit = Number(char);\n      const previousDigit = /\\d/.test(oldChar) ? Number(oldChar) : null;\n      const changed = previousDigit !== nextDigit;\n      if (!changed) {\n        return (\n          <span className=\"nd-static nd-static--digit\" key={`d-${fromRight}`}>\n            {char}\n          </span>\n        );\n      }\n      return (\n        <DriftDigit\n          key={`d-${fromRight}-${old.text}-${text}-${oldChar}`}\n          previous={previousDigit}\n          digit={nextDigit}\n          direction={direction}\n        />\n      );\n    }\n    return (\n      <span className=\"nd-static\" key={`s-${index}-${char}`}>\n        {char}\n      </span>\n    );\n  });\n\n  const accessible = `${prefix ?? \"\"}${text}${suffix ?? \"\"}`;\n\n  return (\n    <span\n      className={`nd-root${className ? ` ${className}` : \"\"}`}\n      data-changing={old.text !== text ? \"true\" : \"false\"}\n    >\n      <span className=\"nd-visual\" aria-hidden=\"true\">\n        {prefix ? <span className=\"nd-affix\">{prefix}</span> : null}\n        {visual}\n        {suffix ? <span className=\"nd-affix\">{suffix}</span> : null}\n      </span>\n      <span className=\"nd-sr\">{accessible}</span>\n    </span>\n  );\n}\n"},{"path":"components/lab/number-drift/number-drift.css","target":"components/lab/number-drift/number-drift.css","type":"registry:file","content":".nd-root {\n  display: inline-flex;\n  align-items: baseline;\n  min-width: 0;\n  color: inherit;\n  font: inherit;\n  font-variant-numeric: tabular-nums;\n  font-feature-settings: \"tnum\" 1;\n  white-space: nowrap;\n}\n\n.nd-visual {\n  display: inline-flex;\n  align-items: center;\n  line-height: 1;\n}\n\n.nd-static,\n.nd-affix {\n  display: inline-block;\n  line-height: 1;\n}\n\n.nd-static--digit {\n  width: 1ch;\n  text-align: center;\n}\n\n.nd-affix {\n  white-space: pre;\n}\n\n.nd-digit {\n  position: relative;\n  display: inline-block;\n  width: 1ch;\n  height: 1em;\n  overflow: hidden;\n  vertical-align: baseline;\n}\n\n.nd-track {\n  display: flex;\n  flex-direction: column;\n  animation: nd-roll var(--nd-duration, 280ms)\n    cubic-bezier(0.22, 1, 0.36, 1) both;\n  will-change: transform;\n}\n\n.nd-cell {\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  height: 1em;\n  line-height: 1;\n}\n\n.nd-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  padding: 0;\n  margin: -1px;\n  overflow: hidden;\n  clip: rect(0 0 0 0);\n  white-space: nowrap;\n  border: 0;\n}\n\n@keyframes nd-roll {\n  from {\n    transform: translate3d(0, var(--nd-from, 0em), 0);\n  }\n  to {\n    transform: translate3d(0, var(--nd-to, 0em), 0);\n  }\n}\n\n@media (prefers-reduced-motion: reduce) {\n  .nd-track {\n    animation: none;\n    transform: translate3d(0, var(--nd-to, 0em), 0);\n  }\n}\n"}]}