Menhir

Tooltip

View as Markdown

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

Styling
TooltipExample.tsx
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.

PropTypeDefaultDescription
openboolean—Whether the tooltip is open, when controlled. Pair it with onOpenChange.
defaultOpenbooleanfalseWhether 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.
delaynumber600How long the pointer rests on the Trigger before the tooltip opens, in milliseconds. In a Group, the Group's delay when unset.
closeDelaynumber0How 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.

PropTypeDefaultDescription
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.

PropTypeDefaultDescription
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.
sideOffsetnumber0The gap between the Trigger and the Popup, in pixels.
alignOffsetnumber0The shift along the Trigger's side, in pixels.
collisionPaddingnumber5The space it keeps from the viewport's edges, in pixels.
anchorElement | 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.

PropTypeDefaultDescription
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.

PropTypeDefaultDescription
delaynumber600The hover delay of the tooltips inside that set none, in milliseconds.
timeoutnumber400How long after the last tooltip closed the next still opens at once, in milliseconds.

Keyboard

KeysAction
TabOnto the Trigger, opens the tooltip; away from it, closes it. Tab never moves into the tooltip.
EscapeCloses the tooltip, and only it: a dialog or popover below it stays open. Focus stays on the Trigger.
EnterOn the Trigger, presses it, which closes the tooltip.
SpaceOn the Trigger, presses it, which closes the tooltip.