Tooltips display informative text when users hover over, focus on, or tap an element.
When activated, Tooltips display a text label identifying an element, such as a description of its function.
{{ādemoā: āBasicTooltip.jsā}}
The Tooltip
has 12 placement choices.
They donāt have directional arrows; instead, they rely on motion emanating from the source to convey direction.
{{ādemoā: āPositionedTooltips.jsā}}
Here are some examples of customizing the component. You can learn more about this in the overrides documentation page.
{{ādemoā: āCustomizedTooltips.jsā}}
You can use the arrow
prop to give your tooltip an arrow indicating which element it refers to.
{{ādemoā: āArrowTooltips.jsā}}
To adjust the distance between the tooltip and its anchor, you can use the slotProps
prop to modify the offset of the popper.
{{ādemoā: āTooltipOffset.jsā}}
Alternatively, you can use the slotProps
prop to customize the margin of the popper.
{{ādemoā: āTooltipMargin.jsā}}
The tooltip needs to apply DOM event listeners to its child element. If the child is a custom React element, you need to make sure that it spreads its props to the underlying DOM element.
const MyComponent = React.forwardRef(function MyComponent(props, ref) {
// Spread the props to the underlying DOM element.
return (
<div {...props} ref={ref}>
Bin
</div>
);
});
// ...
<Tooltip title="Delete">
<MyComponent />
</Tooltip>;
If using a class component as a child, youāll also need to ensure that the ref is forwarded to the underlying DOM element. (A ref to the class component itself will not work.)
class MyComponent extends React.Component {
render() {
const { innerRef, ...props } = this.props;
// Spread the props to the underlying DOM element.
return (
<div {...props} ref={innerRef}>
Bin
</div>
);
}
}
// Wrap MyComponent to forward the ref as expected by Tooltip
const WrappedMyComponent = React.forwardRef(function WrappedMyComponent(props, ref) {
return <MyComponent {...props} innerRef={ref} />;
});
// ...
<Tooltip title="Delete">
<WrappedMyComponent />
</Tooltip>;
You can define the types of events that cause a tooltip to show.
The touch action requires a long press due to the enterTouchDelay
prop being set to 700
ms by default.
{{ādemoā: āTriggersTooltips.jsā}}
You can use the open
, onOpen
and onClose
props to control the behavior of the tooltip.
{{ādemoā: āControlledTooltips.jsā}}
The Tooltip
wraps long text by default to make it readable.
{{ādemoā: āVariableWidth.jsā}}
Tooltips are interactive by default (to pass WCAG 2.1 success criterion 1.4.13).
It wonāt close when the user hovers over the tooltip before the leaveDelay
is expired.
You can disable this behavior (thus failing the success criterion which is required to reach level AA) by passing disableInteractive
.
{{ādemoā: āNonInteractiveTooltips.jsā}}
By default disabled elements like <button>
do not trigger user interactions so a Tooltip
will not activate on normal events like hover. To accommodate disabled elements, add a simple wrapper element, such as a span
.
In order to work with Safari, you need at least one display block or flex item below the tooltip wrapper.
{{ādemoā: āDisabledTooltips.jsā}}
If youāre not wrapping a MaterialĀ UI component that inherits from
ButtonBase
, for instance, a native<button>
element, you should also add the CSS property pointer-events: none; to your element when disabled:
<Tooltip title="You don't have permission to do this">
<span>
<button disabled={disabled} style={disabled ? { pointerEvents: 'none' } : {}}>
A disabled button
</button>
</span>
</Tooltip>
Use a different transition.
{{ādemoā: āTransitionsTooltips.jsā}}
You can enable the tooltip to follow the cursor by setting followCursor={true}
.
{{ādemoā: āFollowCursorTooltips.jsā}}
In the event you need to implement a custom placement, you can use the anchorEl
prop:
The value of the anchorEl
prop can be a reference to a fake DOM element.
You need to create an object shaped like the VirtualElement
.
{{ādemoā: āAnchorElTooltips.jsā}}
The tooltip is normally shown immediately when the userās mouse hovers over the element, and hides immediately when the userās mouse leaves. A delay in showing or hiding the tooltip can be added through the enterDelay
and leaveDelay
props.
On mobile, the tooltip is displayed when the user longpresses the element and hides after a delay of 1500ms. You can disable this feature with the disableTouchListener
prop.
{{ādemoā: āDelayTooltips.jsā}}
(WAI-ARIA: https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/)
By default, the tooltip only labels its child element.
This is notably different from title
which can either label or describe its child depending on whether the child already has a label.
For example, in:
<button title="some more information">A button</button>
the title
acts as an accessible description.
If you want the tooltip to act as an accessible description you can pass describeChild
.
Note that you shouldnāt use describeChild
if the tooltip provides the only visual label. Otherwise, the child would have no accessible name and the tooltip would violate success criterion 2.5.3 in WCAG 2.1.
{{ādemoā: āAccessibilityTooltips.jsā}}