Overview
A tooltip holds short, optional help. Its trigger is a help icon from Feedback Icons, a word in a sentence, or a button. The balloon holds a message, with an optional title and a help icon, and an arrow points at the trigger. You write the markup yourself, or put the text in data attributes and the script builds the parts.
Pick another component when:
Markup
Variants
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Auto (default) | — | — | A help icon next to a field label or a heading, from two attributes. The script builds the trigger and the balloon |
| Structure | Manual (hint: You write every part) | canon #tooltip-help |
— | The same help icon with every part written out. Use it for other content in the balloon |
| Structure | Term (hint: A word in a sentence is the trigger) | canon #tooltip-term |
— | A word in a sentence is the trigger, with a dotted underline |
| Structure | Button (demo: + hover-500) | canon #tooltip-button |
— | An icon-only button whose title becomes the message. Add data-tooltip-hover="500": a button needs hover, so its own click still works. An <a> link takes the same classes and attributes |
| Open on | Click (default) | — | .nds-tooltip:not(.nds-btn) |
A click, a tap, or Enter on the trigger opens and closes the balloon |
| Open on | Hover | [data-tooltip-hover] |
.nds-tooltip |
The balloon opens 120ms after the mouse enters, and on keyboard focus. Use it on a link or a button |
| Open on | Hover after 500ms (id: hover-500) | [data-tooltip-hover="500"] |
.nds-tooltip |
The same, with a longer wait. Use it in a row of icon buttons, so a passing mouse opens nothing |
| Position | Below (default) (hint: The balloon opens below the trigger) | — | — | The balloon opens below the trigger, or above it when there is no room below |
| Position | Beside (hint: The balloon opens beside the trigger) | [data-position="end"] |
.nds-tooltip |
The balloon opens at the end side of the trigger, or at the start side when there is no room. Use it in a column of icons, so the balloon does not cover the next icon. start opens it at the start side first |
| Status | Info | [data-tooltip-status="info"] |
.nds-tooltip[data-tooltip-title] |
The help icon uses the info status. Auto structure only |
| Status | Success | [data-tooltip-status="success"] |
.nds-tooltip[data-tooltip-title] |
The help icon uses the success status. Auto structure only |
| Status | Warning | [data-tooltip-status="warning"] |
.nds-tooltip[data-tooltip-title] |
The help icon uses the warning status. Auto structure only |
| Status | Error | [data-tooltip-status="error"] |
.nds-tooltip[data-tooltip-title] |
The help icon uses the error status. Auto structure only |
Behavior
Click Mode
By default a click on the trigger opens the balloon, and a second click closes it. The click opens only the balloon: on a link or a button, the link does not open and the button action does not run.
Hover Mode
data-tooltip-hover opens the balloon when the mouse rests on the trigger, and when the trigger gets focus from the keyboard. The balloon stays open while the mouse moves into it, and closes when the mouse leaves both. A click during the wait cancels the open. On a link or a button, a click or a tap runs the action and closes the balloon. On a text term, a tap opens and closes the balloon, and a mouse click does nothing.
Auto Markup
Put data-tooltip-title, data-tooltip-message or both on .nds-tooltip, and the script builds the parts you did not write. An empty root gets a help icon trigger. A root with text or elements is the trigger itself. The balloon gets a help icon only when it has a title. A part you wrote is always kept.
Text Term
A term is a .nds-tooltip with text inside, and nds-term adds a dotted underline and the help cursor. The script makes the term reachable with Tab. In click mode it also gives the term role="button", and Enter or Space opens and closes the balloon.
Title Attribute
With no data-tooltip-message, the root’s title is the message. Until the script loads, the browser shows title as its own tooltip. Then the script removes title, so the two never show together. An element with no other name keeps the text as its aria-label. The balloon is built on the first open, so a row of icon buttons adds no hidden balloons to the page.
Built-in Features
-
Auto-initialization
Every
.nds-tooltipon the page starts by itself, after the page first shows. CallNDS.Tooltip.init()after you add new ones. -
Balloon Position
While open, the balloon moves to
<body>, so no container cuts it off. It opens below the trigger, and above it when there is more room there. It never runs past the side of the screen, and the arrow points at the trigger. -
Dismissal
A click outside or Escape closes the balloon, wherever focus is, so a balloon opened by hover closes too. While the page scrolls, the balloon stays on its trigger and closes when the trigger leaves view. A browser without CSS anchor positioning closes it on a page scroll instead. Escape on the trigger also returns focus to it. Only one tooltip is open at a time.
-
Accessibility
The balloon gets
role="tooltip"and an id. The trigger'saria-describedbypoints to it, so a screen reader reads the message when the trigger gets focus. -
No Layout Shift
An empty auto root keeps the space of its help icon until the script adds it, so the text after it does not move.
-
Programmatic Control
Open, close and remove a tooltip from a script, and listen to its open and close events.
Best Practices
- Put only optional help in a tooltip. Text the user needs to finish the task goes in the label or the info text.
- Keep the message to one or two sentences. The balloon is at most 240px wide, and the text wraps.
- Put the trigger right after the term or the label it explains. In a form field it goes in the field’s header, after the
label: see the Help option in Forms. - Use the auto markup for plain text. Write the parts yourself only for other content in the balloon.
- Give a help icon trigger an
aria-labelthat names what it explains, such as “About the National ID”. An auto trigger takes the title, or “More info” when there is no title. - Add
data-tooltip-hoverto a link or a button trigger, so its own click still works. - In a form with many tooltips, move the text into info text. Many clicks slow down keyboard and touch users.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-tooltip-title |
.nds-tooltip |
The balloon title. The script builds the balloon from it |
data-tooltip-message |
.nds-tooltip |
The balloon message. The script builds the balloon from it |
data-tooltip-status |
.nds-tooltip |
The status of the help icon the script builds, such as info or error. No value gives the help look. It does not change a trigger or balloon you wrote |
title |
.nds-tooltip |
The message when data-tooltip-message is absent |
data-tooltip-hover |
.nds-tooltip |
Opens on hover and keyboard focus. The value is the wait in milliseconds, 120 by default |
hidden |
.nds-tooltip-balloon |
Keeps the balloon closed at load. The script removes it on open |
data-state |
.nds-tooltip |
The script writes open while the balloon shows. It skips a root that is a link or a button, which has its own open look |
data-position-vertical |
.nds-tooltip, .nds-tooltip-balloon |
The script writes top when the balloon opens above the trigger. The arrow then points down |
data-position |
.nds-tooltip |
end or start. The balloon opens beside the trigger, on that side first |
data-position-horizontal |
.nds-tooltip, .nds-tooltip-balloon |
With data-position, the script writes left or right: the side the balloon opened on. The arrow points back at the trigger |
Keyboard
| Key | Where | Effect |
|---|---|---|
| Enter, Space | trigger | Opens or closes the balloon. On a text term, only in click mode |
| Tab | trigger | In hover mode, focus opens the balloon and leaving closes it |
| Escape | anywhere | Closes the open balloon. On the trigger, also returns focus to it |
CSS Custom Properties
Set these on .nds-tooltip-balloon, or on :root for every tooltip. The balloon moves to <body> while open, so a value set on .nds-tooltip does not reach it.
| Property | Default | Controls |
|---|---|---|
--tooltip-max-width |
240px |
Maximum width of the balloon |
--tooltip-padding |
var(--spacing-md) |
Space inside the balloon |
--tooltip-gap |
var(--spacing-md) |
Space between the icon and the text, and between the title and the message |
--tooltip-arrow-size |
10px |
Size of the arrow |
Tokens
Set a token at :root, or on a wrapper to reach every tooltip inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the tooltip group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--tooltip-background-default | var(--colors-base-white)Dark mode var(--colors-neutral-800) | |
--tooltip-text-heading-default | var(--text-display)Dark mode var(--colors-neutral-50) | |
--tooltip-text-paragraph-default | var(--text-primary-paragraph)Dark mode var(--colors-neutral-100) | |
--tooltip-help-idle | var(--colors-neutral-500)Dark mode var(--colors-neutral-600) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_tooltip.scss.
| Token | Preview | Value |
|---|---|---|
--border-neutral-secondaryRead by border | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) |
JavaScript
The NDS.Tooltip script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
The instance is on the element as el.ndsTooltip. It is there once the component starts: code that runs at page load waits for NDS.init.ready.
| Method | Effect |
|---|---|
NDS.Tooltip.init() |
Starts every .nds-tooltip on the page that has not started yet |
NDS.Tooltip.create(el) |
Starts one tooltip and returns its instance, or the instance it already has. Returns null when it has no balloon and no data-tooltip-title or data-tooltip-message to build one |
NDS.Tooltip.destroy(el) |
The same as el.ndsTooltip.destroy() |
el.ndsTooltip.open(), .close() |
Opens or closes the balloon |
el.ndsTooltip.destroy() |
Closes the balloon, removes the listeners, and takes back what the script wrote: the title it moved, the trigger and balloon it built, aria-describedby, tabindex and role. A balloon you wrote keeps its own attributes. The tooltip can start again after it |
| Event | Fired on | Detail |
|---|---|---|
nds:tooltip:opened |
.nds-tooltip (bubbles) |
{ tooltip, trigger, balloon, isOpen } |
nds:tooltip:closed |
.nds-tooltip (bubbles) |
{ tooltip, trigger, balloon, isOpen } |
A balloon built from title is not in the page before its first open. Read it from the nds:tooltip:opened detail. While open, the balloon is in <body>, so closest('.nds-tooltip') from inside it finds nothing.
The full API is in the banner of _js/nds-tooltip.js.