{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"split-flap-number-counter-with-hinged-digit-leaves","type":"registry:component","title":"Split-Flap Number Counter","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/split-flap-number-counter-with-hinged-digit-leaves.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// Sample figures for a made-up restaurant. Each row gets one flap per digit.\nconst rows = [\n  { label: \"Covers served this week\", value: 1486 },\n  { label: \"Tables turned\", value: 312 },\n  { label: \"Oysters shucked\", value: 2040 },\n];\nconst STEP = 170; // ms between flips; each leaf takes most of this to fall\n\nconst fmt = new Intl.NumberFormat(\"en-US\");\nconst css = `\n.sfc-card{position:relative;display:inline-block;width:.92em;height:1.36em;perspective:3.5em;border-radius:.09em;box-shadow:0 .06em .12em rgba(0,0,0,.55),inset 0 0 0 1px rgba(255,255,255,.04)}\n.sfc-half{position:absolute;left:0;right:0;height:50%;overflow:hidden;background:#24282e;color:#efe9dc;backface-visibility:hidden}\n.sfc-top{top:0;border-radius:.09em .09em 0 0;background:linear-gradient(180deg,#2b3037,#24282e);transform-origin:50% 100%}\n.sfc-bot{bottom:0;border-radius:0 0 .09em .09em;background:linear-gradient(180deg,#1f2328,#262a30);transform-origin:50% 0}\n.sfc-face{position:absolute;left:0;right:0;height:200%;display:flex;align-items:center;justify-content:center}\n.sfc-top .sfc-face{top:0}.sfc-bot .sfc-face{top:-100%}\n.sfc-fall{z-index:2;animation:sfc-fall var(--sfc-d) cubic-bezier(.55,0,.9,.4) both}\n.sfc-rise{z-index:2;animation:sfc-rise var(--sfc-d) cubic-bezier(.2,.7,.35,1) calc(var(--sfc-d)*.92) both}\n.sfc-fall::after{content:\"\";position:absolute;inset:0;background:#000;animation:sfc-dim var(--sfc-d) ease-in both}\n.sfc-seam{position:absolute;left:0;right:0;top:50%;height:2px;margin-top:-1px;background:rgba(0,0,0,.7);z-index:3}\n.sfc-pin{position:absolute;top:50%;width:.06em;height:.16em;margin-top:-.08em;background:#0b0d10;z-index:3;border-radius:1px}\n@keyframes sfc-fall{from{transform:rotateX(0)}to{transform:rotateX(-90deg)}}\n@keyframes sfc-rise{from{transform:rotateX(90deg)}to{transform:rotateX(0)}}\n@keyframes sfc-dim{from{opacity:0}to{opacity:.45}}\n@media (prefers-reduced-motion: reduce){.sfc-fall,.sfc-rise{display:none}}\n`;\n\nfunction Flap({ digit }: { digit: string }) {\n  const [s, setS] = useState({ prev: digit, cur: digit, n: 0 });\n  useEffect(() => {\n    setS((o) => (o.cur === digit ? o : { prev: o.cur, cur: digit, n: o.n + 1 }));\n  }, [digit]);\n  const half = (cls: string, ch: string, key?: string) => (\n    <span key={key} className={`sfc-half ${cls}`}>\n      <span className=\"sfc-face\">{ch}</span>\n    </span>\n  );\n  return (\n    <span className=\"sfc-card\" style={{ [\"--sfc-d\" as string]: `${STEP * 0.45}ms` }}>\n      {half(\"sfc-top\", s.cur)}\n      {half(\"sfc-bot\", s.prev)}\n      {s.n > 0 && half(\"sfc-top sfc-fall\", s.prev, `f${s.n}`)}\n      {s.n > 0 && half(\"sfc-bot sfc-rise\", s.cur, `r${s.n}`)}\n      <span className=\"sfc-seam\" />\n      <span className=\"sfc-pin left-0\" />\n      <span className=\"sfc-pin right-0\" />\n    </span>\n  );\n}\n\nexport default function SplitFlapNumberCounter() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const finals = rows.map((r) => String(r.value).split(\"\").map(Number));\n  const [digits, setDigits] = useState<number[][]>(finals);\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) {\n      setDigits(finals);\n      return;\n    }\n    let current = finals.map((d) => d.map(() => 0));\n    setDigits(current);\n    let timer = 0;\n    const step = () => {\n      let moving = false;\n      current = current.map((row, r) =>\n        row.map((d, i) => {\n          if (d < finals[r][i]) {\n            moving = true;\n            return d + 1;\n          }\n          return d;\n        }),\n      );\n      setDigits(current);\n      if (!moving) window.clearInterval(timer);\n    };\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        window.clearInterval(timer);\n        if (entry.isIntersecting) timer = window.setInterval(step, STEP);\n      },\n      { threshold: 0.3 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      window.clearInterval(timer);\n    };\n    // finals is derived from the constant rows array\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"bg-[#0f1318] px-5 py-16 text-[#efe9dc] sm:py-20\">\n      <style>{css}</style>\n      <div className=\"mx-auto max-w-3xl\">\n        <p className=\"text-sm font-medium uppercase tracking-[0.2em] text-[#e0b860]\">Harbour Kitchen, Pier 4</p>\n        <h2 className=\"mt-3 text-3xl font-semibold tracking-tight sm:text-4xl\">The week on the pass</h2>\n        <div className=\"mt-10 rounded-xl border border-white/5 bg-[#161a20] p-4 shadow-[inset_0_2px_12px_rgba(0,0,0,.6)] sm:p-6\">\n          <dl className=\"divide-y divide-white/5\">\n            {rows.map((r, ri) => {\n              const groups = fmt.format(r.value);\n              let k = 0;\n              return (\n                <div key={r.label} className=\"flex flex-col gap-3 py-4 first:pt-0 last:pb-0 sm:flex-row sm:items-center sm:justify-between\">\n                  <dt className=\"text-sm uppercase tracking-[0.14em] text-[#a9b0b9]\">{r.label}</dt>\n                  <dd className=\"font-sans text-[2.6rem] font-semibold leading-none tabular-nums sm:text-6xl\">\n                    <span className=\"sr-only\">{groups}</span>\n                    <span aria-hidden=\"true\" className=\"flex items-end gap-[0.07em]\">\n                      {groups.split(\"\").map((ch, ci) =>\n                        /\\d/.test(ch) ? (\n                          <Flap key={ci} digit={String(digits[ri][k++])} />\n                        ) : (\n                          <span key={ci} className=\"w-[0.22em] pb-[0.08em] text-center text-[0.55em] text-[#6f7782]\">{ch}</span>\n                        ),\n                      )}\n                    </span>\n                  </dd>\n                </div>\n              );\n            })}\n          </dl>\n        </div>\n        <p className=\"mt-6 text-sm text-[#a9b0b9]\">Sample numbers for a made-up restaurant. Each leaf flips one step at a time, the way a station board does.</p>\n      </div>\n    </section>\n  );\n}\n"}]}
