Glide nav
A floating pill navigation bar where the highlight stretches like elastic from link to link, then folds into a menu on small screens.
"use client";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import "./glide-nav.css";
export type GlideLink = { label: string; href: string };
type GlideNavProps = {
links: GlideLink[];
brand?: string;
cta?: { label: string; href: string };
/** Which link starts active (index). */
initial?: number;
theme?: "light" | "dark";
};
/**
* GlideNav
* - A dark pill sits behind the hovered (or active) link.
* - When it moves, the leading edge goes first and the trailing edge follows a beat later,
* so the pill stretches like elastic instead of sliding stiffly.
* - Below 620px of available width the links fold into a menu button.
*/
export function GlideNav({
links,
brand = "Northwind",
cta,
initial = 0,
theme = "light",
}: GlideNavProps) {
const [active, setActive] = useState(initial);
const [hover, setHover] = useState<number | null>(null);
const [open, setOpen] = useState(false);
const list = useRef<HTMLUListElement>(null);
const pill = useRef<HTMLSpanElement>(null);
const last = useRef(initial);
const target = hover ?? active;
// Place the pill under the target link. Which edge leads depends on the direction.
useLayoutEffect(() => {
const ul = list.current;
const el = pill.current;
if (!ul || !el) return;
const place = () => {
const a = ul.querySelectorAll("a")[target] as HTMLElement | undefined;
if (!a) return;
const right = target > last.current;
el.style.setProperty("--l", `${a.offsetLeft}px`);
el.style.setProperty(
"--r",
`${ul.clientWidth - a.offsetLeft - a.offsetWidth}px`,
);
el.style.setProperty("--dl", right ? "0.08s" : "0s");
el.style.setProperty("--dr", right ? "0s" : "0.08s");
last.current = target;
};
place();
const ro = new ResizeObserver(place);
ro.observe(ul);
return () => ro.disconnect();
}, [target]);
// Escape closes the small-screen menu.
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [open]);
return (
<nav className="gu-glide" data-theme={theme} aria-label="Main">
<div className="gu-glide__bar">
<a className="gu-glide__brand" href="#">
<span className="gu-glide__mark" aria-hidden="true" />
{brand}
</a>
<div className="gu-glide__track" onPointerLeave={() => setHover(null)}>
<span ref={pill} className="gu-glide__pill" aria-hidden="true" />
<ul ref={list} className="gu-glide__links">
{links.map((link, i) => (
<li key={link.href}>
<a
href={link.href}
data-on={i === target || undefined}
aria-current={i === active ? "page" : undefined}
onPointerEnter={(e) =>
e.pointerType === "mouse" && setHover(i)
}
onFocus={() => setHover(i)}
onBlur={() => setHover(null)}
onClick={() => setActive(i)}
>
{link.label}
</a>
</li>
))}
</ul>
</div>
{cta && (
<a className="gu-glide__cta" href={cta.href}>
{cta.label}
</a>
)}
<button
type="button"
className="gu-glide__toggle"
aria-expanded={open}
aria-controls="gu-glide-sheet"
onClick={() => setOpen((o) => !o)}
>
<span className="gu-glide__sr">
{open ? "Close menu" : "Open menu"}
</span>
<span
className="gu-glide__burger"
data-open={open || undefined}
aria-hidden="true"
/>
</button>
</div>
{/* Small screens: the same links in a dropdown sheet */}
<ul
id="gu-glide-sheet"
className="gu-glide__sheet"
data-open={open || undefined}
>
{links.map((link, i) => (
<li key={link.href}>
<a
href={link.href}
aria-current={i === active ? "page" : undefined}
tabIndex={open ? 0 : -1}
onClick={() => {
setActive(i);
setOpen(false);
}}
>
{link.label}
</a>
</li>
))}
</ul>
</nav>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Glide nav
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A floating, pill-shaped top navigation bar. A dark highlight pill sits behind the hovered link and travels between links with an elastic stretch: the leading edge moves first and the trailing edge follows 80ms later.
## Layout and look
- Bar: fully rounded, 1px border at 12% text color, background #fbf8f2 (dark: #18181b), soft long shadow, padding 8px 8px 8px 20px, max width 780px.
- Left: brand name, weight 800, with a 14px orange (#ff5a1f) rounded square mark that has an offset dark square shadow.
- Center: links, 15px, weight 600, muted #57534b (dark: #aea89c), padding 11px 16px, pill-shaped.
- Highlight pill: #0e0e10 (dark: #f3eee3); the link on it switches to the inverse color.
- Current page: a 4px orange dot under the link, visible even while another link is hovered.
- Right: an orange call-to-action pill with dark text.
## Behavior and motion
1. Hover or focus a link: the pill moves under it. Animate its left and right edges separately (500ms, cubic-bezier(0.16, 1, 0.3, 1)); when moving right the right edge leads and the left edge waits 80ms, and the reverse when moving left.
2. Leaving the link list returns the pill to the active link. Clicking a link makes it active.
3. Recalculate positions when the bar resizes.
4. When the bar's own width is under 620px (container query), hide links and CTA and show a round menu button (hamburger morphing into an X). It opens a dropdown card with large links that fades and drops in (400ms). Escape closes it.
## Touch
Touch taps make a link active directly (no hover state). The small-width menu has 44px+ targets.
## Accessibility
- A <nav aria-label="Main"> with a real list of links; the active one has aria-current="page".
- Menu button has aria-expanded and aria-controls; hidden sheet links are removed from the tab order.
- Visible focus ring (#2b35ff, or #d7ff3a in dark mode).
- prefers-reduced-motion: no transitions; the pill jumps.
## Props
links ({ label, href }[]), brand (string), cta ({ label, href }), initial (active index), theme ("light" | "dark").
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### GlideNav.tsx
```tsx
"use client";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import "./glide-nav.css";
export type GlideLink = { label: string; href: string };
type GlideNavProps = {
links: GlideLink[];
brand?: string;
cta?: { label: string; href: string };
/** Which link starts active (index). */
initial?: number;
theme?: "light" | "dark";
};
/**
* GlideNav
* - A dark pill sits behind the hovered (or active) link.
* - When it moves, the leading edge goes first and the trailing edge follows a beat later,
* so the pill stretches like elastic instead of sliding stiffly.
* - Below 620px of available width the links fold into a menu button.
*/
export function GlideNav({
links,
brand = "Northwind",
cta,
initial = 0,
theme = "light",
}: GlideNavProps) {
const [active, setActive] = useState(initial);
const [hover, setHover] = useState<number | null>(null);
const [open, setOpen] = useState(false);
const list = useRef<HTMLUListElement>(null);
const pill = useRef<HTMLSpanElement>(null);
const last = useRef(initial);
const target = hover ?? active;
// Place the pill under the target link. Which edge leads depends on the direction.
useLayoutEffect(() => {
const ul = list.current;
const el = pill.current;
if (!ul || !el) return;
const place = () => {
const a = ul.querySelectorAll("a")[target] as HTMLElement | undefined;
if (!a) return;
const right = target > last.current;
el.style.setProperty("--l", `${a.offsetLeft}px`);
el.style.setProperty(
"--r",
`${ul.clientWidth - a.offsetLeft - a.offsetWidth}px`,
);
el.style.setProperty("--dl", right ? "0.08s" : "0s");
el.style.setProperty("--dr", right ? "0s" : "0.08s");
last.current = target;
};
place();
const ro = new ResizeObserver(place);
ro.observe(ul);
return () => ro.disconnect();
}, [target]);
// Escape closes the small-screen menu.
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [open]);
return (
<nav className="gu-glide" data-theme={theme} aria-label="Main">
<div className="gu-glide__bar">
<a className="gu-glide__brand" href="#">
<span className="gu-glide__mark" aria-hidden="true" />
{brand}
</a>
<div className="gu-glide__track" onPointerLeave={() => setHover(null)}>
<span ref={pill} className="gu-glide__pill" aria-hidden="true" />
<ul ref={list} className="gu-glide__links">
{links.map((link, i) => (
<li key={link.href}>
<a
href={link.href}
data-on={i === target || undefined}
aria-current={i === active ? "page" : undefined}
onPointerEnter={(e) =>
e.pointerType === "mouse" && setHover(i)
}
onFocus={() => setHover(i)}
onBlur={() => setHover(null)}
onClick={() => setActive(i)}
>
{link.label}
</a>
</li>
))}
</ul>
</div>
{cta && (
<a className="gu-glide__cta" href={cta.href}>
{cta.label}
</a>
)}
<button
type="button"
className="gu-glide__toggle"
aria-expanded={open}
aria-controls="gu-glide-sheet"
onClick={() => setOpen((o) => !o)}
>
<span className="gu-glide__sr">
{open ? "Close menu" : "Open menu"}
</span>
<span
className="gu-glide__burger"
data-open={open || undefined}
aria-hidden="true"
/>
</button>
</div>
{/* Small screens: the same links in a dropdown sheet */}
<ul
id="gu-glide-sheet"
className="gu-glide__sheet"
data-open={open || undefined}
>
{links.map((link, i) => (
<li key={link.href}>
<a
href={link.href}
aria-current={i === active ? "page" : undefined}
tabIndex={open ? 0 : -1}
onClick={() => {
setActive(i);
setOpen(false);
}}
>
{link.label}
</a>
</li>
))}
</ul>
</nav>
);
}
```
### glide-nav.css
```css
/* GlideNav: change these colors to match your brand. */
.gu-glide {
--bg: #fbf8f2; /* bar background */
--text: #0e0e10; /* brand + text */
--muted: #57534b; /* idle links */
--pill: #0e0e10; /* the moving highlight */
--pill-text: #f3eee3; /* link text on the highlight */
--accent: #ff5a1f; /* call-to-action + active dot */
--accent-text: #0e0e10;
--line: rgb(14 14 16 / 0.12);
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
container-type: inline-size;
position: relative;
width: min(100%, 780px);
font-family: system-ui, sans-serif;
color: var(--text);
}
.gu-glide[data-theme="dark"] {
--bg: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--pill: #f3eee3;
--pill-text: #0e0e10;
--line: rgb(243 238 227 / 0.12);
--focus: #d7ff3a;
}
.gu-glide__bar {
display: flex;
align-items: center;
gap: 12px;
padding: 8px 8px 8px 20px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--bg);
box-shadow: 0 18px 40px -24px rgb(14 14 16 / 0.45);
}
.gu-glide a {
color: inherit;
text-decoration: none;
}
.gu-glide a:focus-visible,
.gu-glide button:focus-visible {
outline: 2px solid var(--focus);
outline-offset: 2px;
}
.gu-glide__brand {
display: inline-flex;
align-items: center;
gap: 10px;
margin-right: auto;
border-radius: 8px;
font-weight: 800;
letter-spacing: -0.02em;
}
.gu-glide__mark {
width: 14px;
height: 14px;
border-radius: 4px;
background: var(--accent);
box-shadow: 6px 6px 0 -2px var(--text);
}
/* Links + the elastic pill */
.gu-glide__track {
position: relative;
}
.gu-glide__links {
display: flex;
gap: 2px;
margin: 0;
padding: 0;
list-style: none;
}
.gu-glide__links a {
position: relative;
z-index: 1;
display: block;
padding: 11px 16px;
border-radius: 999px;
color: var(--muted);
font-size: 0.95rem;
font-weight: 600;
transition: color 0.3s;
}
.gu-glide__links a[data-on] {
color: var(--pill-text);
}
/* A small dot marks the current page, even while another link is hovered. */
.gu-glide__links a[aria-current="page"]::after {
content: "";
position: absolute;
left: 50%;
bottom: 4px;
width: 4px;
height: 4px;
margin-left: -2px;
border-radius: 50%;
background: var(--accent);
}
.gu-glide__pill {
position: absolute;
top: 0;
bottom: 0;
left: var(--l, 0);
right: var(--r, 100%);
border-radius: 999px;
background: var(--pill);
transition:
left 0.5s var(--ease) var(--dl, 0s),
right 0.5s var(--ease) var(--dr, 0s);
}
.gu-glide__cta {
padding: 11px 18px;
border-radius: 999px;
background: var(--accent);
color: var(--accent-text) !important;
font-size: 0.95rem;
font-weight: 700;
white-space: nowrap;
transition: transform 0.3s var(--ease);
}
.gu-glide__cta:hover {
transform: translateY(-1px);
}
/* Menu button + sheet (only shown on narrow containers) */
.gu-glide__toggle {
display: none;
place-items: center;
width: 44px;
height: 44px;
border: 0;
border-radius: 50%;
background: var(--pill);
cursor: pointer;
}
.gu-glide__burger,
.gu-glide__burger::before {
display: block;
width: 18px;
height: 2px;
background: var(--pill-text);
transition: transform 0.35s var(--ease);
}
.gu-glide__burger {
position: relative;
transform: translateY(-3px);
}
.gu-glide__burger::before {
content: "";
position: absolute;
top: 6px;
}
.gu-glide__burger[data-open] {
transform: translateY(0) rotate(45deg);
}
.gu-glide__burger[data-open]::before {
transform: translateY(-6px) rotate(-90deg);
}
.gu-glide__sheet {
position: absolute;
inset: calc(100% + 8px) 0 auto;
z-index: 5;
display: grid;
gap: 2px;
margin: 0;
padding: 8px;
list-style: none;
border: 1px solid var(--line);
border-radius: 24px;
background: var(--bg);
box-shadow: 0 24px 48px -24px rgb(14 14 16 / 0.5);
opacity: 0;
visibility: hidden;
transform: translateY(-8px) scale(0.98);
transform-origin: top center;
transition:
opacity 0.25s,
transform 0.4s var(--ease),
visibility 0s 0.4s;
}
.gu-glide__sheet[data-open] {
opacity: 1;
visibility: visible;
transform: none;
transition:
opacity 0.25s,
transform 0.4s var(--ease);
}
.gu-glide__sheet a {
display: block;
padding: 14px 16px;
border-radius: 16px;
font-size: 1.1rem;
font-weight: 700;
}
.gu-glide__sheet a[aria-current="page"] {
background: var(--pill);
color: var(--pill-text);
}
.gu-glide__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
@container (max-width: 620px) {
.gu-glide__track,
.gu-glide__cta {
display: none;
}
.gu-glide__toggle {
display: grid;
}
}
@container (min-width: 621px) {
.gu-glide__sheet {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-glide *,
.gu-glide__pill {
transition: none !important;
}
}
```
You might also want
Next piece: Grid with light beamsCollapsible sidebar
Components, intermediate
Mega menu
Components, advanced
3D tilt card
Components, intermediate