Tooltip
A short, single-line explanation that appears on mouse hover or keyboard focus.
zb:tooltip zubazar/ui · free (MIT)
Basic usage
Put the element to describe inside the component and give the text with text.
<div class="py-6">
<zb:tooltip text="The customer's order history">
<zb:button variant="white" size="sm">History</zb:button>
</zb:tooltip>
</div>
| Prop | Values |
|---|---|
text | Tooltip text (single line) |
Position
The tooltip opens above the element; near the top edge of the page, use position="bottom".
<div class="flex gap-4 py-6">
<zb:tooltip text="Opens above"><zb:button variant="outline-dark" size="sm">Top</zb:button></zb:tooltip>
<zb:tooltip text="Opens below" position="bottom"><zb:button variant="outline-dark" size="sm">Bottom</zb:button></zb:tooltip>
</div>
| Prop | Values |
|---|---|
position | top | bottom |
Icon buttons
Use tooltips on icon-only buttons, and still give the button an aria-label.
<div class="flex gap-1 py-6">
<zb:tooltip text="Edit">
<zb:button variant="ghost" size="icon" aria-label="Edit"><zb:icon name="pencil-square" /></zb:button>
</zb:tooltip>
<zb:tooltip text="Print">
<zb:button variant="ghost" size="icon" aria-label="Print"><zb:icon name="printer" /></zb:button>
</zb:tooltip>
<zb:tooltip text="Delete permanently">
<zb:button variant="ghost" size="icon" aria-label="Delete permanently" class="text-danger-ink"><zb:icon name="trash" /></zb:button>
</zb:tooltip>
</div>
| Prop | Values |
|---|---|
aria-label | Accessible name of the button |
aria-describedby | Automatic: added to the first focusable element in the slot |
In scrollable areas
The tooltip is not cut off at the edge of scrolling areas; add tabindex="0" to an element that cannot take focus, such as a badge.
<div class="max-w-xs overflow-x-auto rounded-sm border border-line py-6">
<div class="flex w-[36rem] items-center gap-6 px-4 text-sm">
<span>Kadıköy warehouse</span>
<zb:tooltip text="Warehouse code: KDK-01"><zb:badge variant="info" tabindex="0">KDK-01</zb:badge></zb:tooltip>
<span>312 orders</span>
</div>
</div>
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
text | — | '' | Tooltip text (role="tooltip") |
position | — | top | top · bottom |
Slots: default: the element the tooltip is attached to; the first focusable element inside it (a link, a button, a form field or an element with tabindex) gets aria-describedby. Hover and focus open it, Esc closes it.
Developer notes
- For explanations longer than a few words, use a popover (
zb:popover). - The tooltip is rendered at the outermost level of the page (
body), so horizontally scrolling tables and panels do not cut it off.
Accessibility
- ARIA and roles
- The tooltip has
role="tooltip". The first link, button or form field inside is linked to it witharia-describedby, so screen readers read the text as that element description. - Showing and hiding
- It opens on hover and on keyboard focus, and closes when focus leaves. The pointer can move onto the tooltip; a short delay keeps it open.
- Keyboard
- Escape hides it, even when the pointer opened it. While focus is on the element, Escape closes only the tooltip, not a modal or drawer around it.
- What you provide
- The content must be focusable: give plain text or an icon
tabindex="0". A tooltip is a description, not a name, so an icon-only button still needs anaria-label.