Enhancement
Tooltip
A short description of a control, shown beside it while the pointer rests on it or it has focus
- Import
@parallelogram-js/core/components/Tooltip- Selector
[data-tooltip]- Stylesheet
@parallelogram-js/core/styles/tooltip.css
Playground
Rest the pointer on the button, or tab to it.
The same tooltip on each side of its control, and one without the triangle.
<div class="button-row">
<button type="button" class="btn" data-tooltip="Above" data-tooltip-placement="top">Top</button>
<button type="button" class="btn" data-tooltip="Below" data-tooltip-placement="bottom">Bottom</button>
<button type="button" class="btn" data-tooltip="To the left" data-tooltip-placement="left">Left</button>
<button type="button" class="btn" data-tooltip="To the right" data-tooltip-placement="right">Right</button>
<button type="button" class="btn" data-tooltip="No triangle" data-tooltip-arrow="false">Plain</button>
</div>
Use the example to see the events it sends.
A description with markup, taken from a hidden element on the page.
<button type="button" class="btn" data-tooltip="" data-tooltip-target="#shortcut-tip">Save</button>
<div id="shortcut-tip" hidden>Saves the draft. <kbd>โ</kbd> <kbd>S</kbd></div>
Use the example to see the events it sends.
About
The tooltip is the text of the data-tooltip attribute, or the content of an element named by data-tooltip-target when the text is empty. It appears above the control after a short delay under the pointer and at once on keyboard focus, and goes when the pointer leaves, focus moves, the control is pressed, or Escape is pressed. It is a description, not a name: a control with only an icon still needs its own aria-label. The control gets aria-describedby pointing at the tooltip while it is shown.
The tooltip sits where data-tooltip-placement says and flips to the opposite side when there is no room, staying inside the viewport; the triangle keeps pointing at the control. It fades and moves into place over the framework's transition, and appears at once for a visitor who prefers reduced motion. Touch gets nothing: a tooltip under a finger has nowhere to be, so a control that needs its description on a phone should say it in its label.
Usage
Register the component and the framework loads it the first time a page contains [data-tooltip].
import { Parallelogram } from '@parallelogram-js/core';
const app = Parallelogram.create();
app.components.add('[data-tooltip]', () => import('@parallelogram-js/core/components/Tooltip'));
app.run();
The framework is optional. Tooltip.enhanceAll() mounts the component on every matching element by itself, for a page that wants one enhancement rather than a framework:
import Tooltip from '@parallelogram-js/core/components/Tooltip';
Tooltip.enhanceAll();
It takes a selector and the component's options, and returns the instance it mounted them with. What it leaves out is what the framework adds: loading a component only on the pages that use it, mounting again after a page swap, and ordering with dependsOn.
Its styles come from the package stylesheet:
@import '@parallelogram-js/core/styles/tooltip.css';
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
data-tooltiprequired | string | none | The text of the tooltip; empty when data-tooltip-target names the content instead |
data-tooltip-target | string | none | A selector for an element whose content is the tooltip, for a description with markup; hide the element on the page yourself |
data-tooltip-placement | top | bottom | left | right | top | Which side of the control the tooltip sits on; it flips when there is no room For every instance, Tooltip.defaults.placement. |
data-tooltip-arrow | boolean | true | The triangle pointing at the control For every instance, Tooltip.defaults.arrow. |
data-tooltip-delay | number | 150 | Milliseconds the pointer rests before the tooltip shows; focus shows it at once For every instance, Tooltip.defaults.delay. |
data-tooltip-offset | number | 8 | Pixels between the control and the tooltip For every instance, Tooltip.defaults.offset. |
data-tooltip-stateset by the component | open | closed | none | Whether the tooltip is shown |
Events
| Name | Detail | Description |
|---|---|---|
tooltip:show | { element: HTMLElement; tooltip: HTMLElement; placement: string } | Dispatched on the control when its tooltip appears, with the side it took DOM event and event bus |
tooltip:hide | { element: HTMLElement; tooltip: HTMLElement; placement: string } | Dispatched on the control when its tooltip goes DOM event and event bus |
tooltip:mount | { element: HTMLElement; timestamp: number } | A control was set up Event bus |
CSS custom properties
| Name | Default | Description |
|---|---|---|
--tooltip-bg | var(--color-inverse-surface) | Background |
--tooltip-color | var(--color-inverse-text) | Text colour |
--tooltip-radius | var(--surface-control-radius) | Corner radius |
--tooltip-padding | 0.35em 0.6em | Padding |
--tooltip-font-size | 0.8125rem | Font size |
--tooltip-max-width | 18rem | Widest a tooltip grows before wrapping |
--tooltip-arrow-size | 0.5rem | Side of the triangle |
--tooltip-shadow | var(--surface-panel-shadow) | Shadow |
--tooltip-z-index | 2147483646 | Stacking; one under the toasts |
--tooltip-transition-duration | var(--framework-transition-duration) | How long the fade and move in take; none under prefers-reduced-motion |