{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"service-list-with-a-photo-that-follows-the-pointer","type":"registry:block","title":"Service List With a Photo That Follows the Pointer","description":"","dependencies":[],"files":[{"path":"components/we-inc/service-list-with-a-photo-that-follows-the-pointer.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst FOLLOW = 0.14; // how quickly the photo catches up with the pointer (0 to 1)\nconst DEMO_EVERY = 1900; // ms per row in the idle demo\n\nconst services = [\n  { name: \"Handbuilding evenings\", line: \"Pinch, coil and slab over six Tuesdays\", price: \"$210\", src: \"https://images.pexels.com/photos/6693557/pexels-photo-6693557.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Hands shaping a small clay pot on a wooden table among tools and bisque pieces\" },\n  { name: \"Open studio membership\", line: \"Your own shelf, wheels and kiln space\", price: \"$95 a month\", src: \"https://images.pexels.com/photos/34004100/pexels-photo-34004100.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Rows of freshly thrown clay pots drying side by side in a workshop\" },\n  { name: \"Glaze and firing service\", line: \"Bring finished pieces, collect them in a week\", price: \"From $6 a piece\", src: \"https://images.pexels.com/photos/8063833/pexels-photo-8063833.jpeg?auto=compress&cs=tinysrgb&w=800\", alt: \"Black and white photo of stacked ceramic bowls and small round pots\" },\n];\n\nexport default function ServiceListWithAPhotoThatFollowsThePointer() {\n  const list = useRef<HTMLDivElement>(null);\n  const prev = useRef<HTMLDivElement>(null);\n\n  useEffect(() => {\n    const ul = list.current, pv = prev.current;\n    if (!ul || !pv) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const rows = Array.from(ul.querySelectorAll<HTMLAnchorElement>(\"a\"));\n    const imgs = Array.from(pv.querySelectorAll<HTMLImageElement>(\"img\"));\n    let active = -1, tx = 0, ty = 0, x = -1, y = 0, live = false, focused = false, frame = 0, visible = true, timer = 0, n = 0;\n    const show = (i: number) => {\n      active = i;\n      pv.toggleAttribute(\"data-on\", i >= 0);\n      imgs.forEach((im, k) => im.toggleAttribute(\"data-on\", k === i));\n    };\n    // parks the photo near the right end of a row (focus and idle demo)\n    const park = (i: number) => {\n      const r = rows[i];\n      tx = ul.clientWidth * 0.7;\n      ty = r.offsetTop + r.offsetHeight / 2;\n      show(i);\n    };\n    const move = (e: PointerEvent) => {\n      if (e.pointerType !== \"mouse\") return;\n      const b = ul.getBoundingClientRect();\n      tx = e.clientX - b.left;\n      ty = e.clientY - b.top;\n      live = true;\n    };\n    const over = (e: PointerEvent) => {\n      if (e.pointerType !== \"mouse\") return;\n      const i = rows.indexOf((e.target as HTMLElement).closest(\"a\") as HTMLAnchorElement);\n      if (i >= 0) show(i);\n    };\n    const leave = () => { live = false; show(-1); };\n    const focusIn = (e: FocusEvent) => { const i = rows.indexOf(e.target as HTMLAnchorElement); if (i >= 0) { focused = true; park(i); } };\n    const focusOut = () => { focused = false; show(-1); };\n    const demo = () => { if (!live && !focused) park(n++ % rows.length); };\n    const tick = () => {\n      if (x < 0) { x = tx; y = ty; }\n      x += (tx - x) * FOLLOW;\n      y += (ty - y) * FOLLOW;\n      pv.style.transform = `translate(${(x - pv.offsetWidth / 2).toFixed(1)}px, ${(y - pv.offsetHeight / 2).toFixed(1)}px)`;\n      frame = requestAnimationFrame(tick);\n    };\n    const play = () => { if (!frame && visible) { frame = requestAnimationFrame(tick); timer = window.setInterval(demo, DEMO_EVERY); } };\n    const stop = () => { cancelAnimationFrame(frame); frame = 0; window.clearInterval(timer); };\n    const io = new IntersectionObserver(([en]) => { visible = en.isIntersecting; if (visible) play(); else stop(); });\n    io.observe(ul);\n    if (window.matchMedia(\"(hover: hover) and (pointer: fine)\").matches) {\n      ul.addEventListener(\"pointermove\", move);\n      ul.addEventListener(\"pointerover\", over);\n      ul.addEventListener(\"pointerleave\", leave);\n    }\n    ul.addEventListener(\"focusin\", focusIn);\n    ul.addEventListener(\"focusout\", focusOut);\n    demo();\n    play();\n    return () => {\n      stop();\n      io.disconnect();\n      ul.removeEventListener(\"pointermove\", move);\n      ul.removeEventListener(\"pointerover\", over);\n      ul.removeEventListener(\"pointerleave\", leave);\n      ul.removeEventListener(\"focusin\", focusIn);\n      ul.removeEventListener(\"focusout\", focusOut);\n    };\n  }, []);\n\n  return (\n    <section className=\"bg-[#f3ece4] px-5 py-16 text-[#2a1f1a] sm:py-20\">\n      <style>{`\n        .w3row-pv { will-change: transform; }\n        .w3row-in { opacity: 0; transform: scale(.85) rotate(-3deg); transition: opacity .35s ease, transform .5s cubic-bezier(.2,.8,.2,1); }\n        .w3row-pv[data-on] .w3row-in { opacity: 1; transform: none; }\n        .w3row-in img { opacity: 0; transition: opacity .35s ease; }\n        .w3row-in img[data-on] { opacity: 1; }\n        .w3row-a:focus-visible { outline: 2px solid #2a1f1a; outline-offset: 2px; }\n        .w3row-name { transition: transform .4s cubic-bezier(.2,.8,.2,1); }\n        .w3row-a:focus-visible .w3row-name { transform: translateX(10px); }\n        .w3row-thumb { display: none; }\n        @media (hover: hover) and (pointer: fine) { .w3row-a:hover .w3row-name { transform: translateX(10px); } }\n        /* touch and reduced motion: no follower, each row shows its own small photo */\n        @media (hover: none), (pointer: coarse), (prefers-reduced-motion: reduce) {\n          .w3row-pv { display: none; }\n          .w3row-thumb { display: block; }\n          .w3row-name { transition: none; }\n        }\n      `}</style>\n      <div className=\"mx-auto max-w-4xl\">\n        <p className=\"text-xs font-semibold uppercase tracking-[0.25em] text-[#8a4b32]\">Tidewater Clay Studio</p>\n        <h2 className=\"mt-3 text-3xl font-semibold tracking-tight sm:text-4xl\">Ways to get your hands dirty</h2>\n        <div ref={list} className=\"relative mt-10\">\n        <ul className=\"border-t border-[#d9cabb]\">\n          {services.map((s, i) => (\n            <li key={s.name} className=\"border-b border-[#d9cabb]\">\n              <a href=\"#\" className=\"w3row-a relative z-10 flex items-center gap-4 py-6 sm:gap-6\">\n                <span className=\"w-8 shrink-0 font-mono text-sm text-[#7a5a48]\">0{i + 1}</span>\n                <img src={s.src} alt=\"\" aria-hidden=\"true\" loading=\"lazy\" width={64} height={48} className=\"w3row-thumb h-12 w-16 shrink-0 rounded-md object-cover\" />\n                <span className=\"w3row-name min-w-0 flex-1\">\n                  <span className=\"block text-xl font-semibold sm:text-2xl\">{s.name}</span>\n                  <span className=\"mt-1 block text-sm text-[#5e4a3f] sm:text-base\">{s.line}</span>\n                </span>\n                <span className=\"shrink-0 text-right text-sm font-semibold sm:text-base\">{s.price}</span>\n              </a>\n            </li>\n          ))}\n        </ul>\n          <div ref={prev} aria-hidden=\"true\" className=\"w3row-pv pointer-events-none absolute left-0 top-0 z-20 w-56 sm:w-64\">\n            <div className=\"w3row-in relative aspect-[4/3] overflow-hidden rounded-xl bg-[#d9cabb] shadow-[0_20px_40px_-12px_rgba(42,31,26,0.45)]\">\n              {services.map((s) => (\n                <img key={s.name} src={s.src} alt={s.alt} width={256} height={192} className=\"absolute inset-0 h-full w-full object-cover\" />\n              ))}\n            </div>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
