Smart tooltips
A toolbar whose tooltips flip to stay on screen, wait a moment before showing, then appear instantly while you glide between buttons, with keyboard shortcuts inside.
"use client";
import {
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
type ReactElement,
} from "react";
import "./tooltip-toolbar.css";
type Tool = { id: string; label: string; keys: string[]; icon: ReactElement };
const I = (d: string) => (
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d={d} />
</svg>
);
const TOOLS: Tool[] = [
{
id: "bold",
label: "Bold",
keys: ["Ctrl", "B"],
icon: I("M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z"),
},
{
id: "italic",
label: "Italic",
keys: ["Ctrl", "I"],
icon: I("M10 5h8M6 19h8M14 5l-4 14"),
},
{
id: "link",
label: "Add link",
keys: ["Ctrl", "K"],
icon: I(
"M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1",
),
},
{
id: "list",
label: "Bulleted list",
keys: ["Ctrl", "Shift", "8"],
icon: I("M9 6h11M9 12h11M9 18h11M4.5 6h.01M4.5 12h.01M4.5 18h.01"),
},
{
id: "image",
label: "Insert image",
keys: ["/", "image"],
icon: I("M4 5h16v14H4zM4 15l4-4 5 5 3-3 4 4M15 9.5h.01"),
},
{
id: "undo",
label: "Undo",
keys: ["Ctrl", "Z"],
icon: I("M9 14 4 9l5-5M4 9h10a6 6 0 0 1 0 12h-3"),
},
];
/**
* TooltipToolbar
* - First tooltip waits 500ms (no flicker while the mouse passes by). After one has shown, moving to a
* neighbour shows it instantly ("warm" mode), until the pointer has been away for 300ms.
* - Position: above the button by default, flips below when there is no room, and shifts sideways to stay inside the window.
* - The tooltip is linked with aria-describedby and closes on Escape.
*/
export function TooltipToolbar({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const uid = useId();
const [shown, setShown] = useState<string | null>(null);
const [pressed, setPressed] = useState<Record<string, boolean>>({
bold: true,
});
const [place, setPlace] = useState<{
x: number;
y: number;
below: boolean;
shift: number;
}>({ x: 0, y: 0, below: false, shift: 0 });
const warm = useRef(false);
const timers = useRef<{ open?: number; cool?: number; press?: number }>({});
const btns = useRef<Record<string, HTMLButtonElement | null>>({});
const tip = useRef<HTMLDivElement>(null);
const bar = useRef<HTMLDivElement>(null);
const open = (id: string, now = false) => {
clearTimeout(timers.current.open);
clearTimeout(timers.current.cool);
if (now || warm.current) {
warm.current = true;
setShown(id);
} else
timers.current.open = window.setTimeout(() => {
warm.current = true;
setShown(id);
}, 500);
};
const close = () => {
clearTimeout(timers.current.open);
setShown(null);
timers.current.cool = window.setTimeout(() => (warm.current = false), 300);
};
// Measure and place after the tooltip renders, before paint.
useLayoutEffect(() => {
if (!shown || !tip.current || !bar.current) return;
const b = btns.current[shown]!.getBoundingClientRect();
const host = bar.current.getBoundingClientRect();
const t = tip.current.getBoundingClientRect();
const below = b.top - t.height - 12 < 0;
const center = b.left + b.width / 2;
const shift = Math.max(
8 - (center - t.width / 2),
Math.min(0, innerWidth - 8 - (center + t.width / 2)),
);
setPlace({
x: center - host.left,
y: below ? b.bottom - host.top + 10 : b.top - host.top - 10,
below,
shift,
});
}, [shown]);
useEffect(() => {
const t = timers.current;
const esc = (e: KeyboardEvent) => e.key === "Escape" && setShown(null);
addEventListener("keydown", esc);
return () => {
removeEventListener("keydown", esc);
clearTimeout(t.open);
clearTimeout(t.cool);
clearTimeout(t.press);
};
}, []);
const current = TOOLS.find((t) => t.id === shown);
return (
<div className="gu-tips" data-theme={theme}>
<div
ref={bar}
className="gu-tips__bar"
role="toolbar"
aria-label="Text formatting"
onPointerLeave={close}
>
{TOOLS.map((t, i) => (
<span key={t.id} className="gu-tips__slot">
{i === 3 && <span className="gu-tips__sep" aria-hidden="true" />}
<button
ref={(el) => {
btns.current[t.id] = el;
}}
type="button"
aria-label={t.label}
aria-pressed={
["bold", "italic", "list"].includes(t.id)
? !!pressed[t.id]
: undefined
}
aria-describedby={shown === t.id ? `${uid}-tip` : undefined}
onPointerEnter={(e) => e.pointerType === "mouse" && open(t.id)}
onPointerDown={(e) => {
if (e.pointerType !== "mouse")
timers.current.press = window.setTimeout(
() => open(t.id, true),
450,
);
}}
onPointerUp={() => clearTimeout(timers.current.press)}
onFocus={(e) =>
e.currentTarget.matches(":focus-visible") && open(t.id, true)
}
onBlur={close}
onContextMenu={(e) => e.preventDefault()}
onClick={() => setPressed((p) => ({ ...p, [t.id]: !p[t.id] }))}
>
{t.icon}
</button>
</span>
))}
{current && (
<div
ref={tip}
id={`${uid}-tip`}
role="tooltip"
className="gu-tips__tip"
data-below={place.below ? "" : undefined}
style={{ left: place.x, top: place.y, marginLeft: place.shift }}
key={current.id}
>
{current.label}
<span className="gu-tips__keys">
{current.keys.map((k) => (
<kbd key={k}>{k}</kbd>
))}
</span>
</div>
)}
</div>
<p className="gu-tips__hint">
Hover slowly, then glide along the bar. Tab with the keyboard, or
long-press on touch.
</p>
</div>
);
}
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: Smart tooltips
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
An editor formatting toolbar with polished tooltips: a 500ms delay before the first tooltip, instant tooltips while gliding to neighbours, flipping below when there is no room above, staying inside the window, and keyboard shortcut chips inside.
## Layout and look
- Toolbar: card background, radius 16px, 6px padding, soft shadow; 44px icon buttons (radius 11px), a thin divider after the third. Toggle buttons that are on (Bold) are Ink with Cream icons.
- Tooltip: Ink pill (Cream in dark mode), radius 10px, 14px bold label, then shortcut keys as small monospace chips; a rotated-square arrow pointing at the button.
## Behavior and motion
1. Hover (mouse) waits 500ms, then enters "warm" mode: other buttons show instantly. Leaving the toolbar for 300ms cools down.
2. Keyboard focus (focus-visible) shows the tooltip immediately; Escape hides it.
3. Touch: long press 450ms shows it; a tap just presses.
4. useLayoutEffect measures button + tooltip: above by default, below if it would leave the top; shift sideways to keep 8px from the window edges.
5. Tooltip enters with a 180ms fade, 4px rise and scale from 0.92.
## Accessibility
- role="toolbar"; buttons have aria-label and aria-pressed for toggles; the visible tooltip is role="tooltip" linked with aria-describedby.
- prefers-reduced-motion: no tooltip animation.
## Props
theme. Edit TOOLS.
## 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.
### TooltipToolbar.tsx
```tsx
"use client";
import {
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
type ReactElement,
} from "react";
import "./tooltip-toolbar.css";
type Tool = { id: string; label: string; keys: string[]; icon: ReactElement };
const I = (d: string) => (
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d={d} />
</svg>
);
const TOOLS: Tool[] = [
{
id: "bold",
label: "Bold",
keys: ["Ctrl", "B"],
icon: I("M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z"),
},
{
id: "italic",
label: "Italic",
keys: ["Ctrl", "I"],
icon: I("M10 5h8M6 19h8M14 5l-4 14"),
},
{
id: "link",
label: "Add link",
keys: ["Ctrl", "K"],
icon: I(
"M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1",
),
},
{
id: "list",
label: "Bulleted list",
keys: ["Ctrl", "Shift", "8"],
icon: I("M9 6h11M9 12h11M9 18h11M4.5 6h.01M4.5 12h.01M4.5 18h.01"),
},
{
id: "image",
label: "Insert image",
keys: ["/", "image"],
icon: I("M4 5h16v14H4zM4 15l4-4 5 5 3-3 4 4M15 9.5h.01"),
},
{
id: "undo",
label: "Undo",
keys: ["Ctrl", "Z"],
icon: I("M9 14 4 9l5-5M4 9h10a6 6 0 0 1 0 12h-3"),
},
];
/**
* TooltipToolbar
* - First tooltip waits 500ms (no flicker while the mouse passes by). After one has shown, moving to a
* neighbour shows it instantly ("warm" mode), until the pointer has been away for 300ms.
* - Position: above the button by default, flips below when there is no room, and shifts sideways to stay inside the window.
* - The tooltip is linked with aria-describedby and closes on Escape.
*/
export function TooltipToolbar({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const uid = useId();
const [shown, setShown] = useState<string | null>(null);
const [pressed, setPressed] = useState<Record<string, boolean>>({
bold: true,
});
const [place, setPlace] = useState<{
x: number;
y: number;
below: boolean;
shift: number;
}>({ x: 0, y: 0, below: false, shift: 0 });
const warm = useRef(false);
const timers = useRef<{ open?: number; cool?: number; press?: number }>({});
const btns = useRef<Record<string, HTMLButtonElement | null>>({});
const tip = useRef<HTMLDivElement>(null);
const bar = useRef<HTMLDivElement>(null);
const open = (id: string, now = false) => {
clearTimeout(timers.current.open);
clearTimeout(timers.current.cool);
if (now || warm.current) {
warm.current = true;
setShown(id);
} else
timers.current.open = window.setTimeout(() => {
warm.current = true;
setShown(id);
}, 500);
};
const close = () => {
clearTimeout(timers.current.open);
setShown(null);
timers.current.cool = window.setTimeout(() => (warm.current = false), 300);
};
// Measure and place after the tooltip renders, before paint.
useLayoutEffect(() => {
if (!shown || !tip.current || !bar.current) return;
const b = btns.current[shown]!.getBoundingClientRect();
const host = bar.current.getBoundingClientRect();
const t = tip.current.getBoundingClientRect();
const below = b.top - t.height - 12 < 0;
const center = b.left + b.width / 2;
const shift = Math.max(
8 - (center - t.width / 2),
Math.min(0, innerWidth - 8 - (center + t.width / 2)),
);
setPlace({
x: center - host.left,
y: below ? b.bottom - host.top + 10 : b.top - host.top - 10,
below,
shift,
});
}, [shown]);
useEffect(() => {
const t = timers.current;
const esc = (e: KeyboardEvent) => e.key === "Escape" && setShown(null);
addEventListener("keydown", esc);
return () => {
removeEventListener("keydown", esc);
clearTimeout(t.open);
clearTimeout(t.cool);
clearTimeout(t.press);
};
}, []);
const current = TOOLS.find((t) => t.id === shown);
return (
<div className="gu-tips" data-theme={theme}>
<div
ref={bar}
className="gu-tips__bar"
role="toolbar"
aria-label="Text formatting"
onPointerLeave={close}
>
{TOOLS.map((t, i) => (
<span key={t.id} className="gu-tips__slot">
{i === 3 && <span className="gu-tips__sep" aria-hidden="true" />}
<button
ref={(el) => {
btns.current[t.id] = el;
}}
type="button"
aria-label={t.label}
aria-pressed={
["bold", "italic", "list"].includes(t.id)
? !!pressed[t.id]
: undefined
}
aria-describedby={shown === t.id ? `${uid}-tip` : undefined}
onPointerEnter={(e) => e.pointerType === "mouse" && open(t.id)}
onPointerDown={(e) => {
if (e.pointerType !== "mouse")
timers.current.press = window.setTimeout(
() => open(t.id, true),
450,
);
}}
onPointerUp={() => clearTimeout(timers.current.press)}
onFocus={(e) =>
e.currentTarget.matches(":focus-visible") && open(t.id, true)
}
onBlur={close}
onContextMenu={(e) => e.preventDefault()}
onClick={() => setPressed((p) => ({ ...p, [t.id]: !p[t.id] }))}
>
{t.icon}
</button>
</span>
))}
{current && (
<div
ref={tip}
id={`${uid}-tip`}
role="tooltip"
className="gu-tips__tip"
data-below={place.below ? "" : undefined}
style={{ left: place.x, top: place.y, marginLeft: place.shift }}
key={current.id}
>
{current.label}
<span className="gu-tips__keys">
{current.keys.map((k) => (
<kbd key={k}>{k}</kbd>
))}
</span>
</div>
)}
</div>
<p className="gu-tips__hint">
Hover slowly, then glide along the bar. Tab with the keyboard, or
long-press on touch.
</p>
</div>
);
}
```
### tooltip-toolbar.css
```css
/* TooltipToolbar: change these colors to match your brand. */
.gu-tips {
--bar: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.12);
--hover: rgb(14 14 16 / 0.07);
--on: #0e0e10;
--on-text: #f3eee3;
--tip: #0e0e10;
--tip-text: #f3eee3;
--focus: #2b35ff;
display: grid;
justify-items: center;
gap: 18px;
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-tips[data-theme="dark"] {
--bar: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.12);
--hover: rgb(243 238 227 / 0.08);
--on: #f3eee3;
--on-text: #0e0e10;
--tip: #f3eee3;
--tip-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-tips__bar {
position: relative;
display: flex;
align-items: center;
gap: 2px;
padding: 6px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--bar);
box-shadow: 0 14px 30px -18px rgb(0 0 0 / 0.35);
}
.gu-tips__slot {
display: flex;
align-items: center;
gap: 2px;
}
.gu-tips__sep {
width: 1px;
height: 24px;
margin: 0 6px;
background: var(--line);
}
.gu-tips__bar button {
display: grid;
place-items: center;
width: 44px;
height: 44px;
border: 0;
border-radius: 11px;
background: none;
color: inherit;
cursor: pointer;
-webkit-touch-callout: none;
transition:
background-color 0.15s,
color 0.15s;
}
.gu-tips__bar button:hover {
background: var(--hover);
}
.gu-tips__bar button[aria-pressed="true"] {
background: var(--on);
color: var(--on-text);
}
.gu-tips__bar button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 1px;
}
.gu-tips__bar svg {
width: 20px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.gu-tips__tip {
position: absolute;
z-index: 5;
display: flex;
align-items: center;
gap: 10px;
padding: 7px 8px 7px 12px;
border-radius: 10px;
background: var(--tip);
color: var(--tip-text);
font-size: 0.85rem;
font-weight: 700;
white-space: nowrap;
pointer-events: none;
translate: -50% -100%;
transform-origin: bottom center;
animation: gu-tips-in 0.18s cubic-bezier(0.16, 1, 0.3, 1);
}
.gu-tips__tip[data-below] {
translate: -50% 0;
transform-origin: top center;
}
/* Arrow */
.gu-tips__tip::after {
content: "";
position: absolute;
left: 50%;
bottom: -4px;
width: 9px;
height: 9px;
margin-left: -4.5px;
background: inherit;
border-radius: 2px;
rotate: 45deg;
}
.gu-tips__tip[data-below]::after {
top: -4px;
bottom: auto;
}
@keyframes gu-tips-in {
from {
opacity: 0;
transform: translateY(4px) scale(0.92);
}
}
.gu-tips__keys {
display: flex;
gap: 3px;
}
.gu-tips kbd {
min-width: 22px;
padding: 2px 6px;
border-radius: 6px;
background: color-mix(in srgb, var(--tip-text) 16%, transparent);
font:
700 0.72rem ui-monospace,
monospace;
text-align: center;
}
.gu-tips__hint {
max-width: 34ch;
margin: 0;
color: var(--muted);
font-size: 0.85rem;
text-align: center;
}
@media (prefers-reduced-motion: reduce) {
.gu-tips__tip {
animation: none;
}
}
```
You might also want
Next piece: Snap carousel with progressAvatar stack
Components, beginner
Hold to confirm
Components, beginner
Magnetic button
Components, beginner