{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"soft-silicone-pill-navbar-with-a-glowing-ball-rolling-under-the-rubber","type":"registry:component","title":"Soft Silicone Pill Navbar with a Glowing Ball Rolling Under the Rubber","description":"Floating pill navbar shaded like silicone: a glowing ball under the rubber marks the current page and stretches as it rolls. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/soft-silicone-pill-navbar-with-a-glowing-ball-rolling-under-the-rubber.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Edit the menu here. Four to six short words fit the pill at phone width.\nconst links = [\"Work\", \"Studio\", \"Journal\", \"Shop\", \"Contact\"];\nconst RUBBER = [238, 233, 223]; // the silicone, as red, green, blue\nconst GLOW = [255, 62, 104]; // the lit ball under it\nconst RES = 2; // canvas pixels per CSS pixel\n\nexport default function SoftSiliconePillNavbar() {\n  const nav = useRef<HTMLElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const items = useRef<(HTMLAnchorElement | null)[]>([]);\n  const [active, setActive] = useState(0);\n  const activeRef = useRef(0);\n  const api = useRef<{ go: (i: number, fromUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const el = nav.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;\n    let base = new Float32Array(0), alpha = new Float32Array(0), grain = new Float32Array(0), height = new Float32Array(0), lit = new Float32Array(0), halo = new Float32Array(0);\n    let out: ImageData | null = null;\n    let centres: number[] = [];\n\n    const s = { lead: 0, leadV: 0, trail: 0, trailV: 0, press: 0, pressV: 0, pressTo: 0, dent: 0, dentTo: 0, px: 0, py: 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      const n = W * H;\n      base = new Float32Array(n); alpha = new Float32Array(n); grain = new Float32Array(n); height = new Float32Array(n); lit = new Float32Array(n); halo = new Float32Array(n);\n      out = ctx.createImageData(W, H);\n      const r = H / 2, half = W / 2 - r, edge = r * 0.62, rise = H * 0.1;\n      let seed = 11;\n      for (let y = 0; y < H; y++) {\n        for (let x = 0; x < W; x++) {\n          const px = x + 0.5 - W / 2, py = y + 0.5 - H / 2, q = Math.abs(px) - half;\n          const d = q <= 0 ? r - Math.abs(py) : r - Math.hypot(q, py);\n          const i = y * W + x, t = Math.max(0, Math.min(1, d / edge));\n          alpha[i] = Math.max(0, Math.min(1, d + 0.5));\n          base[i] = rise * (1 - (1 - t) * (1 - t)); // the pad is flat on top with soft rolled edges\n          seed = (seed * 16807) % 2147483647;\n          grain[i] = (seed / 2147483647 - 0.5) * 5;\n        }\n      }\n      centres = items.current.map((a) => { if (!a) return 0; const k = a.getBoundingClientRect(); return (k.left - b.left + k.width / 2) * RES; });\n      if (!s.ready && centres.length) { s.lead = s.trail = centres[activeRef.current]; s.ready = true; }\n    };\n\n    const paint = () => {\n      if (!out) return;\n      const dst = out.data;\n      const xa = Math.min(s.lead, s.trail), xb = Math.max(s.lead, s.trail), cy = H / 2;\n      const stretch = xb - xa;\n      const A = H * 0.215 * Math.max(0.5, 1 - stretch / (260 * RES)); // a stretched bump is a lower bump\n      const rad = H * 0.4, reach = rad * rad, inv = 1 / reach;\n      const D = s.dent * RES, dsig = 12 * RES, dinv = 1 / (2 * dsig * dsig), dreach = dsig * dsig * 11;\n      const dx0 = s.px * RES, dy0 = s.py * RES;\n      for (let y = 0; y < H; y++) {\n        const dy = y - cy, dy2 = dy * dy, ey = y - dy0, ey2 = ey * ey;\n        for (let x = 0; x < W; x++) {\n          const i = y * W + x;\n          if (alpha[i] <= 0) { height[i] = 0; lit[i] = 0; halo[i] = 0; continue; }\n          const gap = x < xa ? xa - x : x > xb ? x - xb : 0, d2 = gap * gap + dy2;\n          let h = base[i], g = 0;\n          if (d2 < reach) { const u = 1 - d2 * inv; g = u * Math.sqrt(u); h += A * g; } // a dome that meets the sheet without a crease\n          if (D > 0.05) { const ex = x - dx0, e2 = ex * ex + ey2; if (e2 < dreach) h -= D * (Math.exp(-e2 * dinv) - 0.0041); }\n          height[i] = h; lit[i] = g; halo[i] = d2 < reach * 5 ? Math.exp(-d2 * inv * 1.15) : 0;\n        }\n      }\n      // Light from the upper left. Rubber is matte, so the highlight is wide and faint.\n      const Lx = -0.37, Ly = -0.59, Lz = 0.72, Hx = -0.23, Hy = -0.37, Hz = 0.9;\n      const [R, G, B] = RUBBER, [gr, gg, gb] = GLOW;\n      for (let y = 0; y < H; y++) {\n        for (let x = 0; x < W; x++) {\n          const i = y * W + x, o = i * 4, a = alpha[i];\n          if (a <= 0) { dst[o + 3] = 0; continue; }\n          const nx = (x > 0 ? height[i - 1] : 0) - (x < W - 1 ? height[i + 1] : 0);\n          const ny = (y > 0 ? height[i - W] : 0) - (y < H - 1 ? height[i + W] : 0);\n          const len = Math.sqrt(nx * nx * 0.25 + ny * ny * 0.25 + 1);\n          const diff = Math.max(0, (nx * 0.5 * Lx + ny * 0.5 * Ly + Lz) / len);\n          let sp = Math.max(0, (nx * 0.5 * Hx + ny * 0.5 * Hy + Hz) / len); sp *= sp; sp *= sp; sp *= sp; sp *= sp;\n          const shade = 0.5 + 0.6 * diff, hi = sp * 30 + grain[i];\n          // Light from the ball soaks into the skin above it, strongest where the sheet is stretched thinnest.\n          const g = lit[i], k = Math.min(0.93, g * (0.5 + 0.42 * g) + halo[i] * 0.36), core = g * g * g * 46, warm = 0.66 + 0.66 * diff;\n          dst[o] = (R * shade + hi) * (1 - k) + gr * warm * k + core;\n          dst[o + 1] = (G * shade + hi) * (1 - k) + gg * warm * k + core;\n          dst[o + 2] = (B * shade + hi) * (1 - k) + gb * warm * k + core;\n          dst[o + 3] = a * 255;\n        }\n      }\n      ctx.putImageData(out, 0, 0);\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 1250, 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      // Two springs of different stiffness: the front of the bump arrives first and the back catches up.\n      s.leadV += (330 * (target - s.lead) - 24 * s.leadV) * dt; s.lead += s.leadV * dt;\n      s.trailV += (120 * (target - s.trail) - 17 * s.trailV) * dt; s.trail += s.trailV * dt;\n      s.pressV += (420 * (s.pressTo - s.press) - 15 * s.pressV) * dt; s.press += s.pressV * dt;\n      s.dent += (s.dentTo - s.dent) * Math.min(1, dt * 16);\n      if (now - touched > 4200 && t > nextDemo) { const n = (activeRef.current + 1) % links.length; activeRef.current = n; setActive(n); nextDemo = t + 2100; }\n      const moving = Math.abs(s.leadV) + Math.abs(s.trailV) > 0.6 || Math.abs(target - s.trail) > 0.3 || Math.abs(s.dentTo - s.dent) > 0.02 || Math.abs(s.pressV) > 0.002 || Math.abs(s.pressTo - s.press) > 0.002;\n      if (moving || dirty) { paint(); dirty = false; el.style.transform = `scale(${(1 + s.press * 0.014).toFixed(4)}, ${(1 - s.press * 0.05).toFixed(4)})`; }\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 = x; s.leadV = s.trailV = 0; s.dent = s.dentTo; 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    const onMove = (e: PointerEvent) => {\n      const b = el.getBoundingClientRect();\n      s.px = e.clientX - b.left; s.py = e.clientY - b.top; touched = performance.now();\n      if (s.dentTo < 4.5) s.dentTo = 4.5;\n      dirty = true; if (reduce) still();\n    };\n    const onLeave = () => { s.dentTo = 0; s.pressTo = 0; dirty = true; if (reduce) still(); };\n    const onDown = (e: PointerEvent) => { onMove(e); s.dentTo = 11; s.pressTo = 1; if (reduce) still(); };\n    const onUp = () => { s.pressTo = 0; s.dentTo = s.dentTo > 0 ? 4.5 : 0; dirty = true; if (reduce) still(); };\n    el.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerleave\", onLeave);\n    el.addEventListener(\"pointerdown\", onDown);\n    window.addEventListener(\"pointerup\", onUp);\n    window.addEventListener(\"pointercancel\", onLeave);\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      window.removeEventListener(\"pointerup\", onUp); window.removeEventListener(\"pointercancel\", onLeave);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const choose = (i: number) => { setActive(i); api.current?.go(i, true); };\n\n  return (\n    <div className=\"flex min-h-[420px] w-full flex-col items-center justify-center bg-[#b4c3e0] px-4 py-16\">\n      <div className=\"relative w-full max-w-[452px] sm:max-w-[540px]\">\n        {/* Where the pad rests on the table. */}\n        <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-x-3 bottom-[-10px] top-6 rounded-full bg-[#1f2f5c] opacity-45 blur-[14px]\" />\n        <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-x-1 bottom-[-3px] top-3 rounded-full bg-[#1a2750] opacity-40 blur-[4px]\" />\n        <nav ref={nav} aria-label=\"Main\" className=\"relative h-[68px] w-full sm:h-[78px] touch-manipulation select-none rounded-full bg-[#f3f0ea] will-change-transform\">\n          <canvas ref={canvas} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 h-full w-full rounded-full\" />\n          <ul className=\"relative flex h-full items-stretch px-[14px]\">\n            {links.map((label, i) => (\n              <li key={label} className=\"flex min-w-0 flex-1\">\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={`my-3 flex w-full items-center justify-center rounded-full text-[13px] font-semibold tracking-tight outline-none sm:text-[15px] transition-[color,transform] duration-500 ease-out focus-visible:ring-2 focus-visible:ring-[#1c2230] focus-visible:ring-offset-0 motion-reduce:transition-none ${active === i ? \"scale-110 text-[#1c0810]\" : \"text-[#4a5061] hover:text-[#1c2230]\"}`}\n                >\n                  {label}\n                </a>\n              </li>\n            ))}\n          </ul>\n        </nav>\n      </div>\n      <p className=\"mt-10 text-center text-[13px] font-medium text-[#27355c]\">Push a finger into it. Pick a page and the light rolls there.</p>\n    </div>\n  );\n}\n"}]}
