跳转到内容

Tooltip API

想要学习更多组件属性和 CSS API,可以查阅 React Tooltip 组件的 API 文档。

导入

import Tooltip from '@mui/material/Tooltip';
// 或
import { Tooltip } from '@mui/material';
你可以阅读这个关于最小化捆绑包的指南以了解以上二者的差异。

组件名称

在主题中,名称“MuiTooltip”可用于提供默认属性(props),或者样式覆盖

属性

原生(Native) 组件的属性也是可用的。

名称类型默认值描述
children*element
Tooltip reference element.
⚠️ Needs to be able to hold a ref.
arrowboolfalse
If true, adds an arrow to the tooltip.
classesobject
Override or extend the styles applied to the component. See CSS API below for more details.
components{ Arrow?: elementType, Popper?: elementType, Tooltip?: elementType, Transition?: elementType }{}
The components used for each slot inside the Tooltip. Either a string to use a HTML element or a component.
componentsProps{ arrow?: object, popper?: object, tooltip?: object, transition?: object }{}
The props used for each slot inside the Tooltip. Note that componentsProps.popper prop values win over PopperProps and componentsProps.transition prop values win over TransitionProps if both are applied.
describeChildboolfalse
Set to true if the title acts as an accessible description. By default the title acts as an accessible label for the child.
disableFocusListenerboolfalse
Do not respond to focus-visible events.
disableHoverListenerboolfalse
Do not respond to hover events.
disableInteractiveboolfalse
Makes a tooltip not interactive, i.e. it will close when the user hovers over the tooltip before the leaveDelay is expired.
disableTouchListenerboolfalse
Do not respond to long press touch events.
enterDelaynumber100
The number of milliseconds to wait before showing the tooltip. This prop won't impact the enter touch delay (enterTouchDelay).
enterNextDelaynumber0
The number of milliseconds to wait before showing the tooltip when one was already recently opened.
enterTouchDelaynumber700
The number of milliseconds a user must touch the element before showing the tooltip.
followCursorboolfalse
If true, the tooltip follow the cursor over the wrapped element.
idstring
This prop is used to help implement the accessibility logic. If you don't provide this prop. It falls back to a randomly generated id.
leaveDelaynumber0
The number of milliseconds to wait before hiding the tooltip. This prop won't impact the leave touch delay (leaveTouchDelay).
leaveTouchDelaynumber1500
The number of milliseconds after the user stops touching an element before hiding the tooltip.
onClosefunc
Callback fired when the component requests to be closed.

Signature:
function(event: object) => void
event: The event source of the callback.
onOpenfunc
Callback fired when the component requests to be open.

Signature:
function(event: object) => void
event: The event source of the callback.
openboolfalse
Control the popup` open state.
placement'bottom-end'
| 'bottom-start'
| 'bottom'
| 'left-end'
| 'left-start'
| 'left'
| 'right-end'
| 'right-start'
| 'right'
| 'top-end'
| 'top-start'
| 'top'
'bottom'
Tooltip placement.
PopperComponentelementTypePopper
The component used for the popper.
PopperPropsobject{}
Props applied to the Popper element.
sxArray<func
| object
| bool>
| func
| object
The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details.
titlenode
Tooltip title. Zero-length titles string, undefined, null and false are never displayed.
TransitionComponentelementTypeGrow
The component used for the transition. Follow this guide to learn more about the requirements for this component.
TransitionPropsobject
Props applied to the transition element. By default, the element is based on this Transition component.

ref 则会被传递到根元素中。

CSS

规则名称全局类名描述
popper.MuiTooltip-popperStyles applied to the Popper component.
popperInteractive.MuiTooltip-popperInteractiveStyles applied to the Popper component unless disableInteractive={true}.
popperArrow.MuiTooltip-popperArrowStyles applied to the Popper component if arrow={true}.
popperClose.MuiTooltip-popperCloseStyles applied to the Popper component unless the tooltip is open.
tooltip.MuiTooltip-tooltipStyles applied to the tooltip (label wrapper) element.
tooltipArrow.MuiTooltip-tooltipArrowStyles applied to the tooltip (label wrapper) element if arrow={true}.
arrow.MuiTooltip-arrowStyles applied to the arrow element.
touch.MuiTooltip-touchStyles applied to the tooltip (label wrapper) element if the tooltip is opened by touch.
tooltipPlacementLeft.MuiTooltip-tooltipPlacementLeftStyles applied to the tooltip (label wrapper) element if placement contains "left".
tooltipPlacementRight.MuiTooltip-tooltipPlacementRightStyles applied to the tooltip (label wrapper) element if placement contains "right".
tooltipPlacementTop.MuiTooltip-tooltipPlacementTopStyles applied to the tooltip (label wrapper) element if placement contains "top".
tooltipPlacementBottom.MuiTooltip-tooltipPlacementBottomStyles applied to the tooltip (label wrapper) element if placement contains "bottom".

您可以使用组件自定义选项对组件进行个性化:

演示项目