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.

<button type="button" class="btn" data-tooltip="Copies the booking reference">Copy reference</button>

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>

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>

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

NameTypeDefaultDescription
data-tooltiprequiredstringnoneThe text of the tooltip; empty when data-tooltip-target names the content instead
data-tooltip-targetstringnoneA selector for an element whose content is the tooltip, for a description with markup; hide the element on the page yourself
data-tooltip-placementtop | bottom | left | righttopWhich side of the control the tooltip sits on; it flips when there is no room For every instance, Tooltip.defaults.placement.
data-tooltip-arrowbooleantrueThe triangle pointing at the control For every instance, Tooltip.defaults.arrow.
data-tooltip-delaynumber150Milliseconds the pointer rests before the tooltip shows; focus shows it at once For every instance, Tooltip.defaults.delay.
data-tooltip-offsetnumber8Pixels between the control and the tooltip For every instance, Tooltip.defaults.offset.
data-tooltip-stateset by the componentopen | closednoneWhether the tooltip is shown

Events

NameDetailDescription
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

NameDefaultDescription
--tooltip-bgvar(--color-inverse-surface)Background
--tooltip-colorvar(--color-inverse-text)Text colour
--tooltip-radiusvar(--surface-control-radius)Corner radius
--tooltip-padding0.35em 0.6emPadding
--tooltip-font-size0.8125remFont size
--tooltip-max-width18remWidest a tooltip grows before wrapping
--tooltip-arrow-size0.5remSide of the triangle
--tooltip-shadowvar(--surface-panel-shadow)Shadow
--tooltip-z-index2147483646Stacking; one under the toasts
--tooltip-transition-durationvar(--framework-transition-duration)How long the fade and move in take; none under prefers-reduced-motion