tooltip
tooltip shows a short line of text about a control. It shows when the pointer rests on the
control, or when the keyboard focuses it.
onChange called 0 time(s)
import { lucide, tooltip } from '@markless/ui';export default function TooltipBasic() @{ <tooltip.root class="tooltip"> <tooltip.trigger class="save"> <lucide.save class="icon" aria-hidden="true" /> Save </tooltip.trigger> <tooltip.content class="tip">Keeps a copy in your drafts</tooltip.content> </tooltip.root>;}
.tooltip { display: grid; justify-items: center; padding-block-start: 3.5em; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.save { display: inline-flex; gap: 0.5em; align-items: center; padding: 0.55em 1.1em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer; transition: background 140ms ease;}.save:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.save:active { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.save:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.icon { width: 1em; height: 1em; flex: none;}.tip { z-index: 5; width: max-content; max-width: 16rem; margin-block-end: 0.6em; padding: 0.35em 0.75em; border-radius: 6px; background: var(--slab); box-shadow: 3px 3px 0 color-mix(in srgb, var(--purple) 45%, var(--cream)); color: var(--slab-ink); font-family: 'Shantell Sans', system-ui, sans-serif;}.tip[hidden] { display: none;}@media (prefers-reduced-motion: reduce) { .save { transition: none; }}
Rest the pointer on Save, or Tab to it, and the tip shows. Change delay to set the wait. Turn
on open to show the tip from the start.
A tooltip describes a control. It does not name it. The trigger points at the tip with
aria-describedby, even while the tip is hidden. So a screen reader reads the tip when focus
reaches the button, whether the tip shows or not.
When to use it
Use tooltip for a hint that a person can live without, such as what a button does. The tip holds
text only. If it needs a link or a button, use hovercard or popover
instead. Touch screens never show a tooltip, so do not put anything there that a person needs.
Parts
The tooltip itself; the trigger and the tip go inside it, in that order.
tooltip.rootholds whether the tip shows, and the wait before it shows. It renders a<div>around both parts.tooltip.triggeris the<button>the tip describes.tooltip.contentis the tip, withrole="tooltip". A hidden tip getshidden. It stays in the page, soaria-describedbystill finds it.
When the tip shows and hides
The family's tests check each of these:
- A resting pointer waits for
delaybefore the tip shows. The default is 600 milliseconds. Setdelay={0}to show it at once. - Keyboard focus shows the tip at once. Focus from a mouse press does not show it.
- The pointer can move from the trigger onto the tip. The tip stays.
- The tip hides when the pointer leaves both parts. If the keyboard focused the trigger, the tip stays until focus leaves.
- A showing tip never hides on a timer.
- A press on the trigger hides the tip, and it stays hidden.
- A touch never shows the tip.
- Showing one tip hides another tip that keyboard focus held open. A menu that opens hides the tip too.
Where the tip sits
The tip sits above its trigger. That default is a CSS rule inside @layer markless. One rule of
yours outside a layer beats it:
.tip {
position-area: inline-end;
}The tooltip has no prop for its position. Placement is CSS. Styling has a live example. Two tooltips on one page each sit against their own trigger. The tip also finds its trigger when you write the content part before the trigger part.
Styling hooks
| Attribute | On | Present when |
|---|---|---|
ui-open |
root, trigger, content | The tip is showing. |
ui-closed |
root, trigger, content | The tip is hidden. |
tooltip.content also carries an overlay attribute. The family uses it to place the tip on top
of the page. It is not a styling hook.
Keyboard
| Key | What it does |
|---|---|
| Tab to the trigger | Shows the tip at once. Tab away and the tip hides. |
| Escape | Hides the tip. Focus stays on the trigger. |
Gotchas
API reference
tooltip.root
openbooleanWhether the tooltip is showing. Omit it and it starts hidden.Defaultfalse.delaynumberHow long the pointer must rest on the trigger before the tooltip shows, in milliseconds. Omit it and it is 600. Keyboard focus shows it at once, with no wait; the focus a click gives the trigger shows nothing.Default600.onChange(open: boolean) => voidCalled with the new value when the tooltip shows or hides, including when Escape or a press elsewhere hides it.
tooltip.trigger
No props of its own.
tooltip.content
No props of its own.