Tooltip
Provide a floating label or hint that appears on hover/focus.
Basic example
Wrap an element in a tooltip. Hovering or focusing the element will reveal the tooltip. Use Tooltips to indicate what action clicking an element will perform, or to show additional information about something.
Editable example
Best practices
- Avoid rendering any interactive elements within a Tooltip. Tooltips are primarily for text/images and are finnicky to interact with if they contain buttons, links, or controls. Consider using a if you need to contextually disclose interactive controls.
- Be clear and descriptive, but to the point. Avoid unnecessary words and try to stick to 1—3 for small Tooltips.
- If an action is clear enough, a Tooltip isn't really necessary. For example, an
x
icon in a modal dialog is ubiquitously understood to mean close, so doesn't require a Tooltip.
Position
Set a preferred position for the Tooltip. If there's not enough space in the viewport it'll automatically flip to the opposite side. The default position is TooltipPosition.Top
.
Editable example
Size
For basic 1—3 word hints, use the default size. For tooltips containing a sentence or paragraph, use TooltipSize.Large
.
Editable example
Content
For simple use cases string content will do it, but sometimes Tooltips need to render different styled text or images. Just pass in any React nodes into the content prop and it'll render it. Just be careful not to render any interactive elements — Tooltips aren't designed to contain them.
Editable example
Wrap target
Sometimes the component wrapped by the Tooltip doesn't accept all of the props Tooltip needs to pass to its children. Use the wrapTarget
prop to move all of the tooltip events and attributes from the target to the Tooltip's wrapping div.
Wrapped target
Editable example
Controlled
Tooltips can be manually shown/hidden using the show
prop.
Editable example
Conditionally Controlled Tooltips
For advanced use cases where we sometimes want to use a controlled Tooltip, we can pass a condition to the show
prop like so:
show={isFoo ? undefined : false}
For example, a scenario where we want to use the built in hover behaviours to show/hide a Tooltip when the Tooltip has content, while hiding the Tooltip when there is no content. In this case, we can use both the content
prop and the show
prop together. This will use the default hover behaviour if there is content, and won't show the tooltip at all if there is no content.
Editable example
We could also use this pattern for combining a Tooltip with components that don't have their own disabled state:
Editable example
Accessibility
- Only use
wrapTarget
if the target element is not interactive. Using this prop with an interactive target will cause the wrapper to take focus first, announcing the tooltip, then require a second tab to reach the interactive element within, which is a clunky user experience. It's better to update your interactive component to accept all of the propsTooltip
passes to it. - Tooltips announce their content when the target is focused using a screen reader. If your tooltip only provides supplementary text and does not directly describe the target, make sure the target contains text or an
aria-label
directly describing itself.