Tooltip25
A short hint that appears on hover and keyboard focus. Sticker slaps it on like a tilted label, Comic writes it in a yellow caption box, Arcade shows it on a neon screen.
<TooltipTrigger>
<Button aria-label="Copy link">…</Button>
<Tooltip>Copy link</Tooltip>
</TooltipTrigger>Usage
import { Button, Tooltip, TooltipTrigger } from "plonk-ui";
export function CopyButton() {
return (
<TooltipTrigger>
<Button aria-label="Copy link">…</Button>
<Tooltip>Copy link</Tooltip>
</TooltipTrigger>
);
}TooltipTrigger takes the trigger and the tooltip. It opens after 500ms of
hovering, or right away on keyboard focus. Once one tooltip is open, moving to
the next one doesn't wait again.
Icon buttons
The classic use: an icon button names itself with aria-label, and the tooltip
shows the same name to sighted users.
import { Button, Tooltip, TooltipTrigger } from "plonk-ui";
const ICONS = {
Bold: "M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z",
Italic: "M10 5h8M6 19h8M14 5l-4 14",
Link: "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",
};
export default function Icons() {
return (
<div className="flex gap-2">
{Object.entries(ICONS).map(([label, d]) => (
<TooltipTrigger key={label}>
<Button variant="neutral" size="sm" aria-label={label}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2.5}
strokeLinecap="round"
strokeLinejoin="round"
className="size-4"
>
<path d={d} />
</svg>
</Button>
<Tooltip>{label}</Tooltip>
</TooltipTrigger>
))}
</div>
);
}Shortcuts
A tooltip can hold a Kbd to teach a keyboard shortcut.
import { Button, Kbd, Tooltip, TooltipTrigger } from "plonk-ui";
export default function Shortcut() {
return (
<TooltipTrigger>
<Button variant="ghost">Search</Button>
<Tooltip placement="bottom">
<span className="flex items-center gap-2">
Open search <Kbd>⌘</Kbd>
<Kbd>K</Kbd>
</span>
</Tooltip>
</TooltipTrigger>
);
}When not to use it
A tooltip disappears as soon as the pointer leaves, and touch screens have no hover. Keep it for hints people can do without, and never put buttons or links in it. For something to click or read at length, use a Popover.
Personalities
Sticker sticks the tooltip on at an angle with a little bounce, Retro shows it instantly in mono type, Comic prints it in a yellow caption box and Arcade shows it on a dark screen with a neon frame.
Accessibility
- It opens on keyboard focus as well as hover, and
Esccloses it. - The trigger is described by the tooltip (
aria-describedby), so screen readers read it after the trigger's name. - Don't put the only label of a control in a tooltip: give the trigger its own name too.
API
Tooltip
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the bubble. |
placement | "left" | "right" | "bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right top" | "right bottom" | "end" | "end top" | "end bottom" | "top" | Preferred side. It flips when there's no room. |
offset | number | 10 | Gap to the trigger, in pixels. |
crossOffset | number | 0 | Shift along the trigger's edge, in pixels. |
shouldFlip | boolean | true | Flip to the other side when it doesn't fit. |
TooltipTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
delay | number | 500 | Milliseconds of hovering before it opens. Once one tooltip is open, the next opens right away. |
closeDelay | number | 200 | Milliseconds before it closes after the pointer leaves. |
trigger | "focus" | "hover" | hover and focus | By default it opens on hover and on focus. focus opens it on focus only. |
isDisabled | boolean | — | Never shows the tooltip. |
isOpen | boolean | — | Controlled open state. Use with onOpenChange. |
defaultOpen | boolean | — | Open on first render, when uncontrolled. |
onOpenChange | (isOpen: boolean) => void | — | Called with true or false when it opens or closes. |