An accessible tooltip pattern built on native hint popovers with a supporting custom element.
ui-tooltip keeps the tooltip surface native and adds the ownership and interaction behavior the platform does not provide by itself.
import { TooltipElement } from 'inclusive-elements';
window.customElements.define('ui-tooltip', TooltipElement);<!-- Adjacent sibling tooltip -->
<button type="button">Save</button>
<ui-tooltip popover="hint" data-placement="top">Save changes</ui-tooltip>
<!-- Explicit owner with for -->
<button id="share-button" type="button">Share</button>
<ui-tooltip for="share-button" popover="hint" data-placement="top">
Create a shareable link
</ui-tooltip>- Requires modern browser support for the Popover API and hint popovers with
popover="hint". - The styling examples use CSS anchor positioning.
ui-tooltipaddspopover="hint"androle="tooltip"if they are not already present.- The tooltip ensures it has an
idand adds that value to the owner'saria-describedby.
- Use whichever ownership style fits the markup best: if the tooltip has a
forattribute, it targets the element with thatid; otherwise, it targets its immediately preceding element sibling. - Changing
for,id, ordisabledre-syncs the owner relationship automatically.
- The
disabledattribute, or the reflectedtooltip.disabledproperty, prevents the tooltip from showing and clears any current owner relationship. - Tooltips do not show while the tooltip itself is
disabled, or when the resolved owner isdisabledoraria-disabled="true". - The tooltip shows on keyboard focus and pointer hover, hides on blur, pointer exit, click, and Escape, and stays open while the pointer moves between the owner and the tooltip.
- Tooltip content should stay descriptive, supplemental, and non-interactive.
- Do not put required instructions or critical information in a tooltip, because hover is not reliably available on touch and other coarse-pointer devices.
- If you need buttons, links, form controls, or information the user must be able to discover, use a popover or dialog pattern instead.
// Default delay in milliseconds before the tooltip shows or hides.
TooltipElement.delay = 100;
// Override the delay for one tooltip instance.
tooltip.setAttribute('delay', '200');
// Reflect the disabled attribute as a property when convenient.
tooltip.disabled = true;
// Convenience methods for programmatic control.
tooltip.show();
tooltip.hide();ui-tooltip is unstyled. Position the tooltip with native anchor positioning:
[popover] {
position: fixed;
position-anchor: auto;
}
ui-tooltip[popover][data-placement='top'] {
position-area: block-start;
max-inline-size: 18rem;
padding: 0.5rem 0.75rem;
}
ui-tooltip[popover][data-placement='bottom'] {
position-area: block-end;
}