{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"navbar-with-liquid-stretch-highlight-and-inverting-labels","type":"registry:block","title":"Navbar with Liquid Stretch Highlight and Inverting Labels","description":"Navbar where a lime blob stretches between links like liquid and the label text inverts exactly where the blob covers it. Free React and HTML code to copy.","dependencies":["framer-motion","lucide-react"],"files":[{"path":"components/we-inc/navbar-with-liquid-stretch-highlight-and-inverting-labels.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { animate, motion, useInView, useMotionTemplate, useMotionValue, useReducedMotion, useTransform } from \"framer-motion\";\nimport { ArrowUpRight } from \"lucide-react\";\n\nconst links = [\"Work\", \"Studio\", \"Journal\", \"Shop\", \"Contact\"];\n\n// The edge that leads the move is stiff, the edge that trails is loose: that gap is the stretch.\nconst LEAD = { type: \"spring\", stiffness: 520, damping: 34, mass: 0.7 } as const;\nconst TRAIL = { type: \"spring\", stiffness: 150, damping: 15, mass: 0.9 } as const;\nconst PAD = 5; // gap between the blob and the edge of the bar\nconst DEMO_MS = 1150;\n\nconst chairs = [\n  { name: \"Oda lounge chair\", price: \"$1,240\", tone: \"bg-lime-300\", seat: \"#1c1917\" },\n  { name: \"Tern side table\", price: \"$380\", tone: \"bg-stone-900\", seat: \"#bef264\" },\n  { name: \"Halde bench\", price: \"$910\", tone: \"bg-orange-200\", seat: \"#1c1917\" },\n];\n\nexport default function NavbarWithLiquidStretchHighlightAndInvertingLabels() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLDivElement>(null);\n  const barRef = useRef<HTMLUListElement>(null);\n  const itemRefs = useRef<(HTMLLIElement | null)[]>([]);\n  const inView = useInView(rootRef, { amount: 0.3 });\n  const [active, setActive] = useState(0);\n  const [hover, setHover] = useState<number | null>(null);\n  const [demo, setDemo] = useState(true);\n  const [ready, setReady] = useState(false);\n\n  const left = useMotionValue(0);\n  const right = useMotionValue(0);\n  const rest = useMotionValue(1); // width the blob settles at\n  const barW = useMotionValue(0);\n  const target = hover ?? active;\n\n  // Squash the blob vertically while it is stretched wider than its resting width.\n  const squash = useTransform([left, right, rest], ([l, r, w]: number[]) => {\n    const extra = Math.max(0, r - l - w);\n    return PAD + Math.min(7, extra * 0.07);\n  });\n  const rightInset = useTransform([right, barW], ([r, w]: number[]) => Math.max(0, w - r));\n  const clip = useMotionTemplate`inset(${squash}px ${rightInset}px ${squash}px ${left}px round 999px)`;\n\n  const moveTo = useCallback(\n    (index: number, instant: boolean) => {\n      const bar = barRef.current;\n      const el = itemRefs.current[index];\n      if (!bar || !el) return;\n      const l = el.offsetLeft;\n      const r = l + el.offsetWidth;\n      barW.set(bar.offsetWidth);\n      rest.set(el.offsetWidth);\n      if (instant) {\n        left.set(l);\n        right.set(r);\n        return;\n      }\n      const goingRight = l >= left.get();\n      animate(left, l, goingRight ? TRAIL : LEAD);\n      animate(right, r, goingRight ? LEAD : TRAIL);\n    },\n    [barW, left, rest, right],\n  );\n\n  // First paint and every resize: snap, do not animate.\n  useEffect(() => {\n    const bar = barRef.current;\n    if (!bar) return;\n    moveTo(target, true);\n    setReady(true);\n    const ro = new ResizeObserver(() => moveTo(target, true));\n    ro.observe(bar);\n    return () => ro.disconnect();\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, []);\n\n  useEffect(() => {\n    if (!ready) return;\n    moveTo(target, !!reduce);\n    return () => {\n      left.stop();\n      right.stop();\n    };\n  }, [target, ready, reduce, moveTo, left, right]);\n\n  // Idle demo: the blob visits each link in turn until someone takes over.\n  useEffect(() => {\n    if (!demo || reduce || !inView) return;\n    const order = [1, 3, 2, 4, 0];\n    let step = 0;\n    const tick = () => {\n      if (document.hidden) return;\n      setActive(order[step % order.length]);\n      step += 1;\n    };\n    let id = window.setTimeout(function run() {\n      tick();\n      id = window.setTimeout(run, DEMO_MS);\n    }, 1400);\n    return () => window.clearTimeout(id);\n  }, [demo, reduce, inView]);\n\n  const stop = () => setDemo(false);\n\n  return (\n    <section ref={rootRef} className=\"bg-stone-100 px-4 py-6 text-stone-900 sm:px-8 sm:py-8\">\n      <div className=\"mx-auto max-w-5xl\">\n        <header className=\"flex flex-col items-center gap-4 sm:flex-row sm:justify-between\">\n          <a href=\"#\" className=\"flex items-center gap-2 rounded-full text-lg font-semibold tracking-tight focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-4 focus-visible:ring-offset-stone-100\">\n            <svg viewBox=\"0 0 24 24\" className=\"h-6 w-6\" aria-hidden=\"true\">\n              <circle cx=\"8\" cy=\"12\" r=\"6\" fill=\"#1c1917\" />\n              <circle cx=\"16\" cy=\"12\" r=\"6\" fill=\"#a3e635\" />\n            </svg>\n            Nordlow\n          </a>\n\n          <nav aria-label=\"Main\" className=\"w-full sm:w-auto\" onPointerDown={stop} onFocusCapture={stop}>\n            <ul\n              ref={barRef}\n              onPointerLeave={() => setHover(null)}\n              className=\"relative flex rounded-full bg-stone-900 shadow-lg shadow-stone-900/20\"\n              style={{ padding: 0 }}\n            >\n              {links.map((label, i) => (\n                <li key={label} ref={(el) => { itemRefs.current[i] = el; }} className=\"flex-1 sm:flex-none\">\n                  <a\n                    href=\"#\"\n                    aria-current={active === i ? \"page\" : undefined}\n                    onClick={(e) => {\n                      e.preventDefault();\n                      stop();\n                      setActive(i);\n                    }}\n                    onPointerEnter={(e) => {\n                      if (e.pointerType === \"mouse\") {\n                        stop();\n                        setHover(i);\n                      }\n                    }}\n                    onFocus={() => setHover(i)}\n                    onBlur={() => setHover(null)}\n                    className=\"group flex h-12 items-center justify-center rounded-full px-1 text-sm font-medium text-stone-300 focus-visible:outline-none sm:px-5\"\n                  >\n                    <span className=\"rounded-full px-0.5 sm:px-1 group-focus-visible:ring-2 group-focus-visible:ring-white group-focus-visible:ring-offset-4 group-focus-visible:ring-offset-stone-900\">{label}</span>\n                  </a>\n                </li>\n              ))}\n\n              {/* One layer is both the blob and the dark copy of the labels. Its clip-path is the liquid shape. */}\n              <motion.li\n                aria-hidden=\"true\"\n                role=\"presentation\"\n                className=\"pointer-events-none absolute inset-0 flex rounded-full bg-lime-300\"\n                style={{ clipPath: clip, opacity: ready ? 1 : 0, willChange: \"clip-path\" }}\n              >\n                {links.map((label) => (\n                  <span key={label} className=\"flex h-12 flex-1 items-center justify-center px-1 text-sm font-medium text-stone-950 sm:flex-none sm:px-5\">\n                    <span className=\"px-0.5 sm:px-1\">{label}</span>\n                  </span>\n                ))}\n              </motion.li>\n            </ul>\n          </nav>\n\n          <a href=\"#\" className=\"hidden h-12 items-center gap-1.5 rounded-full border border-stone-900 px-5 text-sm font-semibold transition-colors hover:bg-stone-900 hover:text-stone-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-2 focus-visible:ring-offset-stone-100 lg:flex\">\n            Book a visit\n            <ArrowUpRight className=\"h-4 w-4\" aria-hidden=\"true\" />\n          </a>\n        </header>\n\n        <div className=\"mt-10 grid items-end gap-8 sm:mt-14 lg:grid-cols-5\">\n          <div className=\"lg:col-span-3\">\n            <p className=\"text-sm font-medium text-stone-600\">Autumn collection, made in small runs</p>\n            <h1 className=\"mt-3 text-4xl font-semibold leading-none tracking-tight sm:text-6xl\">\n              Furniture that keeps its shape and loses its edges.\n            </h1>\n            <p className=\"mt-5 max-w-md text-base text-stone-600\">\n              Steam-bent ash, wool from one mill and joints you can take apart with a coin. Every piece ships flat and outlives the sofa it replaces.\n            </p>\n            <div className=\"mt-7 flex flex-wrap gap-3\">\n              <a href=\"#\" className=\"flex h-12 items-center rounded-full bg-stone-900 px-6 text-sm font-semibold text-stone-50 transition-transform hover:-translate-y-0.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-2 focus-visible:ring-offset-stone-100 motion-reduce:transition-none\">\n                See the collection\n              </a>\n              <a href=\"#\" className=\"flex h-12 items-center rounded-full px-4 text-sm font-semibold underline decoration-lime-500 decoration-2 underline-offset-4 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900\">\n                Order fabric samples\n              </a>\n            </div>\n          </div>\n\n          <ul className=\"grid grid-cols-3 gap-3 lg:col-span-2\">\n            {chairs.map((c, i) => (\n              <li key={c.name} className={i === 1 ? \"sm:-translate-y-6\" : \"\"}>\n                <div className={`flex aspect-[3/4] items-end justify-center rounded-3xl ${c.tone}`}>\n                  <svg viewBox=\"0 0 60 80\" className=\"w-3/4\" aria-hidden=\"true\">\n                    <rect x=\"14\" y=\"14\" width=\"32\" height=\"30\" rx=\"14\" fill={c.seat} />\n                    <rect x=\"8\" y=\"42\" width=\"44\" height=\"12\" rx=\"6\" fill={c.seat} />\n                    <rect x=\"14\" y=\"54\" width=\"4\" height=\"22\" rx=\"2\" fill={c.seat} />\n                    <rect x=\"42\" y=\"54\" width=\"4\" height=\"22\" rx=\"2\" fill={c.seat} />\n                  </svg>\n                </div>\n                <p className=\"mt-2 text-xs font-semibold leading-tight sm:text-sm\">{c.name}</p>\n                <p className=\"text-xs text-stone-600\">{c.price}</p>\n              </li>\n            ))}\n          </ul>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
