Skip to content

Tooltip 文字提示

用于解释图标、按钮或截断文本。

何时使用

  • 需要在鼠标悬停、键盘聚焦或点击时展示短文本说明。
  • 说明内容较短,不承载复杂结构或操作。
  • 复杂卡片、标题和操作按钮请优先使用 Popover 气泡卡片

基础用法

vue
<template>
  <x-tooltip content="这是一段提示内容">
    <x-button>悬浮查看</x-button>
  </x-tooltip>

  <x-tooltip content="键盘聚焦也可查看" :trigger="['hover', 'focus']">
    <x-button>聚焦查看</x-button>
  </x-tooltip>
</template>

弹出位置

vue
<template>
  <x-tooltip content="上方" position="top">
    <x-button>Top</x-button>
  </x-tooltip>
  <x-tooltip content="右侧" position="right">
    <x-button>Right</x-button>
  </x-tooltip>
</template>

迷你尺寸和颜色

vue
<template>
  <x-tooltip content="1234" mini>
    <x-button>迷你尺寸</x-button>
  </x-tooltip>

  <x-tooltip content="蓝色提示" background-color="#165dff">
    <x-button>蓝色</x-button>
  </x-tooltip>
</template>

禁用和空内容

vue
<template>
  <x-tooltip content="禁用后不会展示" disabled>
    <x-button>禁用提示</x-button>
  </x-tooltip>

  <x-tooltip content="需要管理员权限">
    <x-button disabled>禁用按钮提示</x-button>
  </x-tooltip>

  <x-tooltip>
    <x-button>空内容不展示</x-button>
  </x-tooltip>

  <x-tooltip :content="0">
    <x-button>数字 0</x-button>
  </x-tooltip>
</template>

受控显隐和事件

vue
<script setup lang="ts">
import { ref } from 'vue';

const visible = ref(false);
</script>

<template>
  <x-button @click="visible = !visible">切换显隐</x-button>
  <x-tooltip
    v-model:popup-visible="visible"
    trigger="click"
    content="受控 Tooltip"
    @popup-visible-change="(nextVisible) => console.log(nextVisible)"
  >
    <x-button>点击触发</x-button>
  </x-tooltip>
</template>

自定义内容

vue
<template>
  <x-tooltip>
    <x-button>查看状态</x-button>
    <template #content>
      <div>
        <strong>构建完成</strong>
        <span>耗时 12.4s,产物已更新。</span>
      </div>
    </template>
  </x-tooltip>
</template>

更多触发能力

vue
<script setup lang="ts">
import { ref } from 'vue';

const visible = ref(false);
</script>

<template>
  <x-tooltip content="没有箭头" :show-arrow="false">
    <x-button>隐藏箭头</x-button>
  </x-tooltip>

  <x-tooltip v-model:popup-visible="visible" trigger="contextMenu" align-point content="跟随右键位置">
    <x-button>右键触发</x-button>
  </x-tooltip>
</template>

按需导入

ts
import { Tooltip } from 'x-next';

导出项

名称说明
Tooltip主组件

Props

参数说明类型默认值
popupVisible气泡是否可见,受控模式,支持 v-model:popup-visibleboolean-
defaultPopupVisible默认是否可见,非受控模式booleanfalse
trigger触发方式'hover' | 'click' | 'focus' | 'contextMenu' | Array'hover'
disabled是否禁用 Tooltipbooleanfalse
content提示内容string | number-
position弹出位置'top' | 'tl' | 'tr' | 'bottom' | 'bl' | 'br' | 'left' | 'lt' | 'lb' | 'right' | 'rt' | 'rb''top'
mini是否展示为迷你尺寸booleanfalse
backgroundColor气泡与箭头背景色string-
contentClass气泡内容类名string | any[] | Record<string, any>-
contentStyle气泡内容样式CSSProperties-
arrowClass箭头类名string | any[] | Record<string, any>-
arrowStyle箭头样式CSSProperties-
popupContainer弹层挂载容器string | HTMLElement-
popupOffset弹层偏移距离number10
popupTranslate弹层移动距离[number, number] | object-
showArrow是否展示箭头booleantrue
alignPoint是否跟随鼠标位置,常用于右键菜单booleanfalse
popupStyle弹层定位节点样式CSSProperties-
animationName弹出动画名称string'x-zoom-in-fade-out'
duration动画持续时间number | { enter: number; leave: number }-
mouseEnterDelay鼠标移入延迟,单位毫秒number100
mouseLeaveDelay鼠标移出延迟,单位毫秒number100
focusDelay聚焦延迟,单位毫秒number0
popupHoverStayhover 触发时移入浮层是否保持显示booleantrue
blurToClosefocus 触发时失焦是否关闭booleantrue
clickToClose点击触发器时是否允许关闭booleantrue
clickOutsideToClose点击外部是否关闭booleantrue
unmountOnClose关闭时是否卸载弹层节点booleantrue
autoFitPopupWidth是否将弹层宽度设为触发器宽度booleanfalse
autoFitPopupMinWidth是否将弹层最小宽度设为触发器宽度booleanfalse
autoFixPosition触发器尺寸变化时是否重新定位booleantrue
updateAtScroll容器滚动时是否更新弹层位置booleanfalse
autoFitPosition是否自动调整位置适配视口booleantrue
renderToBody是否挂载到 bodybooleantrue
preventFocus点击弹层元素时是否阻止获取焦点booleanfalse
scrollToClose滚动时是否关闭booleanfalse
scrollToCloseDistance滚动关闭阈值number0
openedClass打开时给触发元素添加的类名string | any[] | Record<string, any>-

Events

事件名说明回调参数
update:popupVisible显隐状态变化时触发,用于 v-model:popup-visiblevisible: boolean
popup-visible-change显隐状态变化时触发visible: boolean
show显示动画结束后触发-
hide隐藏动画结束后触发-
resize内部尺寸变化时触发-

Slots

插槽名说明
default触发提示的元素
content自定义提示内容,优先级高于 content prop