{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"live-demo-counter-that-ticks-up-now-and-then","type":"registry:component","title":"Live Demo Counter That Ticks Up","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/live-demo-counter-that-ticks-up-now-and-then.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// Demo only: a made-up refill shop's count that a timer nudges up. Wire `bump` to real data in your app.\nconst START = 1284;\nconst INTRO_FROM = 1180; // the first count runs up from here\nconst INTRO_MS = 1000;\nconst MIN_GAP = 1800; // ms between ticks, randomised up to MAX_GAP\nconst MAX_GAP = 4200;\n\nconst fmt = new Intl.NumberFormat(\"en-US\");\nconst css = `\n@keyframes lvc-in{from{transform:translateY(55%);opacity:0;filter:blur(2px)}to{transform:none;opacity:1;filter:none}}\n@keyframes lvc-chip{0%{transform:translateY(6px);opacity:0}15%{transform:none;opacity:1}75%{opacity:1}100%{transform:translateY(-8px);opacity:0}}\n@keyframes lvc-dot{0%,100%{opacity:.35}50%{opacity:1}}\n.lvc-digit{display:inline-block;animation:lvc-in .42s cubic-bezier(.2,.8,.2,1) both}\n.lvc-chip{animation:lvc-chip 1.6s ease-out both}\n.lvc-dot{animation:lvc-dot 2.4s ease-in-out infinite}\n[data-paused=\"true\"] .lvc-dot{animation-play-state:paused}\n@media (prefers-reduced-motion: reduce){.lvc-digit,.lvc-chip,.lvc-dot{animation:none}}\n`;\n\nexport default function LiveDemoCounter() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const [count, setCount] = useState(START);\n  const [last, setLast] = useState({ by: 0, n: 0 });\n  const [paused, setPaused] = useState(false);\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) {\n      setCount(START);\n      return;\n    }\n    let raf = 0;\n    let timer = 0;\n    let value = START;\n    let introDone = false;\n    let started = 0;\n    setCount(INTRO_FROM);\n    const intro = (t: number) => {\n      if (!started) started = t;\n      const p = Math.min(1, (t - started) / INTRO_MS);\n      setCount(Math.round(INTRO_FROM + (START - INTRO_FROM) * (1 - Math.pow(1 - p, 3))));\n      if (p < 1) raf = requestAnimationFrame(intro);\n      else {\n        introDone = true;\n        queue(600);\n      }\n    };\n    const bump = () => {\n      const by = 1 + Math.floor(Math.random() * 3);\n      value += by;\n      setCount(value);\n      setLast((l) => ({ by, n: l.n + 1 }));\n      queue(MIN_GAP + Math.random() * (MAX_GAP - MIN_GAP));\n    };\n    const queue = (ms: number) => {\n      window.clearTimeout(timer);\n      timer = window.setTimeout(bump, ms);\n    };\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        const on = entry.isIntersecting;\n        setPaused(!on);\n        window.clearTimeout(timer);\n        cancelAnimationFrame(raf);\n        if (!on) return;\n        if (introDone) queue(MIN_GAP);\n        else {\n          started = 0;\n          raf = requestAnimationFrame(intro);\n        }\n      },\n      { threshold: 0.3 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      window.clearTimeout(timer);\n      cancelAnimationFrame(raf);\n    };\n  }, [reduce]);\n\n  const text = fmt.format(count);\n  const width = fmt.format(START + 9000).length; // room for growth without the layout moving\n  return (\n    <section ref={rootRef} data-paused={paused} className=\"relative overflow-hidden bg-[#0d1624] px-5 py-16 text-[#e8eef4] sm:py-20\">\n      <style>{css}</style>\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0\"\n        style={{ background: \"radial-gradient(70% 60% at 50% 110%, rgba(84,170,160,.22), rgba(13,22,36,0) 70%)\" }}\n      />\n      <div className=\"relative mx-auto max-w-xl\">\n        <div className=\"rounded-2xl border border-white/10 bg-white/[0.03] p-6 sm:p-8\">\n          <div className=\"flex items-center justify-between gap-4\">\n            <p className=\"text-sm font-medium text-[#a8bccc]\">Bramble Refill Shop</p>\n            <span className=\"inline-flex items-center gap-2 rounded-full border border-[#7fd1c3]/30 px-3 py-1 text-xs font-medium text-[#9fe0d4]\">\n              <span aria-hidden=\"true\" className=\"lvc-dot h-1.5 w-1.5 rounded-full bg-[#7fd1c3]\" />\n              Demo\n            </span>\n          </div>\n          <h2 className=\"mt-6 text-base font-medium text-[#c9d6e2]\">Jars refilled today</h2>\n          <div className=\"mt-2 flex items-end gap-3\">\n            <p className=\"text-6xl font-semibold leading-none tracking-tight tabular-nums sm:text-7xl\">\n              <span className=\"sr-only\">{fmt.format(Math.max(count, START))} jars, a sample figure</span>\n              <span aria-hidden=\"true\" className=\"inline-flex overflow-hidden py-1\" style={{ minWidth: `${width * 0.62}em` }}>\n                {text.split(\"\").map((c, i) => (\n                  <span key={`${i}-${c}-${text.length}`} className={/\\d/.test(c) ? \"lvc-digit\" : \"text-[#6f8597]\"}>\n                    {c}\n                  </span>\n                ))}\n              </span>\n            </p>\n            {last.n > 0 && (\n              <span key={last.n} aria-hidden=\"true\" className=\"lvc-chip mb-2 rounded-md bg-[#7fd1c3]/15 px-2 py-0.5 text-sm font-medium tabular-nums text-[#9fe0d4]\">\n                +{last.by}\n              </span>\n            )}\n          </div>\n          <p className=\"mt-6 border-t border-white/10 pt-5 text-sm leading-relaxed text-[#a8bccc]\">\n            Demo counter with sample numbers: a timer adds one to three every few seconds. In your app, call the same update when a real event arrives.\n          </p>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
