{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"main-slide-with-synced-draggable-thumbnail-strip","type":"registry:component","title":"Main Slide With Synced Draggable Thumbnail Strip","description":"","dependencies":[],"files":[{"path":"components/we-inc/main-slide-with-synced-draggable-thumbnail-strip.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst FRICTION = 0.93; // speed kept each frame after release\nconst RUBBER = 0.3; // stretch past the ends of either rail\nconst IDLE = 3000; // idle advance until the first interaction\n\nconst pieces = [\n  { name: \"Spindle back chair\", note: \"Cherry with a linen pad, 340.00\", src: \"https://images.pexels.com/photos/28529915/pexels-photo-28529915.jpeg?auto=compress&cs=tinysrgb&w=1400\", thumb: \"https://images.pexels.com/photos/28529915/pexels-photo-28529915.jpeg?auto=compress&cs=tinysrgb&w=300\", alt: \"Wooden spindle back chair with a white seat cushion in a sunlit room\" },\n  { name: \"Three-seat sofa\", note: \"Grey woven cover, 2,150.00\", src: \"https://images.pexels.com/photos/6758245/pexels-photo-6758245.jpeg?auto=compress&cs=tinysrgb&w=1400\", thumb: \"https://images.pexels.com/photos/6758245/pexels-photo-6758245.jpeg?auto=compress&cs=tinysrgb&w=300\", alt: \"Grey three-seat sofa with patterned cushions in front of a leafy mural\" },\n  { name: \"Ring vase and globe lamp\", note: \"Matte ceramic and opal glass, 95.00 and 160.00\", src: \"https://images.pexels.com/photos/6952339/pexels-photo-6952339.jpeg?auto=compress&cs=tinysrgb&w=1400\", thumb: \"https://images.pexels.com/photos/6952339/pexels-photo-6952339.jpeg?auto=compress&cs=tinysrgb&w=300\", alt: \"White ring vase with dried grass beside a glass globe lamp on a white table\" },\n  { name: \"Pleated shade lamp\", note: \"Brass base, warm bulb, 210.00\", src: \"https://images.pexels.com/photos/36799079/pexels-photo-36799079.jpeg?auto=compress&cs=tinysrgb&w=1400\", thumb: \"https://images.pexels.com/photos/36799079/pexels-photo-36799079.jpeg?auto=compress&cs=tinysrgb&w=300\", alt: \"Table lamp with a pleated shade glowing in a cosy living room corner\" },\n  { name: \"Stoneware tableware\", note: \"Speckled white glaze, from 18.00\", src: \"https://images.pexels.com/photos/14341984/pexels-photo-14341984.jpeg?auto=compress&cs=tinysrgb&w=1400\", thumb: \"https://images.pexels.com/photos/14341984/pexels-photo-14341984.jpeg?auto=compress&cs=tinysrgb&w=300\", alt: \"Speckled white stoneware plates, bowls and mugs laid out on linen\" },\n  { name: \"Glazed mugs and plates\", note: \"Twelve colours, from 14.00\", src: \"https://images.pexels.com/photos/38243203/pexels-photo-38243203.jpeg?auto=compress&cs=tinysrgb&w=1400\", thumb: \"https://images.pexels.com/photos/38243203/pexels-photo-38243203.jpeg?auto=compress&cs=tinysrgb&w=300\", alt: \"Shelves of colourful glazed mugs and stacked plates\" },\n];\n\ntype Rail = { x: number; v: number; t: number | null; dn: boolean; k: () => void; lim: () => number; off: () => void };\n\nexport default function MainSlideWithSyncedDraggableThumbnailStrip() {\n  const root = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    const R = root.current;\n    if (!R) return;\n    const main = R.querySelector<HTMLElement>(\".w2thm-main\")!, strip = R.querySelector<HTMLElement>(\".w2thm-strip\")!;\n    const sl = Array.from(R.querySelectorAll<HTMLElement>(\".w2thm-slide\")), th = Array.from(R.querySelectorAll<HTMLButtonElement>(\".w2thm-th\"));\n    const bt = Array.from(R.querySelectorAll<HTMLButtonElement>(\".w2thm-btn\")), lv = R.querySelector<HTMLElement>(\".w2thm-live\")!;\n    const n = sl.length;\n    const rm = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let cur = 0, vis = true, au = 0, mv = false, stopped = false;\n    const cl = (i: number) => Math.max(0, Math.min(n - 1, i));\n    const stop = () => { clearTimeout(au); stopped = true; };\n\n    // one small physics routine shared by both rails; snap lands on whole slides, otherwise it coasts and stops\n    const rail = (vw: HTMLElement, tr: HTMLElement, snap: boolean, cb?: (f: number) => void): Rail => {\n      let raf = 0, sx = 0, x0 = 0, lt = 0;\n      const lim = () => Math.min(0, vw.offsetWidth - tr.scrollWidth);\n      const o: Rail = { x: 0, v: 0, t: 0, dn: false, k: () => { if (!raf) raf = requestAnimationFrame(loop); }, lim, off: () => cancelAnimationFrame(raf) };\n      function loop() {\n        raf = 0;\n        const m = lim(), s = vw.offsetWidth;\n        if (!o.dn) {\n          if (o.t == null) { o.x += o.v; o.v *= FRICTION; if (o.x > 0 || o.x < m) o.v *= 0.6; if (Math.abs(o.v) < 0.5) o.t = snap ? -cl(Math.round(-o.x / s)) * s : Math.max(m, Math.min(0, o.x)); }\n          if (o.t != null) { o.x = rm ? o.t : o.x + (o.t - o.x) * 0.15; if (Math.abs(o.t - o.x) < 0.5) o.x = o.t; }\n        }\n        const p = o.x > 0 ? o.x * RUBBER : o.x < m ? m + (o.x - m) * RUBBER : o.x;\n        tr.style.transform = `translate3d(${p}px,0,0)`;\n        if (cb) cb(p / s);\n        if (vis && (o.dn || o.t == null || o.x !== o.t)) raf = requestAnimationFrame(loop);\n      }\n      vw.onpointerdown = (e) => { stop(); o.dn = true; mv = false; sx = e.clientX; x0 = o.x; o.v = 0; o.k(); };\n      vw.onpointermove = (e) => {\n        if (!o.dn) return;\n        const d = e.clientX - sx;\n        if (Math.abs(d) > 6 && !mv) { mv = true; vw.setPointerCapture(e.pointerId); } // capture only once it is a drag, so a tap still clicks a thumbnail\n        o.v = o.v * 0.5 + (x0 + d - o.x) * 0.5; o.x = x0 + d; lt = e.timeStamp; o.k();\n      };\n      vw.onpointerup = vw.onpointercancel = (e) => { if (!o.dn) return; o.dn = false; if (rm || e.timeStamp - lt > 100) o.v = 0; o.t = null; o.k(); };\n      return o;\n    };\n    const B = rail(strip, R.querySelector<HTMLElement>(\".w2thm-thumbs\")!, false);\n    // keep the active thumbnail centred in the strip unless someone is dragging the strip\n    const centre = (i: number) => {\n      if (B.dn) return;\n      const t = th[i];\n      B.t = Math.max(B.lim(), Math.min(0, strip.offsetWidth / 2 - t.offsetLeft - t.offsetWidth / 2));\n      B.v = 0; B.k();\n    };\n    const show = (i: number) => {\n      if (i === cur) return;\n      th[cur].removeAttribute(\"aria-current\"); th[i].setAttribute(\"aria-current\", \"true\");\n      sl.forEach((e, k) => e.setAttribute(\"aria-hidden\", String(k !== i)));\n      cur = i;\n      bt[0].disabled = i === 0; bt[1].disabled = i === n - 1;\n      centre(i);\n    };\n    const A = rail(main, R.querySelector<HTMLElement>(\".w2thm-track\")!, true, (f) => show(cl(Math.round(-f))));\n    const go = (i: number, say?: boolean) => {\n      const j = cl(i);\n      A.t = -j * main.offsetWidth; A.v = 0; A.k();\n      if (say) lv.textContent = `Piece ${j + 1} of ${n}: ${sl[j].querySelector(\"h3\")!.textContent}`;\n    };\n    const clicks = th.map((t, i) => (e: MouseEvent) => { if (mv) { e.preventDefault(); return; } stop(); go(i, true); });\n    const focuses = th.map((t, i) => () => centre(i));\n    th.forEach((t, i) => { t.addEventListener(\"click\", clicks[i]); t.addEventListener(\"focus\", focuses[i]); });\n    bt.forEach((b) => { b.onclick = () => { stop(); go(cur + Number(b.dataset.d), true); }; });\n    R.onkeydown = (e) => {\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.onpointerenter = stop;\n    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) { A.k(); B.k(); } });\n    io.observe(R);\n    return () => {\n      stop(); io.disconnect(); A.off(); B.off(); window.removeEventListener(\"load\", start);\n      [main, strip].forEach((el) => { el.onpointerdown = el.onpointermove = el.onpointerup = el.onpointercancel = null; });\n      th.forEach((t, i) => { t.removeEventListener(\"click\", clicks[i]); t.removeEventListener(\"focus\", focuses[i]); });\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=\"Oxbow Home pieces\" className=\"overflow-hidden bg-[#f5f6f8] px-4 py-12 text-[#1c2430]\">\n      <style>{`\n        .w2thm-main, .w2thm-strip { touch-action: pan-y; cursor: grab; user-select: none; }\n        .w2thm-track, .w2thm-thumbs { will-change: transform; }\n        .w2thm-th { opacity: .55; transition: opacity .3s; }\n        .w2thm-th::after { content: \"\"; position: absolute; inset: 0; border-radius: 8px; box-shadow: inset 0 0 0 3px #3d5a80; opacity: 0; transition: opacity .3s; }\n        .w2thm-th[aria-current=\"true\"], .w2thm-th[aria-current=\"true\"]::after { opacity: 1; }\n        .w2thm-th:hover { opacity: .85; }\n        .w2thm-btn:disabled { opacity: .35; cursor: default; }\n        .w2thm-btn:focus-visible, .w2thm-th:focus-visible, .w2thm-main:focus-visible { outline: 2px solid #3d5a80; outline-offset: 3px; }\n        @media (prefers-reduced-motion: reduce) { .w2thm-th, .w2thm-th::after { transition: none; } }\n      `}</style>\n      <div className=\"mx-auto max-w-[960px]\">\n        <div className=\"mb-[22px] flex items-end justify-between gap-4\">\n          <div>\n            <p className=\"text-xs font-semibold uppercase tracking-[0.24em] text-[#3d5a80]\">Oxbow Home</p>\n            <h2 className=\"mt-2 text-[26px] font-semibold tracking-tight sm:text-4xl\">New in the showroom</h2>\n          </div>\n          <div className=\"flex gap-2\">\n            {[-1, 1].map((d) => (\n              <button key={d} type=\"button\" data-d={d} disabled={d < 0} aria-label={d < 0 ? \"Previous piece\" : \"Next piece\"} className=\"w2thm-btn grid h-11 w-11 place-items-center rounded-[10px] border border-[#c9d0db] bg-white hover:border-[#3d5a80]\">\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        </div>\n        <div className=\"w2thm-main relative aspect-[4/3] overflow-hidden rounded-xl bg-[#dfe3ea] sm:aspect-video\" tabIndex={0}>\n          <ol className=\"w2thm-track m-0 flex h-full list-none p-0\">\n            {pieces.map((p, i) => (\n              <li key={p.name} className=\"w2thm-slide relative h-full shrink-0 grow-0 basis-full\" role=\"group\" aria-roledescription=\"slide\" aria-label={`${i + 1} of ${pieces.length}`} aria-hidden={i > 0}>\n                <img src={p.src} alt={p.alt} draggable={false} className=\"pointer-events-none block h-full w-full object-cover\" />\n                <div className=\"absolute bottom-4 left-4 box-border max-w-[calc(100%-32px)] rounded-[10px] bg-white/95 px-4 py-3\">\n                  <h3 className=\"text-[17px] font-semibold\">{p.name}</h3>\n                  <p className=\"mt-0.5 text-[13px] text-[#4a5568]\">{p.note}</p>\n                </div>\n              </li>\n            ))}\n          </ol>\n        </div>\n        <div className=\"w2thm-strip mt-3.5 overflow-hidden\">\n          <div className=\"w2thm-thumbs flex w-max gap-2.5\">\n            {pieces.map((p, i) => (\n              <button key={p.name} type=\"button\" className=\"w2thm-th relative w-24 flex-none cursor-pointer overflow-hidden rounded-lg bg-[#dfe3ea] p-0 sm:w-[150px]\" aria-label={`Show ${p.name}`} aria-current={i === 0 ? \"true\" : undefined}>\n                <img src={p.thumb} alt=\"\" draggable={false} className=\"pointer-events-none block aspect-[3/2] w-full object-cover\" />\n              </button>\n            ))}\n          </div>\n        </div>\n        <p className=\"w2thm-live sr-only\" aria-live=\"polite\" />\n      </div>\n    </section>\n  );\n}\n"}]}
