{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sticky-stacking-cards-that-scale-down-and-dim","type":"registry:block","title":"Sticky Stacking Cards That Scale Down and Dim","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/sticky-stacking-cards-that-scale-down-and-dim.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\nconst loaves = [\n  { name: \"Country white\", note: \"Long cold proof, blistered crust, a soft open crumb that holds butter.\", price: \"$9\", tags: [\"Bakes daily\", \"780 g\"], bg: \"#f7f1e6\", ink: \"#2b2119\", chip: \"#e6d9c3\" },\n  { name: \"Rye and caraway\", note: \"Dense, dark and a little sour. Slice it thin and it keeps for a week.\", price: \"$11\", tags: [\"Thu to Sat\", \"900 g\"], bg: \"#b4532e\", ink: \"#fff8f0\", chip: \"#93401f\" },\n  { name: \"Seeded spelt\", note: \"Toasted sunflower, linseed and pumpkin folded through a spelt dough.\", price: \"$10\", tags: [\"Bakes daily\", \"750 g\"], bg: \"#5f6f4f\", ink: \"#f6f3ea\", chip: \"#4b5a3d\" },\n  { name: \"Olive fougasse\", note: \"Leaf-cut flatbread with green olives and thyme, best still warm.\", price: \"$7\", tags: [\"Weekends\", \"Tear and share\"], bg: \"#2b2420\", ink: \"#f3ebe0\", chip: \"#463b33\" },\n];\nconst SCALE_STEP = 0.06; // how much smaller each covering card makes the ones beneath\nconst DIM_STEP = 0.24; // how much darker each covering card makes the ones beneath\nconst AUTO_PX_PER_SEC = 28; // gentle demo scroll speed; 0 turns it off\n\nexport default function StickyStackingCardsThatScaleDownAndDim() {\n  const reduce = useReducedMotion();\n  const boxRef = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    const box = boxRef.current;\n    if (!box || reduce) return;\n    const cards = Array.from(box.querySelectorAll<HTMLElement>(\".w4scd-card\"));\n    const faces = cards.map((c) => c.querySelector<HTMLElement>(\".w4scd-face\")!);\n    const shades = cards.map((c) => c.querySelector<HTMLElement>(\".w4scd-shade\")!);\n    let raf = 0;\n    const paint = () => {\n      raf = 0;\n      const top = box.getBoundingClientRect().top;\n      const cover = cards.map((c) => {\n        const d = c.getBoundingClientRect().top - top - parseFloat(getComputedStyle(c).top);\n        return Math.min(1, Math.max(0, 1 - d / c.offsetHeight));\n      });\n      cards.forEach((_, i) => {\n        let depth = 0;\n        for (let j = i + 1; j < cards.length; j++) depth += cover[j];\n        faces[i].style.transform = `scale(${1 - SCALE_STEP * depth})`;\n        shades[i].style.opacity = String(Math.min(0.6, DIM_STEP * depth));\n      });\n      box.toggleAttribute(\"data-scrolled\", box.scrollTop > 8);\n    };\n    const onScroll = () => { if (!raf) raf = requestAnimationFrame(paint); };\n    box.addEventListener(\"scroll\", onScroll, { passive: true });\n    paint();\n\n    // Demo scroll until the visitor takes over; paused while off screen.\n    let demo = 0, last = 0, pos = box.scrollTop, seen = true, done = !AUTO_PX_PER_SEC;\n    const step = (now: number) => {\n      const dt = last ? Math.min(now - last, 50) : 0;\n      last = now;\n      pos += (AUTO_PX_PER_SEC * dt) / 1000;\n      if (pos >= box.scrollHeight - box.clientHeight) { done = true; demo = 0; return; }\n      box.scrollTop = pos;\n      demo = requestAnimationFrame(step);\n    };\n    const start = () => { if (!done && seen && !demo) { last = 0; demo = requestAnimationFrame(step); } };\n    const stop = () => { cancelAnimationFrame(demo); demo = 0; };\n    const handOver = () => { done = true; stop(); };\n    const io = new IntersectionObserver(([e]) => { seen = e.isIntersecting; if (seen) start(); else stop(); });\n    io.observe(box);\n    const evs = [\"wheel\", \"touchstart\", \"pointerdown\", \"keydown\"] as const;\n    evs.forEach((ev) => box.addEventListener(ev, handOver, { passive: true }));\n    start();\n    return () => {\n      stop();\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      box.removeEventListener(\"scroll\", onScroll);\n      evs.forEach((ev) => box.removeEventListener(ev, handOver));\n    };\n  }, [reduce]);\n\n  return (\n    <section ref={boxRef} tabIndex={0} aria-label=\"Breads this week, scrollable\" className=\"w4scd-root\">\n      <style>{`\n        .w4scd-root { height: 620px; overflow-y: auto; background: #efe7da; color: #2b2119; position: relative; scrollbar-width: thin; scrollbar-color: #c9b89c transparent; }\n        .w4scd-root:focus-visible { outline: 3px solid #b4532e; outline-offset: -3px; }\n        .w4scd-hint { position: sticky; top: 0; z-index: 20; height: 0; display: flex; justify-content: center; pointer-events: none; transition: opacity .4s ease; }\n        .w4scd-hint span { margin-top: 10px; padding: 4px 12px; border-radius: 999px; background: rgba(43,33,25,.82); color: #f7f1e6; font-size: 12px; letter-spacing: .04em; }\n        .w4scd-root[data-scrolled] .w4scd-hint { opacity: 0; }\n        .w4scd-card { position: sticky; top: 48px; }\n        .w4scd-face { transform-origin: 50% 0; will-change: transform; }\n        .w4scd-shade { position: absolute; inset: 0; border-radius: inherit; background: #1a130e; opacity: 0; pointer-events: none; }\n        @media (prefers-reduced-motion: reduce) { .w4scd-face { transform: none !important; } .w4scd-shade { opacity: 0 !important; } .w4scd-hint { display: none; } }\n      `}</style>\n      <div className=\"w4scd-hint\" aria-hidden=\"true\"><span>Scroll inside</span></div>\n      <div className=\"mx-auto grid max-w-5xl gap-8 px-5 pb-24 pt-12 md:grid-cols-[minmax(0,0.8fr)_minmax(0,1.2fr)] md:gap-12 md:px-10\">\n        <div className=\"md:sticky md:top-12 md:self-start\">\n          <p className=\"text-xs font-semibold uppercase tracking-[0.22em] text-[#93401f]\">Larchmont Bakehouse</p>\n          <h2 className=\"mt-3 font-serif text-3xl leading-tight sm:text-4xl\">Four loaves on the shelf this week</h2>\n          <p className=\"mt-4 max-w-sm text-[15px] leading-relaxed text-[#5a4a3b]\">Each one is mixed by hand the afternoon before and baked before the shop opens at seven.</p>\n          <a href=\"#\" className=\"mt-6 inline-flex min-h-[44px] items-center rounded-full bg-[#2b2119] px-6 text-sm font-semibold text-[#f7f1e6] transition hover:bg-[#453629] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#b4532e] focus-visible:ring-offset-2 focus-visible:ring-offset-[#efe7da]\">Reserve a loaf</a>\n        </div>\n        <ul className=\"grid gap-6\">\n          {loaves.map((l, i) => (\n            <li key={l.name} className=\"w4scd-card\">\n              <article className=\"w4scd-face relative flex min-h-[360px] flex-col rounded-[28px] p-7 shadow-[0_18px_40px_-20px_rgba(43,33,25,0.45)] sm:p-9\" style={{ background: l.bg, color: l.ink }}>\n                <div className=\"flex items-center justify-between text-sm font-medium opacity-90\">\n                  <span>No. {String(i + 1).padStart(2, \"0\")}</span>\n                  <span className=\"font-serif text-2xl\">{l.price}</span>\n                </div>\n                <h3 className=\"mt-auto font-serif text-4xl leading-none sm:text-5xl\">{l.name}</h3>\n                <p className=\"mt-4 max-w-md text-[15px] leading-relaxed opacity-90\">{l.note}</p>\n                <div className=\"mt-6 flex flex-wrap gap-2\">\n                  {l.tags.map((t) => (\n                    <span key={t} className=\"rounded-full px-3 py-1 text-xs font-semibold\" style={{ background: l.chip }}>{t}</span>\n                  ))}\n                </div>\n                <span className=\"w4scd-shade\" aria-hidden=\"true\" />\n              </article>\n            </li>\n          ))}\n        </ul>\n      </div>\n    </section>\n  );\n}\n"}]}
