{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"masked-strip-photo-transition-drag-slider-with-momentum","type":"registry:component","title":"Masked Strip Photo Transition Drag Slider","description":"","dependencies":[],"files":[{"path":"components/we-inc/masked-strip-photo-transition-drag-slider-with-momentum.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst STRIPS = 6; // strips each photo is cut into\nconst FRICTION = 0.93; // speed kept each frame after release\nconst IDLE = 3000; // idle advance until the first interaction\n\nconst cabins = [\n  { name: \"The boathouse\", text: \"A jetty over the water and one room above the old boat bay. Sleeps two, from 140 a night.\", src: \"https://images.pexels.com/photos/19231583/pexels-photo-19231583.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Wooden jetty leading to an old boathouse on a lake, framed by branches\" },\n  { name: \"Pine Road pair\", text: \"Two cabins side by side on the forest track, good for families travelling together.\", src: \"https://images.pexels.com/photos/30550409/pexels-photo-30550409.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Two wooden cabins beside a forest road under tall pines\" },\n  { name: \"The stove room\", text: \"Pine walls, a hanging wood stove and a basket of split birch by the door.\", src: \"https://images.pexels.com/photos/33640978/pexels-photo-33640978.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Cabin interior with a black wood stove, pine walls and soft curtains\" },\n  { name: \"Winter lodges\", text: \"Open December to March, with snowshoes in the porch and the trail starting at the gate.\", src: \"https://images.pexels.com/photos/11148974/pexels-photo-11148974.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Wooden cabins among snowy trees in winter\" },\n  { name: \"Red cabin\", text: \"On the far shore below the hills, reached by a short gravel lane.\", src: \"https://images.pexels.com/photos/36707272/pexels-photo-36707272.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"A red cabin at the edge of a calm lake below misty green hills\" },\n];\n\nexport default function MaskedStripPhotoTransitionDragSlider() {\n  const root = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    const R = root.current;\n    if (!R) return;\n    const Q = (s: string) => Array.from(R.querySelectorAll<HTMLElement>(s));\n    const st = Q(\".w2cab-stage\")[0], tr = Q(\".w2cab-track\")[0], ly = Q(\".w2cab-layer\"), it = Q(\".w2cab-item\"), bt = Q(\".w2cab-btn\") as HTMLButtonElement[], lv = Q(\".w2cab-live\")[0];\n    const views = [st, tr.parentElement as HTMLElement];\n    const n = it.length;\n    const rm = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let x = 0, v = 0, tx: number | null = 0, raf = 0, dn = false, sx = 0, x0 = 0, lt = 0, f = 1, cur = 0, vis = true, au = 0, stopped = false;\n    const cl = (i: number) => Math.max(0, Math.min(n - 1, i));\n    const show = (i: number) => {\n      if (i === cur) return;\n      // the new photo's strips slide in over the old one, which stays put underneath\n      ly.forEach((l, k) => {\n        l.className = \"w2cab-layer absolute inset-0\" + (k === i ? \" w2cab-on\" : k === cur ? \" w2cab-was\" : \"\");\n        l.setAttribute(\"aria-hidden\", String(k !== i));\n      });\n      it[cur].classList.remove(\"w2cab-on\"); it[i].classList.add(\"w2cab-on\");\n      cur = i;\n      bt[0].disabled = i === 0; bt[1].disabled = i === n - 1;\n    };\n    const loop = () => {\n      raf = 0;\n      const s = it[0].offsetWidth, m = -(n - 1) * s;\n      if (!dn) {\n        if (tx == null) { x += v; v *= FRICTION; if (x > 0 || x < m) v *= 0.6; if (Math.abs(v) < 0.5) tx = cl(Math.round(-x / s)); }\n        if (tx != null) { const g = -tx * s; x = rm ? g : x + (g - x) * 0.16; if (Math.abs(g - x) < 0.5) x = g; }\n      }\n      const p = x > 0 ? x * 0.3 : x < m ? m + (x - m) * 0.3 : x;\n      tr.style.transform = `translate3d(${p}px,0,0)`;\n      show(cl(Math.round(-p / s)));\n      if (vis && (dn || tx == null || x !== -tx * s)) raf = requestAnimationFrame(loop);\n    };\n    const kick = () => { if (!raf) raf = requestAnimationFrame(loop); };\n    const go = (i: number, say?: boolean) => {\n      tx = cl(i); v = 0; kick();\n      if (say) lv.textContent = `Slide ${tx + 1}: ${it[tx].children[1].textContent}`;\n    };\n    const stop = () => { clearTimeout(au); stopped = true; };\n    const up = (e: PointerEvent) => { if (!dn) return; dn = false; if (rm || e.timeStamp - lt > 100) v = 0; tx = null; kick(); };\n    const downs = views.map((el) => (e: PointerEvent) => {\n      stop(); dn = true; sx = e.clientX; x0 = x; v = 0;\n      f = it[0].offsetWidth / el.offsetWidth; // dragging the photo moves the text rail at its own scale\n      el.setPointerCapture(e.pointerId); kick();\n    });\n    const move = (e: PointerEvent) => {\n      if (!dn) return;\n      const nx = x0 + (e.clientX - sx) * f;\n      v = v * 0.5 + (nx - x) * 0.5; x = nx; lt = e.timeStamp; kick();\n    };\n    views.forEach((el, i) => {\n      el.addEventListener(\"pointerdown\", downs[i]); el.addEventListener(\"pointermove\", move);\n      el.addEventListener(\"pointerup\", up); el.addEventListener(\"pointercancel\", up);\n    });\n    const clicks = bt.map((b) => () => { stop(); go(cur + Number(b.dataset.d), true); });\n    bt.forEach((b, i) => b.addEventListener(\"click\", clicks[i]));\n    const key = (e: KeyboardEvent) => {\n      const i = e.key === \"ArrowLeft\" ? cur - 1 : e.key === \"ArrowRight\" ? cur + 1 : null;\n      if (i == null) return;\n      e.preventDefault(); stop(); go(i, true);\n    };\n    R.addEventListener(\"keydown\", key); R.addEventListener(\"pointerenter\", stop); R.addEventListener(\"focusin\", stop);\n    const tick = () => { if (stopped) return; if (vis) go((cur + 1) % n); au = window.setTimeout(tick, IDLE); };\n    const start = () => { if (!stopped) au = window.setTimeout(tick, 2200); };\n    if (!rm) { if (document.readyState === \"complete\") start(); else window.addEventListener(\"load\", start); }\n    const io = new IntersectionObserver(([e]) => { vis = e.isIntersecting; if (vis) kick(); });\n    io.observe(R);\n    return () => {\n      stop(); io.disconnect(); cancelAnimationFrame(raf); window.removeEventListener(\"load\", start);\n      views.forEach((el, i) => {\n        el.removeEventListener(\"pointerdown\", downs[i]); el.removeEventListener(\"pointermove\", move);\n        el.removeEventListener(\"pointerup\", up); el.removeEventListener(\"pointercancel\", up);\n      });\n      bt.forEach((b, i) => b.removeEventListener(\"click\", clicks[i]));\n      R.removeEventListener(\"keydown\", key); R.removeEventListener(\"pointerenter\", stop); R.removeEventListener(\"focusin\", stop);\n    };\n  }, []);\n\n  return (\n    <section ref={root} role=\"region\" aria-roledescription=\"carousel\" aria-label=\"Hollin Woods cabins\" className=\"overflow-hidden bg-[#1d2720] px-4 py-12 text-[#efe8d8]\">\n      <style>{`\n        .w2cab-stage { touch-action: pan-y; cursor: grab; user-select: none; }\n        .w2cab-strip img { transform: translateY(101%); }\n        .w2cab-strip:nth-child(even) img { transform: translateY(-101%); }\n        .w2cab-was { z-index: 1; }\n        .w2cab-was .w2cab-strip img { transform: none; }\n        .w2cab-on { z-index: 2; }\n        .w2cab-on .w2cab-strip img, .w2cab-on .w2cab-strip:nth-child(even) img { transform: none; transition: transform .8s cubic-bezier(.7,0,.2,1); }\n        .w2cab-view { touch-action: pan-y; cursor: grab; }\n        .w2cab-track { will-change: transform; }\n        .w2cab-item { opacity: .3; transition: opacity .4s; user-select: none; }\n        .w2cab-item.w2cab-on { opacity: 1; }\n        .w2cab-btn:disabled { opacity: .3; cursor: default; }\n        .w2cab-btn:focus-visible, .w2cab-stage:focus-visible { outline: 2px solid #b9c48f; outline-offset: 3px; }\n        @media (prefers-reduced-motion: reduce) { .w2cab-on .w2cab-strip img, .w2cab-on .w2cab-strip:nth-child(even) img, .w2cab-item { transition: none; } }\n      `}</style>\n      <div className=\"mx-auto grid max-w-[1040px] grid-cols-[minmax(0,1fr)] items-center gap-8 md:grid-cols-[minmax(0,1.05fr)_minmax(0,1fr)] md:gap-14\">\n        <div className=\"w2cab-stage relative aspect-square md:aspect-[4/5] max-h-[600px] w-full overflow-hidden rounded-md bg-[#2c3a30]\" tabIndex={0}>\n          {cabins.map((c, k) => (\n            <div key={c.src} className={`w2cab-layer absolute inset-0${k === 0 ? \" w2cab-on\" : \"\"}`} aria-hidden={k !== 0}>\n              {Array.from({ length: STRIPS }, (_, i) => (\n                // each strip is a window onto a full-width copy of the photo\n                <div key={i} className=\"w2cab-strip absolute inset-y-0 overflow-hidden\" style={{ left: `${(i * 100) / STRIPS}%`, width: `calc(${100 / STRIPS}% + 1px)` }}>\n                  <img src={c.src} alt={i === 0 ? c.alt : \"\"} draggable={false} className=\"absolute top-0 h-full max-w-none object-cover\" style={{ left: `${-i * 100}%`, width: `${STRIPS * 100}%`, transitionDelay: `${i * 70}ms` }} />\n                </div>\n              ))}\n            </div>\n          ))}\n        </div>\n        <div>\n          <p className=\"text-xs font-semibold uppercase tracking-[0.24em] text-[#b9c48f]\">Hollin Woods Cabins</p>\n          <h2 className=\"mb-7 mt-3 font-serif text-[28px] font-medium leading-tight sm:text-[40px]\">Five places to stay quiet for a while</h2>\n          <div className=\"w2cab-view overflow-hidden border-t border-[#efe8d8]/20\">\n            <ol className=\"w2cab-track m-0 flex list-none p-0\">\n              {cabins.map((c, i) => (\n                <li key={c.name} className={`w2cab-item box-border shrink-0 grow-0 basis-full pb-2 pr-6 pt-[22px]${i === 0 ? \" w2cab-on\" : \"\"}`} role=\"group\" aria-roledescription=\"slide\" aria-label={`${i + 1} of ${cabins.length}`}>\n                  <span className=\"text-xs font-semibold tracking-[0.12em] text-[#b9c48f]\">{`0${i + 1} / 0${cabins.length}`}</span>\n                  <h3 className=\"mt-2.5 font-serif text-[26px] font-medium leading-snug\">{c.name}</h3>\n                  <p className=\"mt-2.5 max-w-md text-[15px] leading-relaxed text-[#cfc8b8]\">{c.text}</p>\n                </li>\n              ))}\n            </ol>\n          </div>\n          <div className=\"mt-6 flex gap-2.5\">\n            {[-1, 1].map((d) => (\n              <button key={d} type=\"button\" data-d={d} disabled={d < 0} aria-label={d < 0 ? \"Previous slide\" : \"Next slide\"} className=\"w2cab-btn grid h-12 w-12 place-items-center rounded-full border border-[#efe8d8]/35 hover:bg-[#efe8d8]/10\">\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          </div>\n          <p className=\"w2cab-live sr-only\" aria-live=\"polite\" />\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
