{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"label-cursor-reading-view-drag-or-play-on-cards","type":"registry:block","title":"Label Cursor for View, Drag and Play","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/label-cursor-reading-view-drag-or-play-on-cards.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\nconst EASE = 0.2; // how quickly the label catches the pointer\nconst IDLE_MS = 1800; // time the demo spends on each card\nconst plants = [\n  { name: \"Bearded iris\", note: \"Violet, May\", c: \"#6d5a9c\" },\n  { name: \"Lady's mantle\", note: \"Lime froth, June\", c: \"#b7c35a\" },\n  { name: \"Catmint\", note: \"Soft blue, all summer\", c: \"#8e93c4\" },\n  { name: \"Hakone grass\", note: \"Gold, autumn\", c: \"#c9a43a\" },\n  { name: \"Sedum\", note: \"Rust heads, October\", c: \"#a4553a\" },\n];\nconst card = \"w2lbl-card relative block aspect-[4/3] overflow-hidden rounded-[14px] bg-[#dfe5da] shadow-[0_1px_2px_rgba(29,43,34,.08),0_18px_40px_-24px_rgba(29,43,34,.45)] md:aspect-[4/5]\";\nconst ring = \"focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-[#b45309] focus-visible:ring-offset-2 focus-visible:ring-offset-[#eef1ea]\";\n\nfunction Caption({ title, chip }: { title: string; chip: string }) {\n  return (\n    <span className=\"absolute inset-x-3 bottom-3 flex flex-wrap items-center justify-between gap-2 rounded-[10px] bg-[#f7f8f4]/95 px-3 py-2.5\">\n      <span className=\"text-[15px] font-semibold\">{title}</span>\n      <span className=\"rounded-full bg-[#1d2b22] px-2.5 py-1 text-xs font-semibold text-[#f1efe6]\">{chip}</span>\n    </span>\n  );\n}\n\nexport default function LabelCursorReadingViewDragOrPlayOnCards() {\n  const reduce = useReducedMotion();\n  const [playing, setPlaying] = useState(false);\n  const rootRef = useRef<HTMLElement>(null);\n  const curRef = useRef<HTMLSpanElement>(null);\n  const txtRef = useRef<HTMLSpanElement>(null);\n  const stripRef = useRef<HTMLDivElement>(null);\n\n  // Mouse drag on the plant palette; touch and arrow keys scroll it natively.\n  useEffect(() => {\n    const strip = stripRef.current;\n    if (!strip) return;\n    let dragX: number | null = null, from = 0;\n    const down = (e: PointerEvent) => { if (e.pointerType === \"mouse\") { dragX = e.clientX; from = strip.scrollLeft; strip.setPointerCapture(e.pointerId); strip.style.scrollSnapType = \"none\"; } };\n    const move = (e: PointerEvent) => { if (dragX !== null) strip.scrollLeft = from - (e.clientX - dragX); };\n    const up = () => { dragX = null; strip.style.scrollSnapType = \"\"; };\n    strip.addEventListener(\"pointerdown\", down);\n    strip.addEventListener(\"pointermove\", move);\n    strip.addEventListener(\"pointerup\", up);\n    return () => { strip.removeEventListener(\"pointerdown\", down); strip.removeEventListener(\"pointermove\", move); strip.removeEventListener(\"pointerup\", up); };\n  }, []);\n\n  useEffect(() => {\n    const root = rootRef.current, cur = curRef.current, txt = txtRef.current;\n    if (!root || !cur || !txt || reduce || !matchMedia(\"(hover: hover) and (pointer: fine)\").matches) return;\n    const cards = Array.from(root.querySelectorAll<HTMLElement>(\"[data-label]\"));\n    let x = 0, y = 0, s = 0, tx = 0, ty = 0, label = \"\", live = false, frame = 0, idle = 0;\n    let hot: HTMLElement | null = null;\n    const t0 = performance.now();\n    const loop = (now: number) => {\n      const o = root.getBoundingClientRect();\n      let target = hot;\n      if (!live) {\n        const t = Math.max(0, now - t0) / IDLE_MS;\n        target = cards[Math.floor(t) % cards.length];\n        const c = target.getBoundingClientRect();\n        tx = c.left - o.left + c.width * (0.42 + 0.12 * Math.sin(t * 3)); ty = c.top - o.top + c.height * 0.4;\n      }\n      const want = target ? target.dataset.label || \"\" : \"\";\n      if (want && want !== label) { label = want; txt.textContent = want; s *= 0.6; }\n      x += (tx - x) * EASE; y += (ty - y) * EASE; s += ((target ? 1 : 0) - s) * 0.18;\n      cur.style.transform = `translate(${x}px, ${y}px) scale(${s})`;\n      frame = requestAnimationFrame(loop);\n    };\n    const start = () => { if (!frame) frame = requestAnimationFrame(loop); };\n    const stop = () => { cancelAnimationFrame(frame); frame = 0; };\n    const onMove = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") return;\n      const o = root.getBoundingClientRect();\n      tx = e.clientX - o.left; ty = e.clientY - o.top; hot = (e.target as HTMLElement).closest<HTMLElement>(\"[data-label]\");\n      live = true; clearTimeout(idle); root.setAttribute(\"data-live\", \"\"); root.removeAttribute(\"data-kb\");\n    };\n    const onLeave = () => { hot = null; root.removeAttribute(\"data-live\"); idle = window.setTimeout(() => { live = false; }, 1500); };\n    const onFocus = () => { if (root.querySelector(\":focus-visible\")) root.setAttribute(\"data-kb\", \"\"); };\n    const onBlur = () => root.removeAttribute(\"data-kb\");\n    const io = new IntersectionObserver(([e]) => (e.isIntersecting ? start() : stop()));\n    io.observe(root);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"focusin\", onFocus);\n    root.addEventListener(\"focusout\", onBlur);\n    return () => {\n      stop(); io.disconnect(); clearTimeout(idle);\n      root.removeAttribute(\"data-live\");\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"focusin\", onFocus);\n      root.removeEventListener(\"focusout\", onBlur);\n    };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"w2lbl-root relative overflow-hidden bg-[#eef1ea] px-5 pb-14 pt-12 text-[#1d2b22] sm:px-10 sm:py-16\">\n      <style>{`\n        .w2lbl-card img { transition: transform .7s cubic-bezier(.2,.8,.2,1); }\n        .w2lbl-card:hover img, .w2lbl-card:focus-visible img { transform: scale(1.04); }\n        .w2lbl-strip { scrollbar-width: none; user-select: none; scroll-snap-type: x mandatory; }\n        .w2lbl-cur { position: absolute; left: 0; top: 0; width: 92px; height: 92px; margin: -46px 0 0 -46px; border-radius: 50%; background: #1d2b22; color: #f1efe6;\n          display: grid; place-items: center; pointer-events: none; will-change: transform; box-shadow: 0 10px 30px -12px rgba(29,43,34,.6); transform: scale(0); }\n        .w2lbl-root[data-kb] .w2lbl-cur { opacity: 0; }\n        @media (hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference) {\n          .w2lbl-root[data-live] [data-label], .w2lbl-root[data-live] [data-label] * { cursor: none; }\n        }\n        @media (prefers-reduced-motion: reduce) { .w2lbl-cur { display: none; } .w2lbl-card img { transition: none; } }\n      `}</style>\n      <div className=\"mx-auto max-w-6xl\">\n        <div className=\"flex flex-wrap items-end justify-between gap-x-8 gap-y-2\">\n          <h2 className=\"font-serif text-[clamp(1.9rem,4vw,2.75rem)] font-normal\">Recent gardens</h2>\n          <p className=\"max-w-sm text-[15px] leading-relaxed text-[#3f4d43]\">Ferncombe &amp; Lowe design planted gardens for farmhouses, schools and small town yards.</p>\n        </div>\n        <div className=\"mt-8 grid gap-4 md:grid-cols-3 md:gap-5\">\n          <a href=\"#\" data-label=\"View\" className={`${card} ${ring}`}>\n            <img src=\"https://images.pexels.com/photos/38162588/pexels-photo-38162588.jpeg?auto=compress&cs=tinysrgb&w=800\" alt=\"Pale violet bearded irises flowering along a weathered wooden picket fence\" loading=\"lazy\" width={800} height={1200} className=\"absolute inset-0 h-full w-full object-cover\" />\n            <Caption title=\"Iris walk, Holloway Farm\" chip=\"View project\" />\n          </a>\n          <div data-label=\"Drag\" className={`${card} bg-[#e4e8de]`}>\n            <div ref={stripRef} tabIndex={0} role=\"region\" aria-label=\"Plant palette, scrolls sideways\" className={`w2lbl-strip absolute inset-x-0 bottom-[72px] top-0 flex gap-3 overflow-x-auto p-4 ${ring}`}>\n              {plants.map((p) => (\n                <div key={p.name} className=\"flex flex-[0_0_62%] snap-start flex-col justify-end gap-0.5 rounded-[10px] bg-[#f7f8f4] bg-no-repeat p-3\" style={{ backgroundImage: `linear-gradient(${p.c}, ${p.c})`, backgroundSize: \"100% 64%\" }}>\n                  <b className=\"text-[15px]\">{p.name}</b>\n                  <span className=\"text-[13px] text-[#3f4d43]\">{p.note}</span>\n                </div>\n              ))}\n            </div>\n            <Caption title=\"The spring plant palette\" chip=\"Drag to browse\" />\n          </div>\n          <div data-label=\"Play\" className={card}>\n            <img src=\"https://images.pexels.com/photos/30371404/pexels-photo-30371404.jpeg?auto=compress&cs=tinysrgb&w=800\" alt=\"A gardener's hands setting young seedlings into dark, freshly turned soil\" loading=\"lazy\" width={800} height={533} className=\"absolute inset-0 h-full w-full object-cover brightness-[.72]\" />\n            <button type=\"button\" aria-pressed={playing} aria-label=\"Play the film: planting a spring border\" onClick={() => setPlaying((v) => !v)} className={`absolute left-1/2 top-[42%] -ml-8 -mt-8 grid h-16 w-16 place-items-center rounded-full ${playing ? \"bg-[#1d2b22] text-[#f1efe6]\" : \"bg-[#f7f8f4]/90 text-[#1d2b22]\"} ${ring}`}>\n              <svg viewBox=\"0 0 24 24\" width=\"22\" height=\"22\" aria-hidden=\"true\"><path d=\"M8 5v14l11-7z\" fill=\"currentColor\" /></svg>\n            </button>\n            <Caption title=\"Planting a spring border\" chip=\"Play film\" />\n          </div>\n        </div>\n      </div>\n      <span ref={curRef} className=\"w2lbl-cur\" aria-hidden=\"true\"><span ref={txtRef} className=\"text-xs font-bold uppercase tracking-[0.16em]\">View</span></span>\n    </section>\n  );\n}\n"}]}
