{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"black-glass-pill-navbar-with-a-mercury-drop-that-stretches-to-the-active-link","type":"registry:component","title":"Black Glass Pill Navbar with a Mercury Drop That Stretches to the Active Link","description":"Pill navbar where a drop of liquid metal marks the current page and stretches into a thread as it travels. Free React code and an HTML version to copy.","dependencies":[],"files":[{"path":"components/we-inc/black-glass-pill-navbar-with-a-mercury-drop-that-stretches-to-the-active-link.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Edit the menu here. Four to six short words fit the tray at phone width.\nconst links = [\"Index\", \"Projects\", \"Notes\", \"Lab\", \"Hello\"];\nconst RES = 2; // canvas pixels per CSS pixel\nconst BRIGHT = [246, 248, 252]; // the brightest reflection in the metal\nconst DEEP = [13, 15, 21]; // the darkest\nconst INK = [12, 13, 16]; // label colour while it sits on the mercury\nconst PALE = [214, 217, 224]; // label colour on the black glass\n\nconst clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);\nconst smooth = (a: number, b: number, v: number) => { const t = clamp01((v - a) / (b - a)); return t * t * (3 - 2 * t); };\n\nexport default function MercuryDropPillNavbar() {\n  const well = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const items = useRef<(HTMLAnchorElement | null)[]>([]);\n  const [active, setActive] = useState(1);\n  const activeRef = useRef(1);\n  const api = useRef<{ go: (i: number, fromUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const el = well.current, cv = canvas.current;\n    if (!el || !cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    let W = 0, H = 0, slot = 0;\n    let fld = new Float32Array(0), hgt = new Float32Array(0);\n    let out: ImageData | null = null, px32 = new Uint32Array(0);\n    let centres: number[] = [];\n    // The drop is one long body (lead and trail) plus two beads that break away and catch up.\n    const s = { lead: 0, leadV: 0, trail: 0, trailV: 0, b1: 0, b1V: 0, b2: 0, b2V: 0, squash: 0, squashV: 0, pull: 0, pullTo: 0, lift: 0, liftTo: 0, ready: false };\n\n    const measure = () => {\n      const b = el.getBoundingClientRect();\n      if (!b.width) return;\n      W = Math.round(b.width * RES); H = Math.round(b.height * RES);\n      cv.width = W; cv.height = H;\n      fld = new Float32Array(W * H); hgt = new Float32Array(W * H);\n      out = ctx.createImageData(W, H); px32 = new Uint32Array(out.data.buffer);\n      centres = items.current.map((a) => { if (!a) return 0; const k = a.getBoundingClientRect(); return (k.left - b.left + k.width / 2) * RES; });\n      slot = W / Math.max(1, links.length);\n      if (!s.ready && centres.length) { s.lead = s.trail = s.b1 = s.b2 = centres[activeRef.current]; s.ready = true; }\n    };\n\n    const paint = () => {\n      if (!out) return;\n      px32.fill(0);\n      const dst = out.data;\n      const xmin = Math.min(s.lead, s.trail), xmax = Math.max(s.lead, s.trail), stretch = xmax - xmin;\n      // The same amount of metal: a longer drop is a thinner drop.\n      const R = H * 0.365 * Math.max(0.6, 1 - stretch / (slot * 5.2)) * (1 - s.squash * 0.1);\n      const L0 = Math.max(0, Math.min(slot * 0.5 - R - 3 * RES, R * 0.82)) * (1 + s.squash * 0.25);\n      const wall = R * 1.04 + 2 * RES; // the drop piles up against the end of the tray, it never passes through\n      const xa = Math.max(wall, xmin - L0 + s.pull * RES), xb = Math.min(W - wall, xmax + L0 + s.pull * RES);\n      const cy = H * 0.5 + s.lift * RES, R2 = R * R;\n      const r1 = R * 0.46, r2 = R * 0.33, r12 = r1 * r1, r22 = r2 * r2;\n      const b1x = s.b1 + s.pull * RES * 1.9, b2x = s.b2 + s.pull * RES * 0.6;\n      const b1y = cy + R * 0.2 + s.lift * RES * 0.8, b2y = cy - R * 0.26;\n      const x0 = Math.max(1, Math.floor(Math.min(xa, b1x, b2x) - R * 2.3)), x1 = Math.min(W - 2, Math.ceil(Math.max(xb, b1x, b2x) + R * 2.3));\n      const HM = R * 0.6;\n\n      for (let y = 0; y < H; y++) {\n        const dy = (y - cy) * 1.06, dy2 = dy * dy, e1y = y - b1y, e2y = y - b2y;\n        for (let x = x0 - 1; x <= x1 + 1; x++) {\n          const gap = x < xa ? xa - x : x > xb ? x - xb : 0;\n          const e1x = x - b1x, e2x = x - b2x;\n          const F = R2 / (gap * gap + dy2 + 1) + r12 / (e1x * e1x + e1y * e1y + 1) + r22 / (e2x * e2x + e2y * e2y + 1);\n          const i = y * W + x;\n          fld[i] = F;\n          if (F <= 1) hgt[i] = 0; else { const q = 1 / F; hgt[i] = HM * Math.sqrt(1 - q * Math.sqrt(q)); } // a shallow lens with a steep rim\n        }\n      }\n      for (let y = 1; y < H - 1; y++) {\n        for (let x = x0; x <= x1; x++) {\n          const i = y * W + x, F = fld[i];\n          if (F < 0.3) continue;\n          const o = i * 4;\n          // A faint smear of light on the glass around the drop.\n          const ring = F < 1 ? (F - 0.3) / 0.7 : 1, ga = ring * ring * ring * 0.13 * smooth(-R * 0.2, R * 0.9, y - cy);\n          const fx = (fld[i + 1] - fld[i - 1]) * 0.5, fy = (fld[i + W] - fld[i - W]) * 0.5;\n          const a = clamp01((F - 1) / (Math.sqrt(fx * fx + fy * fy) + 1e-4) + 0.5);\n          if (a <= 0) { dst[o] = 205; dst[o + 1] = 214; dst[o + 2] = 232; dst[o + 3] = ga * 255; continue; }\n          const nx = -(hgt[i + 1] - hgt[i - 1]) * 0.5, ny = -(hgt[i + W] - hgt[i - W]) * 0.5;\n          const inv = 1 / Math.sqrt(nx * nx + ny * ny + 1), nz = inv;\n          // Where the mirror points: straight up on the flat top, at the horizon on the shoulder, at the table on the rim.\n          const rx = 2 * nz * nx * inv, ry = 2 * nz * ny * inv, rz = 2 * nz * nz - 1;\n          let e: number;\n          if (rz > 0) {\n            const sky = 0.5 + 0.2 * rz + 0.24 * smooth(0.1, -0.9, ry) - 0.26 * smooth(0.08, 0.85, ry);\n            e = 0.035 + (sky - 0.035) * smooth(0, 0.42, rz);\n            const k = 1 / (rz + 0.28), u = rx * k, v = ry * k;\n            e += (1 - e) * smooth(-0.98, -0.86, u) * smooth(0.42, 0.3, u) * smooth(-1.02, -0.9, v) * smooth(-0.3, -0.4, v) * 0.97; // the long lamp, upper left\n            e += (1 - e) * smooth(0.62, 0.7, u) * smooth(0.92, 0.84, u) * smooth(-0.8, -0.6, v) * smooth(0.75, 0.45, v) * 0.5; // a strip light on the right\n          } else {\n            e = 0.035 + 0.42 * smooth(-0.35, -0.97, rz) * smooth(-0.15, 0.75, ry); // the rim picks up the room only along its lower edge\n          }\n          const warm = rz < 0 ? 0 : smooth(0.1, 0.9, ry) * 10;\n          const cr = DEEP[0] + (BRIGHT[0] - DEEP[0]) * e + warm, cg = DEEP[1] + (BRIGHT[1] - DEEP[1]) * e + warm * 0.4, cb = DEEP[2] + (BRIGHT[2] - DEEP[2]) * e - warm * 0.5;\n          const oa = a + (1 - a) * ga;\n          dst[o] = (cr * a + 205 * ga * (1 - a)) / oa; dst[o + 1] = (cg * a + 214 * ga * (1 - a)) / oa; dst[o + 2] = (cb * a + 232 * ga * (1 - a)) / oa; dst[o + 3] = oa * 255;\n        }\n      }\n      ctx.putImageData(out, 0, 0);\n      // A label turns to ink while the metal is under it.\n      const edge = R * 0.55;\n      items.current.forEach((link, i) => {\n        if (!link) return;\n        const c = centres[i], d = c < xa ? xa - c : c > xb ? c - xb : 0, k = 1 - smooth(-edge, edge * 0.6, d - edge);\n        link.style.color = `rgb(${Math.round(PALE[0] + (INK[0] - PALE[0]) * k)},${Math.round(PALE[1] + (INK[1] - PALE[1]) * k)},${Math.round(PALE[2] + (INK[2] - PALE[2]) * k)})`;\n      });\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 1300, dirty = true;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      const t = now - t0, target = centres[activeRef.current] ?? 0;\n      s.leadV += (300 * (target - s.lead) - 21 * s.leadV) * dt; s.lead += s.leadV * dt;\n      s.trailV += (120 * (target - s.trail) - 15 * s.trailV) * dt; s.trail += s.trailV * dt;\n      s.b1V += (58 * (target - s.b1) - 10.5 * s.b1V) * dt; s.b1 += s.b1V * dt;\n      s.b2V += (36 * (target - s.b2) - 8.6 * s.b2V) * dt; s.b2 += s.b2V * dt;\n      // Arriving fast flattens the drop for a moment, then surface tension pulls it round again.\n      const hit = Math.min(1, Math.abs(s.leadV) / (slot * 9));\n      s.squashV += (260 * (hit * 0.9 - s.squash) - 11 * s.squashV) * dt; s.squash += s.squashV * dt;\n      s.pull += (s.pullTo - s.pull) * Math.min(1, dt * 9); s.lift += (s.liftTo - s.lift) * Math.min(1, dt * 9);\n      if (now - touched > 4200 && t > nextDemo) { const n = (activeRef.current + 1) % links.length; activeRef.current = n; setActive(n); nextDemo = t + 2300; }\n      const moving = Math.abs(s.leadV) + Math.abs(s.trailV) + Math.abs(s.b1V) + Math.abs(s.b2V) > 0.5 || Math.abs(target - s.b2) > 0.3 || Math.abs(s.squashV) > 0.01 || Math.abs(s.pullTo - s.pull) + Math.abs(s.liftTo - s.lift) > 0.03;\n      if (moving || dirty) { paint(); dirty = false; }\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const still = () => { const x = centres[activeRef.current] ?? 0; s.lead = s.trail = s.b1 = s.b2 = x; s.leadV = s.trailV = s.b1V = s.b2V = 0; s.squash = 0; s.pull = s.pullTo; s.lift = s.liftTo; paint(); };\n\n    api.current = { go: (i, fromUser) => { if (fromUser) touched = performance.now(); activeRef.current = i; if (reduce) still(); else wake(); } };\n    const setup = () => { measure(); dirty = true; if (reduce) still(); else wake(); };\n    setup();\n\n    // The drop leans toward a pointer, as if a magnet were moving under the glass.\n    const onMove = (e: PointerEvent) => {\n      const b = el.getBoundingClientRect();\n      const dx = e.clientX - b.left - s.lead / RES, dy = e.clientY - b.top - b.height / 2;\n      const near = 1 - smooth(40, 230, Math.abs(dx));\n      s.pullTo = Math.max(-11, Math.min(11, dx * 0.16)) * near; s.liftTo = Math.max(-3.5, Math.min(3.5, dy * 0.14)) * near;\n      touched = Math.max(touched, performance.now() - 2600); dirty = true; if (reduce) still();\n    };\n    const onLeave = () => { s.pullTo = 0; s.liftTo = 0; dirty = true; if (reduce) still(); };\n    const onDown = () => { s.squashV += 7; touched = performance.now(); dirty = true; };\n    el.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerleave\", onLeave);\n    el.addEventListener(\"pointerdown\", onDown);\n    const ro = new ResizeObserver(setup);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(el);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      el.removeEventListener(\"pointermove\", onMove); el.removeEventListener(\"pointerleave\", onLeave); el.removeEventListener(\"pointerdown\", onDown);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const choose = (i: number) => { activeRef.current = i; setActive(i); api.current?.go(i, true); };\n\n  return (\n    <div className=\"flex min-h-[520px] w-full flex-col items-center justify-center bg-[#d5d7dd] bg-[radial-gradient(110%_85%_at_50%_18%,#eceef2_0%,#d5d7dd_55%,#bfc2ca_100%)] px-4 py-16 sm:min-h-[640px]\">\n      <nav\n        aria-label=\"Main\"\n        className=\"w-full max-w-[344px] rounded-full bg-[linear-gradient(180deg,#5d6068_0%,#2a2c31_22%,#17181b_60%,#3a3c42_100%)] p-[5px] shadow-[0_1px_0_rgba(255,255,255,0.75),0_-1px_0_rgba(0,0,0,0.25),0_42px_60px_-22px_rgba(28,32,48,0.55),0_14px_20px_-10px_rgba(28,32,48,0.45)] sm:max-w-[700px] sm:p-[6px]\"\n      >\n        <div\n          ref={well}\n          className=\"relative h-[60px] overflow-hidden rounded-full bg-[#0b0c0e] bg-[linear-gradient(180deg,#141519_0%,#08090a_55%,#0d0e11_100%)] shadow-[inset_0_2px_3px_rgba(0,0,0,0.95),inset_0_-1px_0_rgba(255,255,255,0.09),inset_0_0_0_1px_rgba(0,0,0,0.6)] sm:h-[90px]\"\n        >\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-x-[4%] top-[3px] h-[40%] rounded-full bg-[linear-gradient(180deg,rgba(255,255,255,0.06),rgba(255,255,255,0))] blur-sm\" />\n          <canvas ref={canvas} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 h-full w-full\" />\n          <ul className=\"relative grid h-full\" style={{ gridTemplateColumns: `repeat(${links.length}, minmax(0, 1fr))` }}>\n            {links.map((label, i) => (\n              <li key={label} className=\"flex\">\n                <a\n                  ref={(a) => { items.current[i] = a; }}\n                  href=\"#\"\n                  aria-current={active === i ? \"page\" : undefined}\n                  onClick={(e) => { e.preventDefault(); choose(i); }}\n                  className=\"flex flex-1 items-center justify-center rounded-full text-[13px] font-semibold tracking-[-0.01em] text-[#d6d9e0] outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-sky-300 sm:text-[17px]\"\n                >\n                  {label}\n                </a>\n              </li>\n            ))}\n          </ul>\n        </div>\n      </nav>\n      <p className=\"mt-10 max-w-[300px] text-center text-[13px] font-medium leading-5 text-slate-600 sm:mt-12\">\n        Pick a page and the mercury runs to it. Hold a pointer near and it leans.\n      </p>\n    </div>\n  );\n}\n"}]}
