Skip to content

Tooltip

Defined in: components/tooltip/tooltip.ts:179

A message that appears when the cursor is over an element.

  • Each tooltip should have a unique id that matches the rel attribute of the element that opens it.
  • Nested tooltips are not supported.
  • Show tooltips with data-tooltip, or with Tooltip.show.
  • Valid alignments are right, left, top and bottom, relative to the element that opens the tooltip.
  • With smart: true the tooltip only checks that it fits in the window.
  • An element with a title and no rel gets a tooltip made from the title.
  • The settings you give the constructor are the defaults for every tooltip, and data-tooltip overrides them.
Event Fired when event.detail
tooltip.opened a tooltip is opened TooltipEventDetail
tooltip.closed a tooltip is closed TooltipEventDetail

Markup of a tooltip

<div class="tooltip" id="unique-id1">
<div class="tooltip-content">
<h1 class="tight">Hello! I am a tooltip</h1>
<p class="tight">I'm some text about the tooltip.</p>
</div>
</div>

Showing a tooltip from a link: its rel has to equal the id of the tooltip

<a href="#" rel="unique-id1" data-tooltip="width:200:align:bottom">open tooltip</a>
const tooltips = new Tooltip({ delay: true });
tooltips.init();

Showing a tooltip with JavaScript, next to the element that opens it

tooltips.show(document.getElementById('open') as HTMLAnchorElement, { width: 50 });

A tooltip made from the title of a link

<a href="#" data-tooltip="smart:true:delay:true" title="I am a tooltip">tooltip link</a>

Listening for events

document.addEventListener('tooltip.opened', (event) => {
const { tooltip, options } = (event as CustomEvent<TooltipEventDetail>).detail;
console.log('opened', tooltip, options);
});

new Tooltip(settings?): Tooltip

Defined in: components/tooltip/tooltip.ts:194

TooltipOptions

The defaults for every tooltip. data-tooltip and Tooltip.show override them.

Tooltip

If settings is not an object.

destroy(): void

Defined in: components/tooltip/tooltip.ts:266

Removes the listeners, gives back the titles, removes the tooltips that were made from them, and resets the others. Tooltip.init starts again, with the same settings.

void


hide(): void

Defined in: components/tooltip/tooltip.ts:250

Hides every tooltip.

void


init(): void

Defined in: components/tooltip/tooltip.ts:204

Finds the [data-tooltip] elements and the .tooltip elements on the page, makes tooltips for the elements that have a title and no rel, and binds the events. Calling it again scans the page again.

void

If an element has neither a rel nor a title, or there are no .tooltip elements on the page.


show(trigger, options?): void

Defined in: components/tooltip/tooltip.ts:245

Shows the tooltip of an element, next to it.

HTMLAnchorElement

The element that opens the tooltip. Its rel is the id of the tooltip.

TooltipOptions

Overrides the settings of the constructor, for this tooltip.

void

If no element has the id in rel.

tooltips.show(link, { width: 400, align: 'top' });