Files
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

182 lines
8.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Tooltip 工具提示
description: 当用户悬停或聚焦某个元素时,展示提示性文本。
component: tooltip
group: 组件
---
> 基于固定 HeroUI 3.2.6(ce90ea3)迁移。命名组件、v-model、作用域插槽与 DOM attrs 为 Vue 适配;预览、复制与高亮来自同一份 SFC。
## 用法
```ts
import { Tooltip } from "@heroui-vue/components";
```
<ComponentPreview name="tooltip-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
Tooltip,
TooltipTrigger,
TooltipContent,
TooltipArrow,
Button,
} from "@heroui-vue/components";
</script>
<template>
<Tooltip>
<TooltipTrigger as-child><Button>Hover for tooltip</Button></TooltipTrigger>
<TooltipContent show-arrow>
<TooltipArrow />
关于此元素的有用信息
</TooltipContent>
</Tooltip>
</template>
```
## 示例
### 位置
<ComponentPreview name="tooltip-placement" />
### 带箭头
<ComponentPreview name="tooltip-with-arrow" />
### 自定义触发
<ComponentPreview name="tooltip-custom-trigger" />
### 渲染函数
<ComponentPreview name="tooltip-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="tooltip-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-tooltip {
@apply rounded-xl shadow-lg;
}
.hv-tooltip__trigger {
@apply cursor-help;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### 全局延迟配置
你可以通过定义 CSS 变量,为应用中所有 Tooltip 设置默认的显示与隐藏延迟:
```css
/* 在你的全局 CSS 文件中 */
:root {
--tooltip-delay: 1500ms;
--tooltip-close-delay: 500ms;
}
/* 也可以为浅色/深色主题设置不同的值 */
.light,
[data-theme="light"] {
--tooltip-delay: 1200ms;
}
.dark,
[data-theme="dark"] {
--tooltip-close-delay: 300ms;
}
```
值支持 `ms`、`s` 等 CSS 时间单位。在单个 Tooltip 上指定 `delay` 或 `closeDelay` 时,会覆盖这些全局设置。
### CSS 类
Tooltip 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/tooltip.css)):
#### 基础类
- `.hv-tooltip` - 带动画的基础 Tooltip 样式
- `.hv-tooltip__trigger` - 触发元素样式
### 交互状态
组件支持以下动画相关状态:
- **进入**:`[data-entering]` — Tooltip 出现过程中应用
- **离开**:`[data-exiting]` — Tooltip 消失过程中应用
- **位置**:`[data-side="*"]` — 根据 Tooltip 位置应用
## API 参考
### Tooltip
| Prop | 类型 | 默认值 | 描述 |
| --------------------- | -------------------- | ------------------ | ------------------------------------------------------------------------------ |
| `默认插槽` | `VNode / 插槽` | - | 触发元素与内容 |
| `delay` | `number` | `1500` 或 CSS 变量 | 显示 Tooltip 前的延迟(毫秒);可通过 `--tooltip-delay` CSS 变量全局配置 |
| `closeDelay` | `number` | `500` 或 CSS 变量 | 隐藏 Tooltip 前的延迟(毫秒);可通过 `--tooltip-close-delay` CSS 变量全局配置 |
| `trigger` | `"hover" \| "focus"` | `"hover"` | Tooltip 的触发方式 |
| `isDisabled` | `boolean` | `false` | 是否禁用 Tooltip |
| `shouldSkipAnimation` | `boolean` | `false` | 在多个 Tooltip 之间快速切换时,是否跳过进入与退出动画 |
### TooltipContent
| Prop | 类型 | 默认值 | 描述 |
| ----------- | ------------------------------------------------- | ------------------- | ------------------------------------------ |
| `默认插槽` | `VNode / 插槽` | - | 在 Tooltip 中展示的内容 |
| `showArrow` | `boolean` | `false` | 是否显示箭头指示器 |
| `offset` | `number` | `3`(带箭头时为 7) | 与触发元素的距离 |
| `placement` | `"top" \| "bottom" \| "left" \| "right"` (及变体) | `"top"` | Tooltip 的位置 |
| `class` | `string / object / array` | - | 额外的 CSS 类名 |
| `as` | `string` | `"div"` | Vue 自定义根元素;自定义箭头使用默认插槽。 |
### TooltipTrigger
| Prop | 类型 | 默认值 | 描述 |
| ---------- | ------------------------- | ------ | ------------------- |
| `默认插槽` | `VNode / 插槽` | - | 触发 Tooltip 的元素 |
| `class` | `string / object / array` | - | 额外的 CSS 类名 |
### TooltipArrow
| Prop | 类型 | 默认值 | 描述 |
| ---------- | ------------------------- | -------- | -------------------------------- |
| `class` | `string / object / array` | - | 额外的 CSS 类名 |
| `默认插槽` | `单个 SVG VNode` | 基线箭头 | 自定义箭头,保留对应 data-slot。 |
## Vue API 与组合约定
Root 的 `open` / `isOpen` 支持受控值,`defaultOpen` 默认 false;推荐 `v-model:open`,事件为 `update:open`、`openChange`(模板 `@open-change`)。父级拒绝请求时保持父级状态,初始值只初始化一次。第一个 Button 可自动作为触发器;自定义内容用显式 Trigger,单元素组合可设 `as-child`。Trigger 的 class/style、ARIA 与 DOM 监听落在实际触发元素;使用 Tooltip 的 isDisabled 禁用提示。
React 的 DOM render 回调改为 `as` 加原生 attrs,例如 `as="div" data-custom="foo"`;class 函数改为 Vue 的字符串/对象/数组绑定。Enter/Space 对自定义按钮根只激活一次,调用者 preventDefault 可取消。Portal 默认 body,可由 ConfigProvider.teleportTo 或 Content/Backdrop 的 to 覆盖;目标应置于所需主题作用域中。SSR Portal 于客户端挂载后插入,尚未承诺 Nuxt 服务端收集。
### 定位与生命周期
placement 支持 top/bottom/left/right 及对齐变体(如 `top start`、`right bottom`),offset 可为 0,crossOffset 默认 0,shouldFlip 默认 true,containerPadding 默认 12。实际碰撞结果以 `data-side`、`data-align` 表示;箭头由 Reka 定位旋转,不重复应用上游 SVG 旋转。保留源进入/退出动画数值及 data-entering/data-exiting,退出由 Presence 协调卸载。
Tooltip 的 isDisabled 默认 false;trigger="hover" 同时支持键盘焦点,trigger="focus" 只在聚焦时打开。delay / closeDelay 显式值优先于触发器作用域的 CSS 时间变量(支持 ms/s),缺省 1500/500ms;失焦、点击、Escape 与外部交互立即关闭,悬停离开遵循 closeDelay。ConfigProvider 提供共享的 Reka warmup 边界;未使用 Provider 时各 Root 拥有自己的 warmup 计时。shouldSkipAnimation 默认 false,为 true 时允许 Reka instant-open 阶段跳过动画,此处与 React Aria 的全局 warmup 调度不同。
showArrow 选择带箭头的默认偏移(7px,否则 3px),仍需显式放置 TooltipArrow,与基线源码相同。Tooltip 通过 aria-describedby 关联隐藏的只读提示文本;可视容器保持 Reka 的语义,不能在其中放需要焦点的按钮/输入,应改用 Popover。TooltipTrigger 支持 as/asChild 和原生 attrs;普通按钮会自动组合。
## 相关组件
[Button 按钮](./button) · [Popover 弹出框](./popover)