Tooltip¶
A floating tooltip that shows when the pointer rests on, or keyboard focus reaches, any element under a root that has a data-stolon-tip attribute.
Decision¶
| When | Use |
|---|---|
| Explaining an icon only control or a short label | Tooltip. |
| Essential information | A Help Hint, or a fieldDescription for a field. Never put it only in a tooltip. |
Usage¶
import { bindTooltips, setTooltip } from './configurationpage?name=myplugin_stolon.js';
var tips = bindTooltips(view);
setTooltip(button, 'Runs the task now.');
<button type="button" data-stolon-tip="Runs the task now." data-stolon-tip-placement="bottom">Run</button>
Classes: stolon-tooltip. The handle is { element, show, hide, destroy }. show(target) shows the tip for one element.
States and Modifiers¶
- The tip hides on leave, blur, Escape, an outside click, a scroll, and a resize, and flips below when there is no room above.
data-stolon-tip-placement="bottom"places it below. bindTooltips(root, { placement })takestop(default) orbottomfor every tip, anddata-stolon-tip-placementoverrides it for one element.- The target gets
aria-describedbywhile the tip shows. setTooltipgives a plain elementtabindex="0".
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
setTooltip¶
function setTooltip<T extends Element>(target: T, text: string, placement?: TooltipPlacement | string): T
Gives an element tooltip text through the data-stolon-tip attribute that bindTooltips reads. A target that cannot take focus gets tabindex 0 so the tooltip also shows from the keyboard.
bindTooltips¶
function bindTooltips(root?: HTMLElement | null, options?: TooltipOptions): TooltipHandle
Shows a floating tooltip when the pointer rests on, or keyboard focus reaches, any element under root that has a data-stolon-tip attribute. Escape, a click outside, scrolling, a resize, and leaving the element hide it, and aria-describedby points at the tip while it shows.
| Option | Type | What it does |
|---|---|---|
placement |
TooltipPlacement | string |
| Handle member | Type |
|---|---|
show |
(target: Element): void |
hide |
(): void |
destroy |
(): void |
| Type | What it is |
|---|---|
TooltipPlacement |
The side of its target a tooltip prefers, before it flips to stay on screen. |
| Class | What it looks like |
|---|---|
stolon-tooltip |
Floating tooltip bubble on the stolon-float surface that bindTooltips positions beside its target, above a dialog and a toast |
Example¶
Example Code¶
<!-- title: Tooltip -->
<!-- min-height: 140 -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const target = base.setTooltip(document.createElement('button'), 'Shown on hover and on keyboard focus.');
target.type = 'button';
target.className = 'raised stolon-button stolon-small';
target.textContent = 'Hover or focus me';
mount.appendChild(target);
base.bindTooltips(mount);
</script>