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
idthat matches therelattribute 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,topandbottom, relative to the element that opens the tooltip. - With
smart: truethe tooltip only checks that it fits in the window. - An element with a
titleand norelgets a tooltip made from thetitle. - The settings you give the constructor are the defaults for every tooltip, and
data-tooltipoverrides them.
| Event | Fired when | event.detail |
|---|---|---|
tooltip.opened |
a tooltip is opened | TooltipEventDetail |
tooltip.closed |
a tooltip is closed | TooltipEventDetail |
Examples
Section titled “Examples”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);});Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Tooltip(
settings?):Tooltip
Defined in: components/tooltip/tooltip.ts:194
Parameters
Section titled “Parameters”settings?
Section titled “settings?”The defaults for every tooltip. data-tooltip and Tooltip.show override them.
Returns
Section titled “Returns”Tooltip
Throws
Section titled “Throws”If settings is not an object.
Methods
Section titled “Methods”destroy()
Section titled “destroy()”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.
Returns
Section titled “Returns”void
hide()
Section titled “hide()”hide():
void
Defined in: components/tooltip/tooltip.ts:250
Hides every tooltip.
Returns
Section titled “Returns”void
init()
Section titled “init()”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.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If an element has neither a rel nor a title, or there are no .tooltip elements on the page.
show()
Section titled “show()”show(
trigger,options?):void
Defined in: components/tooltip/tooltip.ts:245
Shows the tooltip of an element, next to it.
Parameters
Section titled “Parameters”trigger
Section titled “trigger”HTMLAnchorElement
The element that opens the tooltip. Its rel is the id of the tooltip.
options?
Section titled “options?”Overrides the settings of the constructor, for this tooltip.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If no element has the id in rel.
Example
Section titled “Example”tooltips.show(link, { width: 400, align: 'top' });