{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"magnetic-icon-buttons-in-a-floating-toolbar","type":"registry:block","title":"Magnetic Icon Buttons in a Floating Toolbar","description":"","dependencies":[],"files":[{"path":"components/we-inc/magnetic-icon-buttons-in-a-floating-toolbar.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst PULL = 0.4; // share of the pointer distance each icon moves toward it\nconst RADIUS = 60; // px from an icon's centre where the pull starts\n\nconst tools = [\n  { label: \"Notes\", d: \"M5 4h10l4 4v12H5z M15 4v4h4 M8 12h8 M8 16h6\" },\n  { label: \"Search\", d: \"M11 4a7 7 0 1 1 0 14a7 7 0 0 1 0-14z M16 16l4 4\" },\n  { label: \"Calendar\", d: \"M4 6h16v14H4z M4 10h16 M8 3v5 M16 3v5\" },\n  { label: \"Inbox\", d: \"M4 13l3-8h10l3 8v6H4z M4 13h5l1 2h4l1-2h5\" },\n  { label: \"Settings\", d: \"M12 9a3 3 0 1 1 0 6a3 3 0 0 1 0-6z M12 3v3 M12 18v3 M3 12h3 M18 12h3 M5.6 5.6l2.1 2.1 M16.3 16.3l2.1 2.1 M5.6 18.4l2.1-2.1 M16.3 7.7l2.1-2.1\" },\n];\n\nexport default function MagneticIconButtonsInAFloatingToolbar() {\n  const bar = useRef<HTMLElement>(null);\n  const btns = useRef<(HTMLAnchorElement | null)[]>([]);\n\n  useEffect(() => {\n    const el = bar.current;\n    if (!el) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const fine = window.matchMedia(\"(hover: hover) and (pointer: fine)\").matches;\n    const list = btns.current.filter(Boolean) as HTMLAnchorElement[];\n    const pos = list.map(() => ({ x: 0, y: 0 }));\n    let px = 0, py = 0, live = false, frame = 0, visible = true;\n    const move = (e: PointerEvent) => { if (e.pointerType === \"mouse\") { live = true; px = e.clientX; py = e.clientY; } };\n    const leave = () => { live = false; };\n    const tick = (now: number) => {\n      const b = el.getBoundingClientRect();\n      if (!live && el.contains(document.activeElement)) { px = -9999; py = -9999; }\n      else if (!live) {\n        // idle demo: a pretend pointer glides along the bar\n        px = b.left + b.width / 2 + Math.sin(now / 1600) * b.width * 0.42;\n        py = b.top + b.height / 2 - 10;\n      }\n      let best = -1, bestD = RADIUS * 0.55;\n      list.forEach((btn, i) => {\n        const r = btn.getBoundingClientRect();\n        const cx = r.left + r.width / 2 - pos[i].x, cy = r.top + r.height / 2 - pos[i].y;\n        const dx = px - cx, dy = py - cy;\n        const near = Math.hypot(dx, dy) < RADIUS;\n        pos[i].x += ((near ? dx * PULL : 0) - pos[i].x) * 0.15;\n        pos[i].y += ((near ? dy * PULL : 0) - pos[i].y) * 0.15;\n        btn.style.transform = `translate(${pos[i].x.toFixed(2)}px, ${pos[i].y.toFixed(2)}px)`;\n        if (Math.hypot(dx, dy) < bestD) { bestD = Math.hypot(dx, dy); best = i; }\n      });\n      list.forEach((btn, i) => btn.toggleAttribute(\"data-near\", i === best));\n      frame = requestAnimationFrame(tick);\n    };\n    const play = () => { if (!frame && visible) frame = requestAnimationFrame(tick); };\n    const stop = () => { cancelAnimationFrame(frame); frame = 0; };\n    const io = new IntersectionObserver(([en]) => { visible = en.isIntersecting; if (visible) play(); else stop(); });\n    io.observe(el);\n    if (fine) { el.addEventListener(\"pointermove\", move); el.addEventListener(\"pointerleave\", leave); }\n    play();\n    return () => {\n      stop();\n      io.disconnect();\n      el.removeEventListener(\"pointermove\", move);\n      el.removeEventListener(\"pointerleave\", leave);\n    };\n  }, []);\n\n  return (\n    <section className=\"bg-[#e9edf1] px-5 py-16 sm:py-20\">\n      <style>{`\n        .w3nav-btn { will-change: transform; }\n        .w3nav-bg, .w3nav-tip { opacity: 0; transition: opacity .25s ease, transform .25s ease; }\n        .w3nav-tip { transform: translate(-50%, 6px); }\n        .w3nav-btn[data-near] .w3nav-bg, .w3nav-btn:focus-visible .w3nav-bg { opacity: 1; }\n        .w3nav-btn[data-near] .w3nav-tip, .w3nav-btn:focus-visible .w3nav-tip { opacity: 1; transform: translate(-50%, 0); }\n        .w3nav-btn:focus-visible { outline: 2px solid #1e293b; outline-offset: 3px; }\n        @media (hover: hover) and (pointer: fine) {\n          .w3nav-btn:hover .w3nav-bg { opacity: 1; }\n          .w3nav-btn:hover .w3nav-tip { opacity: 1; transform: translate(-50%, 0); }\n        }\n        @media (prefers-reduced-motion: reduce) { .w3nav-btn { transform: none !important; will-change: auto; } .w3nav-bg, .w3nav-tip { transition: none; } }\n      `}</style>\n      <div className=\"mx-auto max-w-xl rounded-3xl bg-white px-6 pb-8 pt-8 shadow-[0_1px_2px_rgba(15,23,42,0.06),0_12px_40px_rgba(15,23,42,0.08)] sm:px-10\">\n        <p className=\"text-xs font-semibold uppercase tracking-[0.2em] text-slate-600\">Thursday, 14 March</p>\n        <h2 className=\"mt-3 text-2xl font-semibold tracking-tight text-slate-900\">Site visit, Larch Street</h2>\n        <p className=\"mt-3 text-base leading-relaxed text-slate-600\">Measure the back stairs again before ordering treads. Ask Mira about the lead time on the oak.</p>\n        <div className=\"mt-6 space-y-2\" aria-hidden=\"true\">\n          <div className=\"h-2.5 w-11/12 rounded-full bg-slate-100\" />\n          <div className=\"h-2.5 w-3/4 rounded-full bg-slate-100\" />\n        </div>\n        <nav ref={bar} aria-label=\"Workspace\" className=\"mx-auto mt-12 flex w-fit items-center gap-1 rounded-full border border-slate-200 bg-slate-50 p-2 sm:gap-2\">\n          {tools.map((t, i) => (\n            <a\n              key={t.label}\n              ref={(n) => { btns.current[i] = n; }}\n              href=\"#\"\n              aria-label={t.label}\n              aria-current={i === 0 ? \"page\" : undefined}\n              className=\"w3nav-btn relative flex h-12 w-12 items-center justify-center rounded-full text-slate-700\"\n            >\n              <span aria-hidden=\"true\" className=\"w3nav-bg absolute inset-0 rounded-full bg-white shadow-[0_4px_14px_rgba(15,23,42,0.14)]\" />\n              {i === 0 && <span aria-hidden=\"true\" className=\"absolute bottom-1 h-1 w-1 rounded-full bg-slate-900\" />}\n              <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\" className=\"relative h-5 w-5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d={t.d} /></svg>\n              <span aria-hidden=\"true\" className=\"w3nav-tip pointer-events-none absolute -top-9 left-1/2 whitespace-nowrap rounded-md bg-slate-900 px-2 py-1 text-xs font-medium text-white\">{t.label}</span>\n            </a>\n          ))}\n        </nav>\n      </div>\n    </section>\n  );\n}\n"}]}
