--- url: "https://web.dev/articles/components-howto-tooltip" titulek: "HowTo Components – howto-tooltip | Articles | web.dev" h1: "HowTo Components – howto-tooltip Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/components-howto-tooltip" drobecky: ["Articles", "HowTo Components – howto-tooltip"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 03:31:55" znaku: 3478 slov: 407 kodovani: "iso-8859-2" sha256: "bbd044cbd2ab0f202cc08ed21da0f43c73cdf4a4cf308c7c21e7db28ac7ece72" --- HowTo Components – howto-tooltip Stay organized with collections Save and categorize content based on your preferences. Ewa Gasperowicz Rob Dodson Surma Summary A `` is a popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it. The element that triggers the tooltip references the tooltip element with `aria-describedby`. The element self-applies the role `tooltip` and sets `tabindex` to -1, as the tooltip itself can never be focused. Reference [HowTo: Components on GitHub](https://github.com/GoogleChromeLabs/howto-components) [Tooltip pattern in ARIA Authoring Practices 1.1](https://www.w3.org/TR/wai-aria-practices-1.1/#tooltip) Demo [View live demo on GitHub](https://googlechromelabs.github.io/howto-components/howto-tooltip/#demo) Example usage `
Ideally your name is Batman
Help I am trapped inside a tooltip message` Code `class HowtoTooltip extends HTMLElement {` The constructor does work that needs to be executed exactly once. `constructor() { super();` These functions are used in a bunch of places, and always need to bind the correct this reference, so do it once. `this._show = this._show.bind(this); this._hide = this._hide.bind(this); }` `connectedCallback()` fires when the element is inserted into the DOM. It's a good place to set the initial role, tabindex, internal state, and install event listeners. `connectedCallback() { if (!this.hasAttribute('role')) this.setAttribute('role', 'tooltip'); if (!this.hasAttribute('tabindex')) this.setAttribute('tabindex', -1); this._hide();` The element that triggers the tooltip references the tooltip element with `aria-describedby`. `this._target = document.querySelector('[aria-describedby=' + this.id + ']'); if (!this._target) return;` The tooltip needs to listen to focus/blur events from the target, as well as hover events over the target. `this._target.addEventListener('focus', this._show); this._target.addEventListener('blur', this._hide); this._target.addEventListener('mouseenter', this._show); this._target.addEventListener('mouseleave', this._hide); }` `disconnectedCallback()` unregisters the event listeners that were set up in `connectedCallback()`. `disconnectedCallback() { if (!this._target) return;` Remove the existing listeners, so that they don't trigger even though there's no tooltip to show. `this._target.removeEventListener('focus', this._show); this._target.removeEventListener('blur', this._hide); this._target.removeEventListener('mouseenter', this._show); this._target.removeEventListener('mouseleave', this._hide); this._target = null; } _show() { this.hidden = false; } _hide() { this.hidden = true; } } customElements.define('howto-tooltip', HowtoTooltip);` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2017-04-06 UTC.