Trigger 触发器
Trigger 是底层浮层 primitive,主要给 x-next 内部组件和高级封装使用。普通业务展示浮动卡片时,请优先使用 Popover 气泡卡片。
Trigger 负责处理触发事件、显隐状态、浮层定位、Teleport、箭头、滚动更新和外部点击关闭。它不提供业务语义,也不提供卡片视觉规范。
何时使用
- 你正在封装新的浮层组件,例如 Tooltip、Dropdown、Popover、Popconfirm、SelectDropdown。
- 你需要完全接管浮层内容结构和样式,不希望使用 Popover 的卡片语义。
- 你需要直接控制底层定位行为,例如
alignPoint、popupContainer、updateAtScroll、scrollToClose。
不推荐使用
- 展示普通说明卡片:使用
Popover。 - 展示纯文字提示:使用
Tooltip。 - 展示操作菜单:使用
Dropdown。 - 展示二次确认:使用
Popconfirm。
与 Popover 的区别
| 维度 | Popover | Trigger |
|---|---|---|
| 用户心智 | 气泡卡片组件 | 底层触发定位 primitive |
| 是否主推给业务用户 | 是 | 否 |
| 是否有默认卡片样式 | 有 | 无 |
是否提供 title / content 语义 | 有 | 无 |
| 主要用途 | 展示富内容浮层 | 封装 Tooltip、Dropdown、Popover 等组件 |
基础封装示例
下面示例展示 Trigger 作为底层能力的使用方式。业务代码通常不需要直接这样写。
vue
<template>
<x-trigger trigger="click" show-arrow>
<x-button>底层触发器</x-button>
<template #content>
<div class="custom-popup">完全自定义的浮层内容</div>
</template>
</x-trigger>
</template>按需导入
ts
import { Trigger } from 'x-next';Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
popupVisible | 弹出框是否可见,支持 v-model:popup-visible | boolean | undefined |
defaultPopupVisible | 默认是否可见,非受控模式 | boolean | false |
trigger | 触发方式 | 'hover' | 'click' | 'focus' | 'contextMenu' | array | 'hover' |
position | 弹出位置 | 'top' | 'tl' | 'tr' | 'bottom' | 'bl' | 'br' | 'left' | 'lt' | 'lb' | 'right' | 'rt' | 'rb' | 'bottom' |
disabled | 是否禁用 | boolean | false |
popupOffset | 弹出框偏移 | number | 0 |
popupTranslate | 弹出框移动距离 | [number, number] | object | - |
showArrow | 是否显示箭头 | boolean | false |
alignPoint | 是否跟随鼠标位置,常用于右键菜单 | boolean | false |
popupHoverStay | hover 触发时移入浮层是否保持显示 | boolean | true |
blurToClose | focus 触发时失焦是否关闭 | boolean | true |
clickToClose | 点击触发器时是否允许关闭 | boolean | true |
clickOutsideToClose | 点击外部是否关闭 | boolean | true |
unmountOnClose | 关闭时是否卸载弹出框 | boolean | true |
contentClass | 弹出内容类名 | string | array | object | - |
contentStyle | 弹出内容样式 | CSSProperties | - |
arrowClass | 箭头类名 | string | array | object | - |
arrowStyle | 箭头样式 | CSSProperties | - |
popupStyle | 弹出框定位节点样式 | CSSProperties | - |
animationName | 弹出动画名称 | string | 'x-fade-in' |
duration | 动画持续时间 | number | { enter: number; leave: number } | - |
mouseEnterDelay | mouseenter 延迟,单位毫秒 | number | 100 |
mouseLeaveDelay | mouseleave 延迟,单位毫秒 | number | 100 |
focusDelay | focus 延迟,单位毫秒 | number | 0 |
autoFitPopupWidth | 是否将弹出框宽度设为触发器宽度 | boolean | false |
autoFitPopupMinWidth | 是否将弹出框最小宽度设为触发器宽度 | boolean | false |
autoFixPosition | 触发器尺寸变化时是否重新定位 | boolean | true |
popupContainer | 弹出框挂载容器 | string | HTMLElement | - |
updateAtScroll | 容器滚动时是否更新浮层位置 | boolean | false |
autoFitTransformOrigin | 是否自动调整 transform-origin | boolean | false |
hideEmpty | 内容为空时是否隐藏 | boolean | false |
openedClass | 打开时给触发元素添加的类名 | string | array | object | - |
autoFitPosition | 是否自动调整位置适配视口 | boolean | true |
renderToBody | 是否挂载到 body | boolean | true |
preventFocus | 点击弹出层元素时是否阻止获取焦点 | boolean | false |
scrollToClose | 滚动时是否关闭 | boolean | false |
scrollToCloseDistance | 滚动关闭阈值 | number | 0 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
update:popupVisible | 弹出框显隐变化时触发 | visible: boolean |
popup-visible-change | 弹出框显隐变化时触发 | visible: boolean |
show | 显示动画结束后触发 | - |
hide | 隐藏动画结束后触发 | - |
resize | 内部尺寸变化时触发 | - |
Slots
| 插槽名 | 说明 |
|---|---|
default | 触发元素 |
content | 弹出内容 |