JGuang

工具提示

Tooltip · Material UI

交互预览
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

工具提示

为原生子元素提供简洁的补充标签或说明。

本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。

使用场景与业务边界

Tooltip 不能成为关键无名控件唯一的名称;子元素需要兼容 ref,禁用控件需要适当包装。

原生组成与范围

Tooltip

保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。

原生安装与初始化

npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。

关键原生参数

仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。

所属组件.属性用途说明声明来源
Tooltip.titleTooltip 内容,保留标签/描述语义。Tooltip
Tooltip.describeChild将 Tooltip 内容作为描述,而不是替换子元素的名称。Tooltip
Tooltip.placement原生 Tooltip/Popper 定位枚举,不是任意 CSS 定位。Tooltip
Tooltip.open受控原生可见性,不获取数据或执行操作。Tooltip
Tooltip.enterDelayTooltip 显示延迟,单位毫秒。Tooltip

可运行原生示例

下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。

  • 基础工具提示 · BasicTooltip · 上游原生示例
  • 标签与描述 · AccessibilityTooltips · 上游原生示例
  • 工具提示方向 · PositionedTooltips · 上游原生示例
  • 定制样式 · CustomizedTooltips · 上游原生示例
  • 带箭头的工具提示 · ArrowTooltips · 上游原生示例
  • 锚点间距 · TooltipOffset · 上游原生示例
  • 锚点间距 · TooltipMargin · 上游原生示例
  • 触发方式 · TriggersTooltips · 上游原生示例
  • 受控工具提示 · ControlledTooltips · 上游原生示例
  • 可变宽度 · VariableWidth · 上游原生示例
  • 交互演示 · NonInteractiveTooltips · 上游原生示例
  • 禁用元素 · DisabledTooltips · 上游原生示例
  • 多种过渡动画 · TransitionsTooltips · 上游原生示例
  • 跟随鼠标 · FollowCursorTooltips · 上游原生示例
  • 显示与隐藏 · DelayTooltips · 上游原生示例

尚未支持的额外示例

这些条目仍保留在上游清单中,不静默跳过,也不展示为已完成预览。需要补充依赖/框架验收后再纳入可运行列表。

  • 虚拟元素锚点 · AnchorElTooltips: @popperjs/core

真实使用示例

固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。

import DeleteIcon from '@mui/icons-material/Delete';
import IconButton from '@mui/material/IconButton';
import Tooltip from '@mui/material/Tooltip';

export default function BasicTooltip() {
  return (
    <Tooltip title="Delete">
      <IconButton>
        <DeleteIcon />
      </IconButton>
    </Tooltip>
  );
}

示例依赖边界

@mui/icons-material/Delete, @mui/material/IconButton, @mui/material/Tooltip, @mui/material/Button, @mui/material/Box, @mui/material/Stack, react, @mui/material/styles, @mui/material/Typography, @mui/material/Grid, @mui/material/ClickAwayListener, @mui/material/Fade, @mui/material/Zoom

直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。

固定上游 API 元数据参考

以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。

Tooltip

import Tooltip from '@mui/material/Tooltip';
import { Tooltip } from '@mui/material';
属性必需上游元数据类型/约束元数据默认值
children是element—
arrow否boolfalse
classes否object—
describeChild否boolfalse
disableFocusListener否boolfalse
disableHoverListener否boolfalse
disableInteractive否boolfalse
disableTouchListener否boolfalse
enterDelay否number100
enterNextDelay否number0
enterTouchDelay否number700
followCursor否boolfalse
id否string—
leaveDelay否number0
leaveTouchDelay否number1500
onClose否func—
onOpen否func—
open否bool—
placement否'auto-end' | 'auto-start' | 'auto' | 'bottom-end' | 'bottom-start' | 'bottom' | 'left-end' | 'left-start' | 'left' | 'right-end' | 'right-start' | 'right' | 'top-end' | 'top-start' | 'top''bottom'
slotProps否{ arrow?: func | object, popper?: func | object, tooltip?: func | object, transition?: func | object }{}
slots否{ arrow?: elementType, popper?: elementType, tooltip?: elementType, transition?: elementType }{}
sx否Array<func | object | bool> | func | object—
title否node—

原生源码声明

声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。

vendor/src/Tooltip/Tooltip.d.ts

import * as React from 'react';

import { SxProps } from '@mui/system';

import { PopperProps } from '../Popper';

import { Theme } from '../styles';

import { InternalStandardProps as StandardProps } from '../internal';

import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

import { TransitionProps } from '../transitions/types';

import { TooltipClasses } from './tooltipClasses';

export interface TooltipPopperSlotPropsOverrides {}

export interface TooltipTransitionSlotPropsOverrides {}

export interface TooltipTooltipSlotPropsOverrides {}

export interface TooltipArrowSlotPropsOverrides {}

export interface TooltipSlots {
  /**
   * The component used for the popper.
   * @default Popper
   */
  popper: React.ElementType;
  /**
   * The component used for the transition.
   * [Follow this guide](/material-ui/transitions/#transition-slots) to learn more about the requirements for this component.
   * @default Grow
   */
  transition: React.ElementType;
  /**
   * The component used for the tooltip.
   */
  tooltip: React.ElementType;
  /**
   * The component used for the arrow.
   */
  arrow: React.ElementType;
}

export type TooltipSlotsAndSlotProps = CreateSlotsAndSlotProps<
  TooltipSlots,
  {
    /**
     * Props forwarded to the popper slot.
     * By default, the available props are based on the [Popper](https://mui.com/material-ui/api/popper/#props) component.
     */
    popper: SlotProps<
      React.ElementType<PopperProps>,
      TooltipPopperSlotPropsOverrides,
      TooltipOwnerState
    >;
    /**
     * Props forwarded to the transition slot.
     * By default, the available props are based on the [Grow](https://mui.com/material-ui/api/grow/#props) component.
     */
    transition: SlotProps<
      React.ElementType<TransitionProps>,
      TransitionProps & TooltipTransitionSlotPropsOverrides,
      TooltipOwnerState
    >;
    /**
     * Props forwarded to the tooltip slot.
     * By default, the available props are based on the div element.
     */
    tooltip: SlotProps<'div', TooltipTooltipSlotPropsOverrides, TooltipOwnerState>;
    /**
     * Props forwarded to the tooltip slot.
     * By default, the available props are based on the span element.
     */
    arrow: SlotProps<'span', TooltipArrowSlotPropsOverrides, TooltipOwnerState>;
  }
>;

export interface TooltipProps
  extends StandardProps<React.HTMLAttributes<HTMLDivElement>, 'title'>, TooltipSlotsAndSlotProps {
  /**
   * If `true`, adds an arrow to the tooltip.
   * @default false
   */
  arrow?: boolean | undefined;
  /**
   * Tooltip reference element.
   */
  children: React.ReactElement<unknown, any>;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<TooltipClasses> | undefined;
  /**
   * Set to `true` if the `title` acts as an accessible description.
   * By default the `title` acts as an accessible label for the child.
   * @default false
   */
  describeChild?: boolean | undefined;
  /**
   * Do not respond to focus-visible events.
   * @default false
   */
  disableFocusListener?: boolean | undefined;
  /**
   * Do not respond to hover events.
   * @default false
   */
  disableHoverListener?: boolean | undefined;
  /**
   * Makes a tooltip not interactive, i.e. it will close when the user
   * hovers over the tooltip before the `leaveDelay` is expired.
   * @default false
   */
  disableInteractive?: boolean | undefined;
  /**
   * Do not respond to long press touch events.
   * @default false
   */
  disableTouchListener?: boolean | undefined;
  /**
   * The number of milliseconds to wait before showing the tooltip.
   * This prop won't impact the enter touch delay (`enterTouchDelay`).
   * @default 100
   */
  enterDelay?: number | undefined;
  /**
   * The number of milliseconds to wait before showing the tooltip when one was already recently opened.
   * @default 0
   */
  enterNextDelay?: number | undefined;
  /**
   * The number of milliseconds a user must touch the element before showing the tooltip.
   * @default 700
   */
  enterTouchDelay?: number | undefined;
  /**
   * If `true`, the tooltip follow the cursor over the wrapped element.
   * @default false
   */
  followCursor?: boolean | undefined;
  /**
   * 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.
   */
  id?: string | undefined;
  /**
   * The number of milliseconds to wait before hiding the tooltip.
   * This prop won't impact the leave touch delay (`leaveTouchDelay`).
   * @default 0
   */
  leaveDelay?: number | undefined;
  /**
   * The number of milliseconds after the user stops touching an element before hiding the tooltip.
   * @default 1500
   */
  leaveTouchDelay?: number | undefined;
  /**
   * Callback fired when the component requests to be closed.
   *
   * @param {React.SyntheticEvent} event The event source of the callback.
   */
  onClose?: ((event: React.SyntheticEvent | Event) => void) | undefined;
  /**
   * Callback fired when the component requests to be open.
   *
   * @param {React.SyntheticEvent} event The event source of the callback.
   */
  onOpen?: ((event: React.SyntheticEvent) => void) | undefined;
  /**
   * If `true`, the component is shown.
   */
  open?: boolean | undefined;
  /**
   * Tooltip placement.
   * @default 'bottom'
   */
  placement?: PopperProps['placement'] | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * Tooltip title. Zero-length titles string, undefined, null and false are never displayed.
   */
  title: React.ReactNode;
}

export interface TooltipOwnerState extends TooltipProps {}

export default function Tooltip(props: TooltipProps): React.JSX.Element;

安装后验收

执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。

来源与许可

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。