Tooltip
Show a short explanation when someone hovers over or focuses an element with a tooltip.
These examples use the following JavaScript initializers:
Example
Breakpoints
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxHTML
Component API
Initialization
Run after the component markup exists:
import { tooltipInit } from '@uswds-tailwind/compat/tooltip'
tooltipInit()Anatomy
Set authored attributes in your HTML. The component manages generated attributes. Types describe HTML attribute values.
root
Wraps the trigger and tooltip content.
| Attribute | Type | Description |
|---|---|---|
data-scope |
"tooltip" |
Authored. Identifies the component scope. |
data-part |
"root" |
Authored. Identifies this part. |
data-placement |
"top" | "bottom" | "left" | "right" |
Authored. Preferred side: top, bottom, left, or right. Defaults to top; invalid values fall back to top. The placement option takes precedence. |
trigger
The element described by the tooltip.
| Attribute | Type | Description |
|---|---|---|
data-part |
"trigger" |
Authored. Identifies this part. |
title |
string |
Authored. Optional tooltip text. Copied to the content and removed during initialization. |
aria-describedby |
ID references |
Generated. Relationship to tooltip content. |
content
Author tooltip text here when not using a trigger title.
| Attribute | Type | Description |
|---|---|---|
data-part |
"content" |
Authored. Identifies this part. |
role |
"tooltip" |
Generated. Accessibility role. |
aria-hidden |
"true" | "false" |
Generated. Accessibility visibility. |
data-state |
"open" | "closed" |
Generated. "open" or "closed"; use for styling. |
data-visible |
presence attribute |
Generated. Present after the 20 ms reveal delay; use for the fade-in. |
data-placement |
"top" | "bottom" | "left" | "right" |
Generated. Resolved side of the trigger; may differ from the preferred side to fit available space. |
data-wrap |
presence attribute |
Generated. Present when width needs to be constrained. |
CSS variables
| Variable | Purpose |
|---|---|
--tooltip-top, --tooltip-bottom |
Vertical position. |
--tooltip-left, --tooltip-right |
Horizontal position. |
--tooltip-margin |
Positioning offset. |
--arrow-offset, --arrow-transform |
Arrow offset and rotation for the resolved side. |
Configuration
Pass these JavaScript options to the instance constructor or factory. Authored configuration attributes are read during construction.
| Option | Description |
|---|---|
placement |
Preferred side: top, bottom, left, or right. Overrides data-placement; defaults to top. |
defaultOpen |
boolean. Initial uncontrolled state; defaults to false. |
open |
boolean. Controlled state; your application supplies updates after change callbacks. |
id, ids |
Customize the instance identifier and generated element IDs. |
Methods
| Method | Returns | Description |
|---|---|---|
api.setOpen(open) |
void |
Requests an open-state change. |
destroy() |
void |
Stops the instance; does not restore a removed title or the original HTML. |
import { Tooltip } from '@uswds-tailwind/compat/tooltip'
const tooltip = Tooltip.getOrCreateInstance( '[data-scope="tooltip"][data-part="root"]',)
tooltip.api.setOpen(true)See initialization and cleanup.
Callbacks
| Callback | Payload | When it runs |
|---|---|---|
onOpenChange |
{ open: boolean } |
An open-state change is requested. |
Pass callbacks in instance options. These are JavaScript functions, not emitted DOM events.
Accessibility
- Give the trigger an accessible name independently of the tooltip.
- Hover or focus reveals it; leaving, blurring, or Escape dismisses it.
- Keep essential instructions visible and interactive controls outside the tooltip.