{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rolling-odometer-number-counter-with-digit-drums","type":"registry:block","title":"Rolling Odometer Number Counter","description":"Free rolling odometer number counter: digit drums that carry like a real meter, start when scrolled into view and keep the final figure readable. React or HTML.","dependencies":["framer-motion","lucide-react"],"files":[{"path":"components/we-inc/rolling-odometer-number-counter-with-digit-drums.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\nimport { RotateCcw } from \"lucide-react\";\n\n// Sample figure for a made-up courier. VALUE is shown to one decimal place on the last (tenths) drum.\nconst VALUE = 48306.4;\nconst WHOLE_DRUMS = 5; // drums before the decimal point\nconst DURATION = 2400; // ms for the full roll\nconst CELL = 1.3; // em, height of one digit window\n\nconst easeOut = (t: number) => 1 - Math.pow(1 - t, 4);\nconst full = new Intl.NumberFormat(\"en-US\", { minimumFractionDigits: 1, maximumFractionDigits: 1 });\nconst group = new Intl.NumberFormat(\"en-US\").formatToParts(1000).find((p) => p.type === \"group\")?.value ?? \",\";\n\n/** Position of each drum, tenths first. Higher drums only turn while every drum below is passing 9 to 0. */\nfunction drumPositions(value: number, count: number) {\n  const ticks = Math.round(value * 10 * 1e6) / 1e6;\n  const out: number[] = [];\n  for (let p = 0; p < count; p++) {\n    const unit = Math.pow(10, p);\n    const digit = Math.floor(ticks / unit) % 10;\n    if (p === 0) {\n      out.push(ticks % 10);\n      continue;\n    }\n    const below = ticks % unit;\n    const carry = Math.max(0, below - (unit - 1));\n    out.push(digit + carry);\n  }\n  return out;\n}\n\nfunction Drum({ pos, light }: { pos: number; light?: boolean }) {\n  return (\n    <span\n      className={`relative inline-block h-[1.3em] w-[0.86em] overflow-hidden rounded-[3px] ${light ? \"bg-[#ece6d8] text-[#1c1d20]\" : \"bg-[#141518] text-[#eee8da]\"}`}\n    >\n      <span className=\"absolute inset-x-0 top-0 block will-change-transform\" style={{ transform: `translateY(${-pos * CELL}em)` }}>\n        {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].map((d, i) => (\n          <span key={i} className=\"flex h-[1.3em] items-center justify-center\">{d}</span>\n        ))}\n      </span>\n      <span\n        className=\"pointer-events-none absolute inset-0\"\n        style={{\n          background: light\n            ? \"linear-gradient(180deg, rgba(40,30,10,.55), rgba(40,30,10,0) 32%, rgba(255,255,255,.18) 48%, rgba(40,30,10,0) 64%, rgba(40,30,10,.55))\"\n            : \"linear-gradient(180deg, rgba(0,0,0,.85), rgba(0,0,0,0) 34%, rgba(255,255,255,.07) 48%, rgba(0,0,0,0) 62%, rgba(0,0,0,.85))\",\n        }}\n      />\n    </span>\n  );\n}\n\nexport default function RollingOdometerCounter() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const [shown, setShown] = useState(VALUE);\n  const runRef = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) {\n      setShown(VALUE);\n      return;\n    }\n    let raf = 0;\n    let start = 0;\n    let elapsed = 0;\n    let visible = false;\n    let done = false;\n    const tick = (now: number) => {\n      if (!start) start = now - elapsed;\n      elapsed = now - start;\n      const p = Math.min(1, elapsed / DURATION);\n      setShown(VALUE * easeOut(p));\n      done = p >= 1;\n      if (!done && visible) raf = requestAnimationFrame(tick);\n    };\n    runRef.current = () => {\n      cancelAnimationFrame(raf);\n      elapsed = 0;\n      start = 0;\n      done = false;\n      if (visible) raf = requestAnimationFrame(tick);\n    };\n    setShown(0);\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        visible = entry.isIntersecting;\n        cancelAnimationFrame(raf);\n        start = 0;\n        if (visible && !done) raf = requestAnimationFrame(tick);\n      },\n      { threshold: 0.3 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, [reduce]);\n\n  const replay = useCallback(() => runRef.current(), []);\n  const pos = drumPositions(shown, WHOLE_DRUMS + 1).reverse();\n  const whole = pos.slice(0, WHOLE_DRUMS);\n\n  return (\n    <section ref={rootRef} className=\"relative overflow-hidden bg-[#202226] px-5 py-16 text-[#eee8da] sm:py-20\">\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0\"\n        style={{ background: \"radial-gradient(80% 60% at 50% 0%, rgba(214,180,120,.14), rgba(0,0,0,0) 70%)\" }}\n      />\n      <div className=\"relative mx-auto flex max-w-3xl flex-col items-center text-center\">\n        <p className=\"text-sm font-medium uppercase tracking-[0.2em] text-[#d6b478]\">Cobble Lane Couriers</p>\n        <h2 className=\"mt-3 text-3xl font-semibold tracking-tight sm:text-4xl\">Miles ridden by bike this year</h2>\n\n        <div className=\"mt-10 rounded-2xl bg-gradient-to-b from-[#c9a76a] via-[#8c6b37] to-[#c19b5c] p-[3px] shadow-[0_24px_60px_-20px_rgba(0,0,0,.8)]\">\n          <div className=\"rounded-[14px] bg-gradient-to-b from-[#2c2e33] to-[#17181b] px-3 py-4 sm:px-5 sm:py-5\">\n            <p className=\"font-mono text-[clamp(2.4rem,9vw,4.5rem)] font-semibold leading-none\">\n              <span className=\"sr-only\">{full.format(VALUE)} miles</span>\n              <span aria-hidden=\"true\" className=\"flex items-center gap-[0.08em] tabular-nums\">\n                {whole.map((p, i) => (\n                  <span key={i} className=\"flex items-center gap-[0.08em]\">\n                    {i === WHOLE_DRUMS - 3 && <span className=\"w-[0.3em] self-end pb-[0.1em] text-center text-[0.6em] text-[#8f8a80]\">{group}</span>}\n                    <Drum pos={p} />\n                  </span>\n                ))}\n                <span className=\"w-[0.25em] self-end pb-[0.12em] text-center text-[0.6em] text-[#8f8a80]\">.</span>\n                <Drum pos={pos[WHOLE_DRUMS]} light />\n              </span>\n            </p>\n          </div>\n        </div>\n\n        <p className=\"mt-8 max-w-md text-sm leading-relaxed text-[#b9b3a7]\">\n          Sample figure for a made-up courier. Each drum only turns while the one to its right passes nine.\n        </p>\n        <button\n          type=\"button\"\n          onClick={replay}\n          className=\"mt-6 inline-flex h-10 items-center gap-2 rounded-full border border-[#d6b478]/40 px-4 text-sm text-[#eee8da] transition hover:bg-white/5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#d6b478]\"\n        >\n          <RotateCcw aria-hidden=\"true\" className=\"h-4 w-4\" />\n          Roll it again\n        </button>\n      </div>\n    </section>\n  );\n}\n"}]}
