Popover 气泡卡片
基础用法
与Tooltip相似,Popover也是基于ElPopper
的构建。 因此对于重复属性,请参考 Tooltip 的文档,在此文档中不做详尽解释。
trigger
属性被用来决定 popover 的触发方式,支持的触发方式: hover
、click
、focus
或 contextmenu
。 如果你想手动控制它,你可以设置 v-model:visible
。
<template>
<el-popover
placement="top-start"
title="Title"
:width="200"
trigger="hover"
content="this is content, this is content, this is content"
>
<template #reference>
<el-button>Hover to activate</el-button>
</template>
</el-popover>
<el-popover
placement="bottom"
title="Title"
:width="200"
trigger="click"
content="this is content, this is content, this is content"
>
<template #reference>
<el-button>Click to activate</el-button>
</template>
</el-popover>
<el-popover
ref="popover"
placement="right"
title="Title"
:width="200"
trigger="focus"
content="this is content, this is content, this is content"
>
<template #reference>
<el-button>Focus to activate</el-button>
</template>
</el-popover>
<el-popover
ref="popover"
title="Title"
:width="200"
trigger="contextmenu"
content="this is content, this is content, this is content"
>
<template #reference>
<el-button>contextmenu to activate</el-button>
</template>
</el-popover>
<el-popover
:visible="visible"
placement="bottom"
title="Title"
:width="200"
content="this is content, this is content, this is content"
>
<template #reference>
<el-button @click="visible = !visible">Manual to activate</el-button>
</template>
</el-popover>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const visible = ref(false)
</script>
虚拟触发
像 Tooltip一样,Popover 可以由虚拟元素触发,这个功能就很适合使用在触发元素和展示内容元素是分开的场景。通常我们使用 #reference
来放置我们的触发元素, 用 triggering-element
API,您可以任意设置您的触发元素 但注意到触发元素应该是接受 mouse
和 keyboard
事件的元素。
WARNING
v-popover
将被废弃,请使用 virtual-ref
作为替代。
<template>
<el-button ref="buttonRef" v-click-outside="onClickOutside"
>Click me</el-button
>
<el-popover
ref="popoverRef"
:virtual-ref="buttonRef"
trigger="click"
title="With title"
virtual-triggering
>
<span> Some content </span>
</el-popover>
</template>
<script setup lang="ts">
import { ref, unref } from 'vue'
import { ClickOutside as vClickOutside } from 'element-plus'
const buttonRef = ref()
const popoverRef = ref()
const onClickOutside = () => {
unref(popoverRef).popperRef?.delayHide?.()
}
</script>
内容可扩展
可以在 Popover 中嵌套其它组件, 以下为嵌套表格的例子。
利用插槽取代 content
属性
<template>
<div style="display: flex; align-items: center">
<el-popover placement="right" :width="400" trigger="click">
<template #reference>
<el-button style="margin-right: 16px">Click to activate</el-button>
</template>
<el-table :data="gridData">
<el-table-column width="150" property="date" label="date" />
<el-table-column width="100" property="name" label="name" />
<el-table-column width="300" property="address" label="address" />
</el-table>
</el-popover>
<el-popover
:width="300"
popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 20px;"
>
<template #reference>
<el-avatar src="https://avatars.githubusercontent.com/u/72015883?v=4" />
</template>
<template #default>
<div
class="demo-rich-conent"
style="display: flex; gap: 16px; flex-direction: column"
>
<el-avatar
:size="60"
src="https://avatars.githubusercontent.com/u/72015883?v=4"
style="margin-bottom: 8px"
/>
<div>
<p
class="demo-rich-content__name"
style="margin: 0; font-weight: 500"
>
Element Plus
</p>
<p
class="demo-rich-content__mention"
style="margin: 0; font-size: 14px; color: var(--el-color-info)"
>
@element-plus
</p>
</div>
<p class="demo-rich-content__desc" style="margin: 0">
Element Plus, a Vue 3 based component library for developers,
designers and product managers
</p>
</div>
</template>
</el-popover>
</div>
</template>
<script lang="ts" setup>
const gridData = [
{
date: '2016-05-02',
name: 'Jack',
address: 'New York City',
},
{
date: '2016-05-04',
name: 'Jack',
address: 'New York City',
},
{
date: '2016-05-01',
name: 'Jack',
address: 'New York City',
},
{
date: '2016-05-03',
name: 'Jack',
address: 'New York City',
},
]
</script>
嵌套操作
当然,你还可以嵌套操作, 它比使用dialog更加轻量。
<template>
<el-popover v-model:visible="visible" placement="top" :width="160">
<p>Are you sure to delete this?</p>
<div style="text-align: right; margin: 0">
<el-button size="small" text @click="visible = false">cancel</el-button>
<el-button size="small" type="primary" @click="visible = false"
>confirm</el-button
>
</div>
<template #reference>
<el-button @click="visible = true">Delete</el-button>
</template>
</el-popover>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const visible = ref(false)
</script>
指令
您可以使用指令性方式弹出窗口,但这种方法不再推荐 ,因为这使得应用程序变得复杂, 您可以参考虚拟触发来实现一样的效果。
<template>
<el-button v-popover="popoverRef" v-click-outside="onClickOutside"
>Click me</el-button
>
<el-popover
ref="popoverRef"
trigger="click"
title="With title"
virtual-triggering
persistent
>
<span> Some content </span>
</el-popover>
</template>
<script setup lang="ts">
import { ref, unref } from 'vue'
import { ClickOutside as vClickOutside } from 'element-plus'
const buttonRef = ref()
const popoverRef = ref()
const onClickOutside = () => {
unref(popoverRef).popperRef?.delayHide?.()
}
</script>
Popover 属性
属性 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
trigger | 触发方式 | string | click/focus/hover/contextmenu | click |
title | 标题 | string | — | — |
effect | Tooltip 主题,Element Plus 内置了 dark / light 两种主题 | string | string | light |
content | 显示的内容,也可以通过写入默认 slot 修改显示内容 | string | — | — |
width | 宽度 | string / number | — | 最小宽度 150px |
placement | 出现位置 | string | top/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-end | bottom |
disabled | Popover 是否可用 | boolean | — | false |
visible / v-model:visible | Popover 是否显示 | Boolean | — | false |
offset | 出现位置的偏移量 | number | — | 0 |
transition | 定义渐变动画 | string | — | el-fade-in-linear |
show-arrow | 是否显示 Tooltip 箭头, 欲了解更多信息,请参考 ElPopper | boolean | — | true |
popper-options | popper.js 的参数 | object | 详情参考 popper.js | { boundariesElement: ‘body’, gpuAcceleration: false } |
popper-class | 为 popper 添加类名 | string | — | — |
show-after | 延迟出现,单位毫秒 | number | — | 0 |
hide-after | 延迟关闭,单位毫秒 | number | — | 200 |
auto-close | Tooltip 出现后自动隐藏延时,单位毫秒,为 0 则不会自动隐藏 | number | — | 0 |
tabindex | Popover 组件的 tabindex | number | — | — |
teleported | 是否将 popover 的下拉列表插入至 body 元素 | boolean | true / false | true |
persistent | 当 popover 组件长时间不触发且 persistent 属性设置为 false 时, popover 将会被删除 | boolean | — | true |
Popover 插槽
插槽名 | 说明 |
---|---|
— | Popover 内嵌 HTML 文本 |
reference | 触发 Popover 显示的 HTML 元素 |
Popover 事件
事件名 | 说明 | 回调参数 |
---|---|---|
show | 显示时触发 | — |
before-enter | 显示动画播放前触发 | — |
after-enter | 显示动画播放完毕后触发 | — |
hide | 隐藏时触发 | — |
before-leave | 隐藏动画播放前触发 | — |
after-leave | 隐藏动画播放完毕后触发 | — |