Try 'magnetic button'
View code

Number 53: Hover pill navigation

Specification

Catalogue
No. 53 of 110
Section
Navigation
Interaction
Hover
Element
Navigation
Preview
Live component
Imports
react 19.3.1
Added

Notes

A single pill follows the pointer across the links and returns to the current page when the pointer leaves the list. Because there is only one highlight, moving quickly between links reads as one continuous motion instead of several flickers.

The pill is positioned from each link's offsetLeft and offsetWidth. Focus moves it too, so keyboard users see the same thing.

The current page also carries a small dot, so it stays identifiable while the pill is elsewhere.

Code

React and plain CSS
"use client";

import { useEffect, useLayoutEffect, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./nav-pill.module.css";

const LINKS = ["Work", "Studio", "Journal", "Contact"];
const DEMO_HOVER = [1, 2, 3, 2, -1];

export default function NavPill({ active, context }: LivePreviewProps) {
  const [current, setCurrent] = useState(0);
  const [hovered, setHovered] = useState(-1);
  const [step, setStep] = useState(0);
  const listRef = useRef<HTMLUListElement>(null);
  const pillRef = useRef<HTMLSpanElement>(null);
  const demo = active && context === "card";

  // Each time the demo starts, it starts from the first step.
  const [demoRunning, setDemoRunning] = useState(demo);
  if (demoRunning !== demo) {
    setDemoRunning(demo);
    setStep(0);
  }

  useEffect(() => {
    if (!demo) return;
    const id = window.setInterval(() => setStep((s) => s + 1), 650);
    return () => window.clearInterval(id);
  }, [demo]);

  const hover = context === "card" ? (demo ? (DEMO_HOVER[step % DEMO_HOVER.length] ?? -1) : -1) : hovered;
  const target = hover >= 0 ? hover : context === "card" ? 0 : current;

  // Slide the pill under the hovered link, or back to the current page.
  useLayoutEffect(() => {
    const link = listRef.current?.querySelectorAll("a")[target];
    const pill = pillRef.current;
    if (!link || !pill) return;
    pill.style.transform = `translateX(${link.offsetLeft}px)`;
    pill.style.width = `${link.offsetWidth}px`;
  }, [target]);

  return (
    <nav className={styles.bar} aria-label="Studio site">
      <span className={styles.mark} aria-hidden="true" />
      <ul ref={listRef} className={styles.list} onPointerLeave={() => setHovered(-1)}>
        <span ref={pillRef} className={styles.pill} data-hovering={hover >= 0 || undefined} aria-hidden="true" />
        {LINKS.map((label, i) => (
          <li key={label}>
            <a
              href={`#${label.toLowerCase()}`}
              className={styles.link}
              aria-current={i === (context === "card" ? 0 : current) ? "page" : undefined}
              tabIndex={context === "card" ? -1 : 0}
              onPointerEnter={() => setHovered(i)}
              onFocus={() => setHovered(i)}
              onBlur={() => setHovered(-1)}
              onClick={(e) => {
                e.preventDefault();
                setCurrent(i);
              }}
            >
              {label}
            </a>
          </li>
        ))}
      </ul>
    </nav>
  );
}

More like this

Gliding menu

Hover, Layout AnimationNavigation

Sphere of dots that parts for the pointer

Hover, PressPage sections

Magnetic button

Hover, PressButtons and toggles

Slope chart for before and after

Hover, PressCharts and numbers

Icons that act out their meaning

HoverButtons and toggles

Image panels that widen

Hover, Layout AnimationMedia and galleries