Tooltip
A short label that shows when a control is hovered or focused. Use it to name an icon button and show its shortcut.
Usage
import { Tooltip } from 'nyctoui/tooltip'
<Tooltip content="Copy link">
<Button iconOnly aria-label="Copy link">
<IconLink size={16} />
</Button>
</Tooltip>Give Tooltip its text as content and wrap the control. The child becomes the trigger, so it must take a ref and spread props, as every NyctoUI control does.
A tooltip is a visual label only. It never opens on touch, and screen readers don’t rely on it, so an icon-only trigger needs an aria-label that matches the text. Don’t put anything in a tooltip that people need to read; for that, use a Popover.
Good for
- Naming icon buttons in an editor toolbar, with their shortcuts.
- The full name of a truncated row in a sidebar.
- Exact time on a relative date such as “2h ago” in a feed.
- The name of a collaborator’s avatar in a document header.
Anatomy
<TooltipProvider>
<Tooltip>
<TooltipTrigger />
<TooltipContent>
<TooltipShortcut />
</TooltipContent>
</Tooltip>
</TooltipProvider>The parts are for when the shorthand isn’t enough: a custom delay on the trigger, offsets, or content beyond a label and a shortcut.
Examples
Shortcut
Pass keys as shortcut and they sit after the label, a step quieter.
Sides
Tooltips open above the trigger. Use side where that would cover something, such as along the top edge of a page.
Group
Once one tooltip has opened, its neighbors open at once. A TooltipProvider sets the delay for the tooltips inside it; wrap a toolbar in one, or the whole app.
Composed
The same tooltip built from parts, here opening with no delay and below the trigger.
Best practices
- Keep it to a few words of plain text, the name of the control or a short fact.
- Add a tooltip to every icon-only button, with the same words as its
aria-label. - Show the shortcut with the
shortcutprop, so people can learn it.
- Put links or buttons in a tooltip. Use a popover.
- Put anything people need to read to finish a task in a tooltip. It never opens on touch.
- Use a tooltip to explain why a control is disabled. Write the reason next to it.
API reference
Tooltip
Holds the open state. With content, also renders the trigger and the bubble. Renders no element of its own.
| Prop | Type | Default |
|---|---|---|
contentShorthand for the tooltip’s text. When set, children is the trigger element. | ReactNode | — |
shortcutShorthand: keys shown after content. | ReactNode | — |
sideShorthand: which side of the trigger to open on. flips when it would clip. | 'top' | 'right' | 'bottom' | 'left' | 'top' |
alignShorthand: how to line up with the trigger along that side. | 'start' | 'center' | 'end' | 'center' |
defaultOpenStarts open. For uncontrolled use. | boolean | false |
openWhether the tooltip is open. For controlled use. | boolean | — |
onOpenChangeCalled when the tooltip opens or closes. | (open: boolean, details) => void | — |
disabledStops the tooltip from opening. | boolean | false |
delayMilliseconds of hover before opening. Defaults to the provider’s. | number | 500 |
closeOnPressCloses the tooltip when the trigger is pressed. | boolean | true |
TooltipProvider
Sets the hover delay for the tooltips inside it. Renders no element.
| Prop | Type | Default |
|---|---|---|
delayMilliseconds of hover before a tooltip opens. | number | 500 |
TooltipTrigger
The element the tooltip labels. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
TooltipContent
The bubble. Renders a <div> in the top layer.
| Prop | Type | Default |
|---|---|---|
sideWhich side of the trigger to open on. Flips when it would clip. | 'top' | 'right' | 'bottom' | 'left' | 'top' |
alignHow to line up with the trigger along that side. | 'start' | 'center' | 'end' | 'center' |
sideOffsetGap between the trigger and the bubble, in pixels. | number | 6 |
alignOffsetShift along the alignment axis, in pixels. | number | 0 |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TooltipShortcut
Keys after the label, aligned to the end. Renders a <span>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |