Files
heroui-vue/apps/docs/components/popover.md
T

180 lines
8.1 KiB
Markdown
Raw 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: Popover 弹出框
description: 在由按钮或任意自定义元素触发后,于 portal 中展示丰富内容。
component: popover
group: 组件
---
> 基于固定 HeroUI 3.2.6(ce90ea3)迁移。命名组件、v-model、作用域插槽与 DOM attrs 为 Vue 适配;预览、复制与高亮来自同一份 SFC。
## 用法
```ts
import { Popover } from "@heroui-vue/components";
```
<ComponentPreview name="popover-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
Popover,
PopoverTrigger,
PopoverContent,
PopoverArrow,
PopoverDialog,
PopoverHeading,
Button,
} from "@heroui-vue/components";
</script>
<template>
<Popover>
<PopoverTrigger as-child><Button>点击我</Button></PopoverTrigger>
<PopoverContent>
<PopoverArrow />
<PopoverDialog>
<PopoverHeading>标题</PopoverHeading>
内容
</PopoverDialog>
</PopoverContent>
</Popover>
</template>
```
## 示例
### 带箭头
<ComponentPreview name="popover-with-arrow" />
### 可交互内容
<ComponentPreview name="popover-interactive" />
### 位置
<ComponentPreview name="popover-placement" />
### 渲染函数
<ComponentPreview name="popover-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="popover-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-popover {
@apply rounded-xl shadow-2xl;
}
.hv-popover__dialog {
@apply p-4;
}
.hv-popover__heading {
@apply text-lg font-bold;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Popover 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/popover.css)):
#### 基础类
- `.hv-popover` - Popover 根容器样式
- `.hv-popover__dialog` - 对话框内容包裹层
- `.hv-popover__heading` - 标题文本样式
- `.hv-popover__trigger` - 触发元素样式
### 交互状态
组件支持以下动画相关状态:
- **进入**:`[data-entering]` — Popover 出现过程中应用
- **离开**:`[data-exiting]` — Popover 消失过程中应用
- **位置**:`[data-side="*"]` — 根据 Popover 位置应用
- **焦点**:`:focus-visible` 或 `[data-focus-visible="true"]`
## API 参考
### Popover
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------------------------- | ------- | ----------------------------- |
| `默认插槽` | `VNode / 插槽` | - | 触发器与内容元素 |
| `isOpen` | `boolean` | - | 控制 Popover 是否可见(受控) |
| `defaultOpen` | `boolean` | `false` | 初始打开状态(非受控) |
| `@open-change` | `(isOpen: boolean) => void` | - | 打开状态变化时调用 |
### PopoverContent
| Prop | 类型 | 默认值 | 描述 |
| ------------ | ------------------------------------------------- | ---------- | ------------------------------------------ |
| `默认插槽` | `VNode / 插槽` | - | 在 Popover 中展示的内容 |
| `placement` | `"top" \| "bottom" \| "left" \| "right"` (及变体) | `"bottom"` | Popover 的位置 |
| `offset` | `number` | `8` | 与触发元素的距离 |
| `shouldFlip` | `boolean` | `true` | 是否允许 Popover 改变方向以适配空间 |
| `class` | `string / object / array` | - | 额外的 CSS 类名 |
| `as` | `string` | `"div"` | Vue 自定义根元素;自定义箭头使用默认插槽。 |
### PopoverDialog
| Prop | 类型 | 默认值 | 描述 |
| ---------- | ------------------------- | ------ | --------------- |
| `默认插槽` | `VNode / 插槽` | - | 对话框内容 |
| `class` | `string / object / array` | - | 额外的 CSS 类名 |
### PopoverTrigger
| Prop | 类型 | 默认值 | 描述 |
| ---------- | ------------------------- | ------ | ------------------- |
| `默认插槽` | `VNode / 插槽` | - | 触发 Popover 的元素 |
| `class` | `string / object / array` | - | 额外的 CSS 类名 |
### PopoverArrow
| 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 监听落在实际触发元素,disabled、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 协调卸载。
PopoverContent 的 isNonModal 默认 false,也可在 Popover Root 设置默认值;交互丰富的内容使用 PopoverDialog,Heading 关联实际对话框名称,不生成两个嵌套 role=dialog。Content 的 isDismissable 默认 true、isKeyboardDismissDisabled 默认 false;进入焦点、外部交互和 Escape 事件可取消。默认模态模式下 closeAutoFocus 继承 Reka 的触发器恢复流程,当前用于通知;自定义取消恢复可用 isNonModal 模式,二者的焦点策略差异见迁移记录。PopoverDialog 的作用域插槽暴露 close。
### PopoverHeading 与内容语义
| 部件 | 属性 / 插槽 | 默认值 | 说明 |
| -------------- | ------------------------------------- | ---------------- | ------------------------------------------------------------------- |
| PopoverHeading | as / id / class / 默认插槽 | h2 / 自动唯一 ID | 动态维护对话框标题引用 |
| PopoverDialog | as / class / 默认作用域插槽 { close } | div | 视觉内容层;ARIA 名称/描述透传到实际对话框表面,避免两个嵌套 dialog |
| PopoverContent | isNonModal | false | 非模态时不锁定外部交互,不限制焦点范围 |
## 相关组件
[Modal 模态框](./modal) · [Popover 弹出框](./popover) · [Tooltip 工具提示](./tooltip)