{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"currency-total-counter-ticking-dollars-and-cents","type":"registry:component","title":"Currency Total Counter With Cents","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/currency-total-counter-ticking-dollars-and-cents.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// Sample takings for a made-up soap maker. The total is the sum of the lines.\nconst lines = [\n  { label: \"Bars and liquid soap\", amount: 6412.8 },\n  { label: \"Gift boxes\", amount: 3958.25 },\n  { label: \"Wholesale to two shops\", amount: 2475.3 },\n];\nconst CURRENCY = \"USD\";\nconst DURATION = 1700; // ms for the total to land\nconst total = lines.reduce((s, l) => s + l.amount, 0);\n\nconst money = new Intl.NumberFormat(\"en-US\", { style: \"currency\", currency: CURRENCY });\nconst easeOut = (t: number) => 1 - Math.pow(1 - t, 3);\nconst cents = (v: number) => Math.round(v * 100) / 100;\n\nfunction Amount({ value, final, big }: { value: number; final: number; big?: boolean }) {\n  const parts = money.formatToParts(cents(value));\n  return (\n    <>\n      <span className=\"sr-only\">{money.format(final)}</span>\n      <span aria-hidden=\"true\" className=\"inline-grid justify-items-end tabular-nums\">\n        <span className=\"invisible col-start-1 row-start-1\">{money.format(final)}</span>\n        <span className=\"col-start-1 row-start-1\">\n          {parts.map((p, i) =>\n            big && (p.type === \"fraction\" || p.type === \"decimal\") ? (\n              <span key={i} className=\"text-[0.55em] align-[0.62em] text-[#2f6b5b]\">{p.type === \"decimal\" ? \"\" : p.value}</span>\n            ) : (\n              <span key={i}>{p.value}</span>\n            ),\n          )}\n        </span>\n      </span>\n    </>\n  );\n}\n\nexport default function CurrencyTotalCounter() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const [p, setP] = useState(1);\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) {\n      setP(1);\n      return;\n    }\n    setP(0);\n    let raf = 0;\n    let start = 0;\n    let elapsed = 0;\n    let visible = false;\n    const tick = (now: number) => {\n      if (!start) start = now - elapsed;\n      elapsed = now - start;\n      const t = Math.min(1, elapsed / DURATION);\n      setP(t);\n      if (t < 1 && visible) raf = requestAnimationFrame(tick);\n    };\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        visible = entry.isIntersecting;\n        cancelAnimationFrame(raf);\n        start = 0;\n        if (visible) 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 e = easeOut(p);\n  return (\n    <section ref={rootRef} className=\"bg-[#dcebe4] px-5 py-16 text-[#163a31] sm:py-20\">\n      <div className=\"mx-auto grid max-w-5xl items-center gap-10 md:grid-cols-[1fr_minmax(0,420px)]\">\n        <div>\n          <p className=\"text-sm font-medium uppercase tracking-[0.18em] text-[#2f6b5b]\">Saltmarsh Soap Co.</p>\n          <h2 className=\"mt-3 text-3xl font-semibold leading-tight tracking-tight sm:text-4xl\">October takings, to the cent.</h2>\n          <p className=\"mt-4 max-w-sm text-base leading-relaxed text-[#2c4f45]\">\n            The cents run fastest and settle last, so the total feels counted rather than faded in. Sample figures for a made-up shop.\n          </p>\n        </div>\n        <div className=\"relative rounded-t-lg bg-[#fbfaf6] px-6 pb-10 pt-7 shadow-[0_30px_60px_-30px_rgba(22,58,49,.45),0_2px_6px_rgba(22,58,49,.08)] sm:px-8\">\n          <div className=\"flex items-baseline justify-between text-xs uppercase tracking-[0.16em] text-[#4f6b63]\">\n            <span>Statement</span>\n            <span>1 to 31 Oct</span>\n          </div>\n          <ul className=\"mt-5 divide-y divide-dashed divide-[#163a31]/20 font-mono text-sm\">\n            {lines.map((l) => (\n              <li key={l.label} className=\"flex items-baseline justify-between gap-4 py-3\">\n                <span className=\"font-sans text-[#2c4f45]\">{l.label}</span>\n                <span className=\"text-[#163a31]\">\n                  <Amount value={l.amount * e} final={l.amount} />\n                </span>\n              </li>\n            ))}\n          </ul>\n          <div className=\"mt-4 border-t-2 border-[#163a31] pt-4\">\n            <p className=\"text-xs uppercase tracking-[0.16em] text-[#4f6b63]\">Total received</p>\n            <p className=\"mt-1 text-right font-mono text-[clamp(2.2rem,8vw,3.25rem)] font-semibold leading-none tracking-tight\">\n              <Amount value={total * e} final={total} big />\n            </p>\n          </div>\n          <div\n            aria-hidden=\"true\"\n            className=\"absolute inset-x-0 -bottom-3 h-3\"\n            style={{\n              background: \"radial-gradient(circle at 8px 0, #fbfaf6 7px, transparent 7.5px) -8px 0 / 16px 12px repeat-x\",\n            }}\n          />\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
