Skip to content

Behaviors — Tooltip

<button and-tooltip="Saves your changes" and-tooltip-placement="top">
Hover me
</button>
AttributeValuesDefaultNotes
and-tooltiptextRequired — content shown in the tooltip.
and-tooltip-placementTooltipPlacement'top'12 values: top/bottom/left/right + -start/-end.
and-tooltip-delay / and-tooltip-show-delaynumber (ms)200Either attribute sets the show delay.
and-tooltip-hide-delaynumber (ms)0
and-tooltip-offsetnumber (px)8Gap between anchor and tooltip.
and-tooltip-disabledboolean attributefalse
and-tooltip-interactiveboolean attributefalseAllow hovering onto the tooltip content without it closing.

Dismissible with Escape without moving the pointer, per WCAG 1.4.13.

import { createTooltip } from '@andersseen/behaviors/tooltip';
const tooltip = createTooltip(document.getElementById('save-btn'), {
content: 'Saves your changes',
placement: 'top',
});
OptionTypeDefaultNotes
contentstring''No-ops if empty — nothing renders.
placementTooltipPlacement'top'12 values: top/bottom/left/right + -start/-end.
showDelaynumber (ms)200
hideDelaynumber (ms)0
offsetnumber (px)8Gap between anchor and tooltip.
disabledbooleanfalse
interactivebooleanfalseAllow hovering onto the tooltip content without it closing.

DEFAULT_TOOLTIP_CONFIG is exported alongside the factory.

MemberSignatureNotes
elementThe anchor passed in.
show() / hide()() => voidThe same triggers and-tooltip wires (mouseenter/focus, mouseleave/blur) call these — invoke directly to show/hide from your own logic.
updateOptions(options)(options: Partial<TooltipConfig>) => voidSetting disabled: true force-closes an open tooltip immediately.
destroy()() => voidRemoves the anchor’s listeners and any open tooltip node.

Behavior worth knowing that isn’t visible from the attribute API: the tooltip element is appended to document.body (position: fixed, positioned via requestAnimationFrame after the anchor’s layout settles) and auto-dismisses if any scrollable ancestor of the anchor scrolls — it doesn’t reposition on scroll, it closes, so a stale tooltip never drifts away from its anchor.

Wires aria-describedby from the anchor to the tooltip’s role="tooltip" element while it’s open, and dismisses on Escape without requiring the pointer to move — both anchor focus/blur and mouseenter/mouseleave trigger show/hide, so keyboard-only users get the same content as mouse users.

Recipes → Tooltip on a disabled control