{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bakery-stats-row-that-counts-up-with-an-ease-out","type":"registry:component","title":"Bakery Stats Row That Counts Up","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/bakery-stats-row-that-counts-up-with-an-ease-out.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// Sample figures for a made-up bakery. Edit the values and labels here.\nconst stats = [\n  { value: 18240, decimals: 0, label: \"Loaves baked this year\" },\n  { value: 1312, decimals: 0, label: \"Regulars on the stamp card\" },\n  { value: 4.9, decimals: 1, label: \"Average review, out of 5\" },\n  { value: 12, decimals: 0, label: \"Years on Fennel Street\" },\n];\nconst DURATION = 1400; // ms each number takes to land\nconst STAGGER = 140; // ms between one number starting and the next\n\nconst easeOut = (t: number) => 1 - Math.pow(1 - t, 3);\nconst formatters = stats.map(\n  (s) => new Intl.NumberFormat(\"en-US\", { minimumFractionDigits: s.decimals, maximumFractionDigits: s.decimals }),\n);\nconst fmt = (i: number, v: number) =>\n  formatters[i].format(stats[i].decimals ? v : Math.round(v));\n\nexport default function BakeryStatsRowCounter() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const [progress, setProgress] = useState<number[]>(stats.map(() => 1));\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) {\n      setProgress(stats.map(() => 1));\n      return;\n    }\n    setProgress(stats.map(() => 0));\n    let raf = 0;\n    let started = 0;\n    let elapsed = 0;\n    let visible = false;\n    const tick = (now: number) => {\n      if (!started) started = now - elapsed;\n      elapsed = now - started;\n      const next = stats.map((_, i) => Math.min(1, Math.max(0, (elapsed - i * STAGGER) / DURATION)));\n      setProgress(next);\n      if (next.some((p) => p < 1) && visible) raf = requestAnimationFrame(tick);\n    };\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        visible = entry.isIntersecting;\n        cancelAnimationFrame(raf);\n        started = 0;\n        if (visible) raf = requestAnimationFrame(tick);\n      },\n      { threshold: 0.25 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, [reduce]);\n\n  return (\n    <section\n      ref={rootRef}\n      className=\"relative overflow-hidden bg-[#f4ede1] px-5 py-16 text-[#2a1f16] sm:px-10 sm:py-20\"\n    >\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 opacity-[0.35] mix-blend-multiply\"\n        style={{\n          backgroundImage:\n            \"url(\\\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .45 0 0 0 0 .36 0 0 0 0 .25 0 0 0 .22 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E\\\")\",\n        }}\n      />\n      <div className=\"relative mx-auto max-w-5xl\">\n        <p className=\"text-sm font-medium uppercase tracking-[0.18em] text-[#8a4a24]\">Fennel Street Bakehouse</p>\n        <h2 className=\"mt-3 max-w-xl font-serif text-3xl leading-tight sm:text-4xl\">Twelve years of early mornings, counted.</h2>\n        <dl className=\"mt-12 grid grid-cols-2 gap-x-6 gap-y-10 lg:grid-cols-4\">\n          {stats.map((s, i) => {\n            const p = progress[i];\n            return (\n              <div key={s.label} className=\"relative flex flex-col pt-5\">\n                <dt className=\"order-2 mt-2 text-sm leading-snug text-[#5e4d3c]\">\n                  <span aria-hidden=\"true\" className=\"absolute inset-x-0 top-0 h-px bg-[#2a1f16]/15\" />\n                  <span\n                    aria-hidden=\"true\"\n                    className=\"absolute left-0 top-0 h-[2px] w-full origin-left bg-[#b4532a]\"\n                    style={{ transform: `scaleX(${easeOut(p)})` }}\n                  />\n                  {s.label}\n                </dt>\n                <dd className=\"order-1 font-serif text-4xl tabular-nums lining-nums sm:text-5xl\">\n                  <span className=\"sr-only\">{fmt(i, s.value)}</span>\n                  <span aria-hidden=\"true\" className=\"grid\">\n                    <span className=\"invisible col-start-1 row-start-1\">{fmt(i, s.value)}</span>\n                    <span className=\"col-start-1 row-start-1\">{fmt(i, s.value * easeOut(p))}</span>\n                  </span>\n                </dd>\n              </div>\n            );\n          })}\n        </dl>\n        <p className=\"mt-12 text-xs text-[#5e4d3c]\">Sample numbers for a made-up bakery, shown to demonstrate the counter.</p>\n      </div>\n    </section>\n  );\n}\n"}]}
