Tooltip
A short text that describes an element, such as what an icon button does or its shortcut, shown when the pointer rests on the element or when it receives keyboard focus: the WAI-ARIA tooltip pattern. The Popup is a popover="manual" element, so the browser puts it in the top layer, above every z-index, with no portal, and no Popover or menu closes it. It opens after a delay on hover, at once on keyboard focus, and not on a press or a touch; it closes on Escape, on blur, when the pointer leaves the Trigger and the Popup, and when the Trigger is pressed. It stays open while the Trigger is hovered or focused, and the pointer can move onto it, as WCAG 1.4.13 asks. Menhir places it against the Trigger with Floating UI, above it by default, and keeps it shown until its exit animation has ended. A tooltip is not a name: the Trigger always names the Popup in aria-describedby, so its text is read after the Trigger's name, open or closed, and an icon button still needs an aria-label. The Popup never takes focus, so put only text in it; a link or a button belongs in a Popover. A disabled button cannot be focused, so its tooltip never opens: use aria-disabled="true" instead, and ignore its presses. Wrap a toolbar's tooltips in a Group, so that once one is open the next opens at once.
- Size
- 9.8 kB
Example
import { Tooltip } from "menhir/tooltip";
const trigger =
"size-8 cursor-pointer rounded-md border border-stone-300 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600";
const popup =
"m-0 origin-(--transform-origin) overflow-visible rounded border-0 bg-stone-800 px-2 py-1 text-xs text-white transition-[opacity,scale] duration-100 data-closed:scale-95 data-closed:opacity-0 dark:bg-stone-200 dark:text-stone-950 starting:data-open:scale-95 starting:data-open:opacity-0";
const arrow =
"absolute size-2 rotate-45 bg-stone-800 data-[side=bottom]:-top-1 data-[side=left]:-right-1 data-[side=right]:-left-1 data-[side=top]:-bottom-1 dark:bg-stone-200";
export default function TooltipExample() {
return (
<Tooltip.Group>
<div class="flex gap-1">
<Tooltip.Root>
<Tooltip.Trigger class={trigger} aria-label="Bold">
<b>B</b>
</Tooltip.Trigger>
<Tooltip.Popup class={popup} sideOffset={8}>
<Tooltip.Arrow class={arrow} />
Make text bold (Ctrl+B)
</Tooltip.Popup>
</Tooltip.Root>
<Tooltip.Root>
<Tooltip.Trigger class={trigger} aria-label="Italic">
<i>I</i>
</Tooltip.Trigger>
<Tooltip.Popup class={popup} sideOffset={8}>
<Tooltip.Arrow class={arrow} />
Make text italic (Ctrl+I)
</Tooltip.Popup>
</Tooltip.Root>
</div>
</Tooltip.Group>
);
}
Anatomy
<Tooltip.Group>
<Tooltip.Root>
<Tooltip.Trigger />
<Tooltip.Popup>
<Tooltip.Arrow />
</Tooltip.Popup>
</Tooltip.Root>
</Tooltip.Group>API reference
Root
Holds the open state and the hover delays, and renders no element. Every part has data-open or data-closed.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether the tooltip is open, when controlled. Pair it with onOpenChange. |
defaultOpen | boolean | false | Whether the tooltip is open at first, when uncontrolled. |
onOpenChange | (open: boolean, details: { reason, event }) => void | — | Called with the new value when the user opens or closes the tooltip, and why: "trigger-hover", "trigger-focus" (keyboard focus, or blur), "trigger-press" or "escape-key". A controlled tooltip changes only if open does. |
delay | number | 600 | How long the pointer rests on the Trigger before the tooltip opens, in milliseconds. In a Group, the Group's delay when unset. |
closeDelay | number | 0 | How long after the pointer has left the Trigger and the Popup the tooltip closes, in milliseconds. The pointer can cross the gap to the Popup without it. |
Trigger
<button type="button"> that the tooltip describes, with aria-describedby naming the Popup. Name it with its content or an aria-label. Your aria-describedby replaces the Popup's; aria-describedby={false} removes it, for a tooltip that only repeats the Trigger's name. It may render a link or another focusable element.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the trigger with your own component. Read once. |
Popup
<div popover="manual" role="tooltip">, placed against the Trigger. Its id is generated, for the Trigger to name. It never takes focus. The browser's own popover styles scroll its overflow: set overflow: visible to show an Arrow outside its edge.
| Prop | Type | Default | Description |
|---|---|---|---|
side | "top" | "right" | "bottom" | "left" | "top" | The side of the Trigger it is placed against. It moves to the other side when it does not fit, and data-side says where it went. |
align | "start" | "center" | "end" | "center" | Where along that side it sits. start and end follow the writing direction. |
sideOffset | number | 0 | The gap between the Trigger and the Popup, in pixels. |
alignOffset | number | 0 | The shift along the Trigger's side, in pixels. |
collisionPadding | number | 5 | The space it keeps from the viewport's edges, in pixels. |
anchor | Element | VirtualElement | (() => Element | VirtualElement | undefined) | — | What it is placed against instead of the Trigger: an element, a virtual element with a getBoundingClientRect(), such as a point, or a function returning one, for an element you hold in a ref. |
render | (props) => JSX.Element | — | Renders the popup with your own component. Read once. |
Arrow
aria-hidden <div> that points at the center of the Trigger: Menhir sets its left or top. Position it absolutely on the Popup's edge by its data-side, which it shares with the Popup. An app that renders no Arrow does not bundle the code that places it.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the arrow with your own component. Read once. |
Group
Renders no element. Once one tooltip inside is open, hovering another Trigger opens its tooltip at once and closes the first, as in a toolbar. An app that renders no Group does not bundle it.
| Prop | Type | Default | Description |
|---|---|---|---|
delay | number | 600 | The hover delay of the tooltips inside that set none, in milliseconds. |
timeout | number | 400 | How long after the last tooltip closed the next still opens at once, in milliseconds. |
Keyboard
| Keys | Action |
|---|---|
| Tab | Onto the Trigger, opens the tooltip; away from it, closes it. Tab never moves into the tooltip. |
| Escape | Closes the tooltip, and only it: a dialog or popover below it stays open. Focus stays on the Trigger. |
| Enter | On the Trigger, presses it, which closes the tooltip. |
| Space | On the Trigger, presses it, which closes the tooltip. |