Skip to the content
human-kit design
36 — Overlays

Tooltip

A short line that describes a control, under the pointer or the keyboard focus.

Installation

pnpm dlx @human-kit/design add tooltip
npx @human-kit/design add tooltip
yarn dlx @human-kit/design add tooltip
bunx @human-kit/design add tooltip

The 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.Provider instead for tooltips that only share their timing, far apart on the page.

Accessibility

  • The text is a role="tooltip", and the anchor gets aria-describedby while 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. Escape closes 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-disabled on it instead.
  • In a Tooltip.Group the label is one element: the control it reaches points at it with aria-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

PropTypeDefault
anchorHTMLElement | null

The element it describes. Get it with bind:element. Required.

—
textstring

What the tooltip says. Required.

—
shortcutstring

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'
offsetnumber

The gap to the anchor, in pixels.

6
delaynumber

Milliseconds from the pointer resting on the anchor to the open. A provider sets it for the tooltips inside.

600
disabledboolean

Stops the tooltip from opening.

false
classstring

Classes for the tooltip box.

—

It wraps nothing: it listens to the element in anchor, which keeps its own component.

Tooltip.Group

PropTypeDefault
delaynumber

Milliseconds from the pointer resting on the first control to the open.

600
closeDelaynumber

Milliseconds from the pointer leaving to the close.

100
skipDelaynumber

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

PropTypeDefault
delaynumber

The open delay of every tooltip inside.

600
closeDelaynumber

Milliseconds from the pointer leaving to the close.

100
skipDelaynumber

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.