Try 'magnetic button'
View code

Specification

Catalogue
No. 48 of 110
Interaction
Hover
Element
Overlay, Icon
Preview
Live component
Imports
react 19.3.1
Added

Notes

Moving along a row of icons, a single tooltip slides between them instead of blinking on and off.

The first tooltip waits 300ms so a passing pointer does not set it off; after that it follows the hand, changing width and place together. Focus shows it at once.

First show after 300ms; then glides 200ms (position and width together) while moving along. Hides 100ms after leaving; stays warm for 300ms more. Focus and tap show it at once; Escape hides it; aria-describedby on the active icon.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./glide-tooltip.css";

const FACILITIES = [
  { id: "step", label: "Step-free entrance", d: "M4 20h4v-4h4v-4h4V8h4" },
  { id: "park", label: "Two parking spaces at the back", d: "M7 20V4h6a4 4 0 0 1 0 8H7" },
  { id: "eve", label: "Evening appointments, Tuesdays to 8pm", d: "M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z" },
  { id: "card", label: "Card and contactless only, no cash", d: "M3 6h18v12H3zM3 10h18M7 15h3" },
  { id: "loop", label: "Hearing loop at reception", d: "M6 18a2 2 0 0 0 4 0c0-3 4-4 4-8a5 5 0 0 0-10 0M12 10a1.5 1.5 0 0 0-3 0" },
];

function GlideTooltipDemo() {
  const [active, setActive] = useState<number | null>(null);
  const [box, setBox] = useState({ left: 0, width: 0 });
  const [placed, setPlaced] = useState(false);
  const row = useRef<HTMLDivElement>(null);
  const btns = useRef<(HTMLButtonElement | null)[]>([]);
  const measure = useRef<HTMLSpanElement>(null);
  const timers = useRef<{ show?: number; hide?: number; warm?: number }>({});
  const warm = useRef(false);
  const uid = useId();

  useEffect(() => {
    const t = timers.current;
    return () => {
      window.clearTimeout(t.show);
      window.clearTimeout(t.hide);
      window.clearTimeout(t.warm);
    };
  }, []);

  // Position and width move together, so the box never snaps while it glides.
  useLayoutEffect(() => {
    if (active === null) return;
    const b = btns.current[active];
    const r = row.current;
    const m = measure.current;
    if (!b || !r || !m) return;
    const width = Math.min(m.offsetWidth + 2, r.offsetWidth);
    const centre = b.offsetLeft + b.offsetWidth / 2;
    setBox({ left: Math.min(r.offsetWidth - width, Math.max(0, centre - width / 2)), width });
  }, [active]);

  const enter = (i: number, now = false) => {
    window.clearTimeout(timers.current.hide);
    window.clearTimeout(timers.current.show);
    if (now || warm.current) {
      setActive(i);
      setPlaced((p) => p || active !== null);
      warm.current = true;
    } else
      timers.current.show = window.setTimeout(() => {
        setActive(i);
        warm.current = true;
      }, 300);
  };
  const leave = () => {
    window.clearTimeout(timers.current.show);
    timers.current.hide = window.setTimeout(() => {
      setActive(null);
      setPlaced(false);
      timers.current.warm = window.setTimeout(() => (warm.current = false), 300);
    }, 100);
  };

  return (
    <div className="dm">
      <div className="gt" ref={row} onKeyDown={(e) => e.key === "Escape" && setActive(null)}>
        <p className="gt-title">At the practice</p>
        <div className="gt-row">
          {FACILITIES.map((f, i) => (
            <button
              key={f.id}
              ref={(el) => {
                btns.current[i] = el;
              }}
              type="button"
              className="gt-btn"
              aria-label={f.label}
              aria-describedby={active === i ? `${uid}-tip` : undefined}
              onPointerEnter={(e) => e.pointerType === "mouse" && enter(i)}
              onPointerLeave={(e) => e.pointerType === "mouse" && leave()}
              onFocus={() => enter(i, true)}
              onBlur={leave}
              onClick={() => enter(i, true)}
            >
              <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
                <path d={f.d} />
              </svg>
            </button>
          ))}
        </div>
        <span ref={measure} className="gt-tip gt-measure" aria-hidden="true">
          {active !== null ? FACILITIES[active]?.label : ""}
        </span>
        <span
          id={`${uid}-tip`}
          role="tooltip"
          className="gt-tip"
          data-on={active !== null || undefined}
          data-glide={placed || undefined}
          style={{ translate: `${box.left}px 0`, width: box.width || undefined }}
        >
          {active !== null ? FACILITIES[active]?.label : ""}
        </span>
      </div>
      <p className="dm-hint">Move along the row: one tooltip follows instead of five blinking</p>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
  { click: ".gt-btn", nth: 0, wait: 900 },
  { click: ".gt-btn", nth: 2, wait: 900 },
  { click: ".gt-btn", nth: 4, wait: 900 },
];

export default function GlideTooltipEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="One tooltip that glides" sample tour={TOUR}>
      <GlideTooltipDemo />
    </PieceStage>
  );
}

More like this

Cookie choice that folds away

FeedbackOverlays and toasts

Select with a gliding highlight

HoverForms and inputs

Dialogs and sheets

Layout AnimationOverlays and toasts

Header that steps aside

ScrollNavigation

Button that shows its own progress

Press, LoadingButtons and toggles

FAQ that opens smoothly

Layout AnimationCards and accordions