Tooltip
A short line that describes a control, under the pointer or the keyboard focus.
Installation
pnpm dlx @human-kit/design add tooltipnpx @human-kit/design add tooltipyarn dlx @human-kit/design add tooltipbunx @human-kit/design add tooltipThe command copies the files to src/lib/components/ui/tooltip. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Button from '$lib/components/ui/button';
import * as Tooltip from '$lib/components/ui/tooltip';
import Printer from '@lucide/svelte/icons/printer';
let print = $state<HTMLButtonElement | null>(null);
</script>
<Button.Root variant="outline" size="icon" aria-label="Print" bind:element={print}><Printer /></Button.Root>
<Tooltip.Root anchor={print} text="Print the prescription" /> Examples
A toolbar with shortcuts
shortcut writes the keys that do the same after the text, quieter. Inside a Tooltip.Group, once the first tooltip has opened, the same label travels to the next button as the pointer or the focus moves along the toolbar, and takes its words.
Guidelines
- Use a tooltip to say what an icon button does, or to add the keys that do it. Keep it to a few words.
- Never put in a tooltip what the reader needs to finish the task: a touch screen shows it only on a long press. Put it on the page.
- Nothing in a tooltip can be pressed. For a link or a button in a floating box, use a
Popover.Root. - Describe the control, do not repeat its name: "Print the prescription" for a button named "Print".
- Wrap a toolbar in one
Tooltip.Group: moving from button to button does not wait each time, and one label travels along the row instead of blinking out and in. - Use a
Tooltip.Providerinstead for tooltips that only share their timing, far apart on the page.
Accessibility
- The text is a
role="tooltip", and the anchor getsaria-describedbywhile it shows: a screen reader reads it after the name. - It is a description, not a name. A button with an icon alone still needs its
aria-label. - A focus from the keyboard opens it at once; the pointer opens it after the delay.
Escapecloses it. - The pointer can cross to the tooltip and rest on it without closing it. A long press opens it on touch.
- Anchor it to an element that takes the focus, or the keyboard never shows it. A disabled button does not: use
aria-disabledon it instead. - In a
Tooltip.Groupthe label is one element: the control it reaches points at it witharia-describedby, and its words change before that, thus each control is read with its own text. - With reduced motion the label of a group does not travel: it changes place and words at once.
API reference
Tooltip.Root
| Prop | Type | Default |
|---|---|---|
anchor | HTMLElement | null The element it describes. Get it with | — |
text | string What the tooltip says. Required. | — |
shortcut | string The keys that do the same, after the text: "Ctrl B". | — |
placement | 'top' | 'bottom' | 'left' | 'right' | 'top-start' | ... The side of the anchor it shows on. It flips when there is no room. | 'top' |
offset | number The gap to the anchor, in pixels. | 6 |
delay | number Milliseconds from the pointer resting on the anchor to the open. A provider sets it for the tooltips inside. | 600 |
disabled | boolean Stops the tooltip from opening. | false |
class | string Classes for the tooltip box. | — |
It wraps nothing: it listens to the element in anchor, which keeps its own component.
Tooltip.Group
| Prop | Type | Default |
|---|---|---|
delay | number Milliseconds from the pointer resting on the first control to the open. | 600 |
closeDelay | number Milliseconds from the pointer leaving to the close. | 100 |
skipDelay | number Milliseconds after a close in which the next control opens at once, the label travelling to it from where it was. | 300 |
One label for a row of controls: it travels from control to control. The Tooltip.Roots inside draw nothing of their own, they say what the label shows for each anchor.
Tooltip.Provider
| Prop | Type | Default |
|---|---|---|
delay | number The open delay of every tooltip inside. | 600 |
closeDelay | number Milliseconds from the pointer leaving to the close. | 100 |
skipDelay | number Milliseconds after a close in which the next tooltip opens at once. | 300 |
It renders nothing of its own: it only shares the timing with the tooltips inside.