{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"flick-away-stacked-card-deck-slider-with-momentum","type":"registry:component","title":"Flick Away Stacked Card Deck Slider","description":"","dependencies":[],"files":[{"path":"components/we-inc/flick-away-stacked-card-deck-slider-with-momentum.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst THROW = 0.35; // drag past this share of the card width and it flies off\nconst FLICK = 9; // or release faster than this (px per frame)\nconst IDLE = 3000; // idle advance until the first interaction\n\nconst plants = [\n  { name: \"Monstera\", note: \"Deliciosa, 70 cm\", price: \"38.00\", src: \"https://images.pexels.com/photos/12014192/pexels-photo-12014192.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Monstera plant on a black stand beside a sunny window\" },\n  { name: \"Snake plant\", note: \"White pot, 45 cm\", price: \"24.00\", src: \"https://images.pexels.com/photos/29218657/pexels-photo-29218657.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Snake plant in a white ribbed pot on a red table\" },\n  { name: \"Barrel cactus\", note: \"12 cm terracotta\", price: \"12.50\", src: \"https://images.pexels.com/photos/12683407/pexels-photo-12683407.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Small round cactus in a terracotta pot by an open window\" },\n  { name: \"Boston fern\", note: \"Likes it damp\", price: \"19.00\", src: \"https://images.pexels.com/photos/10903261/pexels-photo-10903261.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Lush potted ferns in terracotta in soft sunlight\" },\n  { name: \"Terracotta trio\", note: \"Three small pots\", price: \"31.00\", src: \"https://images.pexels.com/photos/35920773/pexels-photo-35920773.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"A group of leafy plants in terracotta pots on a wooden table\" },\n];\n\nexport default function FlickAwayStackedCardDeckSlider() {\n  const root = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    const R = root.current;\n    if (!R) return;\n    const dk = R.querySelector<HTMLElement>(\".w2dek-deck\")!;\n    const cs = Array.from(R.querySelectorAll<HTMLElement>(\".w2dek-card\"));\n    const bt = Array.from(R.querySelectorAll<HTMLButtonElement>(\".w2dek-btn\"));\n    const lv = R.querySelector<HTMLElement>(\".w2dek-live\")!, ct = R.querySelector<HTMLElement>(\".w2dek-count\")!;\n    const n = cs.length;\n    const rm = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let od = cs.slice(), x = 0, v = 0, raf = 0, dn = false, fly = false, sx = 0, x0 = 0, lt = 0, mv = false, vis = true, au = 0, stopped = false, announce = false;\n    const pos = (c: HTMLElement, d: number, t?: boolean) => {\n      c.style.transition = t ? \"\" : \"none\";\n      c.style.zIndex = String(n - d);\n      c.style.opacity = d > 3 ? \"0\" : \"1\";\n      c.inert = d > 0;\n      c.style.transform = `translate3d(0,${d * 16}px,0) scale(${1 - d * 0.05})`;\n    };\n    const lay = (t?: boolean) => {\n      if (t != null) od.forEach((c, d) => { if (d > 1) pos(c, d, t); });\n      const c = od[0], p = Math.min(Math.abs(x) / dk.offsetWidth, 1);\n      c.inert = false; c.style.zIndex = String(n); c.style.opacity = \"1\";\n      c.style.transition = t ? \"\" : \"none\";\n      c.style.transform = `translate3d(${x}px,${Math.abs(x) * 0.06}px,0) rotate(${x * 0.04}deg)`;\n      // the next card rises toward the top as the top one leaves\n      od[1].style.transition = t ? \"\" : \"none\";\n      od[1].style.transform = `translate3d(0,${16 * (1 - p)}px,0) scale(${0.95 + 0.05 * p})`;\n    };\n    const say = () => {\n      const i = cs.indexOf(od[0]);\n      ct.textContent = `${i + 1} / ${n}`;\n      return `Plant ${i + 1} of ${n}: ${od[0].querySelector(\"h3\")!.textContent}`;\n    };\n    const loop = () => {\n      raf = 0;\n      const w = dk.offsetWidth;\n      if (!dn) {\n        if (fly) {\n          x += v; v *= 1.02;\n          if (Math.abs(x) > w * 1.7) {\n            od = [...od.slice(1), od[0]];\n            x = 0; v = 0; fly = false;\n            pos(od[n - 1], n - 1);\n            const t = say();\n            if (announce) lv.textContent = t; // announce once the thrown card has gone\n            announce = false; lay(true); return;\n          }\n        } else {\n          v = (v + (0 - x) * 0.12) * 0.74; x += v; // spring back to the centre\n          if (Math.abs(x) < 0.5 && Math.abs(v) < 0.5) { x = 0; lay(true); return; }\n        }\n      }\n      lay();\n      raf = requestAnimationFrame(loop);\n    };\n    const kick = () => { if (!raf) raf = requestAnimationFrame(loop); };\n    const next = () => {\n      if (rm) { od = [...od.slice(1), od[0]]; say(); lay(false); return; }\n      fly = true; v = FLICK * 2; kick();\n    };\n    const prev = () => {\n      od = [od[n - 1], ...od.slice(0, n - 1)]; say();\n      if (rm) { lay(false); return; }\n      x = -dk.offsetWidth * 1.4; fly = false; v = 0; lay(true); kick();\n    };\n    const stop = () => { clearTimeout(au); stopped = true; };\n    const go = (d: number) => { stop(); if (d > 0) next(); else prev(); if (d < 0 || rm) lv.textContent = say(); else announce = true; };\n    dk.onpointerdown = (e) => { if (fly) return; stop(); dn = true; mv = false; sx = e.clientX; x0 = x; v = 0; kick(); };\n    dk.onpointermove = (e) => {\n      if (!dn) return;\n      const d = e.clientX - sx;\n      if (Math.abs(d) > 6 && !mv) { mv = true; dk.setPointerCapture(e.pointerId); } // capture once it is a drag, so a tap still opens the link\n      v = v * 0.5 + (x0 + d - x) * 0.5; x = x0 + d; lt = e.timeStamp; kick();\n    };\n    dk.onpointerup = dk.onpointercancel = (e) => {\n      if (!dn) return;\n      dn = false;\n      if (e.timeStamp - lt > 100) v = 0;\n      if (Math.abs(x) > dk.offsetWidth * THROW || Math.abs(v) > FLICK) {\n        if (rm) { x = 0; next(); } else { fly = true; v = (x < 0 ? -1 : 1) * Math.max(Math.abs(v), FLICK); }\n      } else if (rm) x = 0;\n      kick();\n    };\n    const click = (e: MouseEvent) => { if (mv) { e.preventDefault(); e.stopPropagation(); } }; // a drag is not a click\n    dk.addEventListener(\"click\", click, true);\n    bt.forEach((b) => { b.onclick = () => go(Number(b.dataset.d)); });\n    R.onkeydown = (e) => { const d = e.key === \"ArrowLeft\" ? -1 : e.key === \"ArrowRight\" ? 1 : 0; if (d) { e.preventDefault(); go(d); } };\n    R.onpointerenter = stop;\n    R.addEventListener(\"focusin\", stop);\n    const tick = () => { if (stopped) return; if (vis && !fly) next(); au = window.setTimeout(tick, IDLE); };\n    const start = () => { if (!stopped) au = window.setTimeout(tick, 2200); };\n    const io = new IntersectionObserver(([e]) => { vis = e.isIntersecting; if (!vis) { cancelAnimationFrame(raf); raf = 0; } else if (fly || x) kick(); });\n    io.observe(R);\n    lay(false);\n    if (!rm) { if (document.readyState === \"complete\") start(); else window.addEventListener(\"load\", start); }\n    return () => {\n      stop(); io.disconnect(); cancelAnimationFrame(raf); window.removeEventListener(\"load\", start);\n      dk.onpointerdown = dk.onpointermove = dk.onpointerup = dk.onpointercancel = null;\n      dk.removeEventListener(\"click\", click, true);\n      bt.forEach((b) => { b.onclick = null; });\n      R.onkeydown = null; R.onpointerenter = null; R.removeEventListener(\"focusin\", stop);\n    };\n  }, []);\n\n  return (\n    <section ref={root} role=\"region\" aria-roledescription=\"carousel\" aria-label=\"Fernhall plants this week\" className=\"overflow-hidden bg-[#ede3d1] px-4 pb-[72px] pt-14 text-[#2e2a22]\">\n      <style>{`\n        .w2dek-deck { touch-action: pan-y; user-select: none; }\n        .w2dek-card { transition: transform .45s cubic-bezier(.2,.8,.2,1), opacity .45s; cursor: grab; will-change: transform; transform-origin: 50% 100%; box-shadow: 0 18px 36px -22px rgba(50,40,20,.5); }\n        .w2dek-btn:focus-visible, .w2dek-card a:focus-visible { outline: 2px solid #5d6a2c; outline-offset: 3px; }\n        .w2dek-info > * { transition: opacity .3s; }\n        .w2dek-card[inert] .w2dek-info > * { opacity: 0; }\n        @media (prefers-reduced-motion: reduce) { .w2dek-card { transition: none; } }\n      `}</style>\n      <div className=\"mx-auto grid max-w-[980px] grid-cols-[minmax(0,1fr)] items-center gap-10 md:grid-cols-2\">\n        <div>\n          <p className=\"text-xs font-semibold uppercase tracking-[0.24em] text-[#5d6a2c]\">Fernhall Plant Shop</p>\n          <h2 className=\"mt-3 font-serif text-[30px] font-semibold leading-tight sm:text-[44px]\">On the bench this week</h2>\n          <p className=\"mt-4 max-w-md text-base leading-relaxed text-[#5a5245]\">Flick the top card away to see the next plant, or use the arrows. Every plant is potted in our back room on Harrow Lane.</p>\n          <div className=\"mt-7 flex items-center gap-2.5\">\n            {[-1, 1].map((d) => (\n              <button key={d} type=\"button\" data-d={d} aria-label={d < 0 ? \"Previous plant\" : \"Next plant\"} className=\"w2dek-btn grid h-12 w-12 place-items-center rounded-full bg-[#2e2a22] text-[#ede3d1] hover:bg-[#4a4437]\">\n                <svg viewBox=\"0 0 24 24\" width=\"20\" height=\"20\" aria-hidden=\"true\"><path d={d < 0 ? \"M15 5l-7 7 7 7\" : \"M9 5l7 7-7 7\"} fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" /></svg>\n              </button>\n            ))}\n            <span className=\"w2dek-count ml-2 text-sm font-semibold tabular-nums text-[#5a5245]\" aria-hidden=\"true\">1 / {plants.length}</span>\n          </div>\n        </div>\n        <div className=\"w2dek-deck relative mx-auto h-[490px] w-[300px] max-w-full\">\n          {plants.map((p, i) => (\n            <article key={p.name} className=\"w2dek-card absolute inset-x-0 top-0 h-[440px] overflow-hidden rounded-[18px] bg-[#fbf7ef]\" role=\"group\" aria-roledescription=\"slide\" aria-label={`${i + 1} of ${plants.length}`}>\n              <img src={p.src} alt={p.alt} draggable={false} className=\"pointer-events-none block h-[330px] w-full object-cover\" />\n              <div className=\"w2dek-info flex h-[110px] items-center justify-between gap-2.5 pl-[18px] pr-3.5\">\n                <div>\n                <h3 className=\"font-serif text-xl font-semibold\">{p.name}</h3>\n                <p className=\"mt-0.5 text-[13px] text-[#5a5245]\">{p.note}</p>\n                </div>\n                <a href=\"#\" className=\"inline-flex flex-none min-h-[40px] items-center gap-2 rounded-full bg-[#e7dcc6] px-3.5 text-[13px] font-semibold\">View plant <span className=\"text-[#5d6a2c]\">{p.price}</span></a>\n              </div>\n            </article>\n          ))}\n        </div>\n        <p className=\"w2dek-live sr-only\" aria-live=\"polite\" />\n      </div>\n    </section>\n  );\n}\n"}]}
