Popover弹出框
用于在页面上显示额外信息或操作
基础用法
通过
trigger插槽和content插槽来定义触发器和弹出内容
vue
<template>
<div class="w-full flex justify-center">
<Popover position="bottom" trigger="hover" :z-index="100">
<template #trigger>
<button class="btn btn-primary">trigger</button>
</template>
<template #content>
<Card />
</template>
</Popover>
</div>
</template>
<script setup lang="ts">
import { Popover } from 'li-daisy'
import Card from '../demo/Card.vue'
</script>设置trigger
可选值有
hover和click,默认为hover
vue
<template>
<div class="w-full flex justify-between">
<Popover trigger="hover" :z-index="100">
<template #trigger>
<button class="btn btn-primary">hover</button>
</template>
<template #content>
<Card />
</template>
</Popover>
<Popover trigger="click" :z-index="100">
<template #trigger>
<button class="btn btn-primary">click</button>
</template>
<template #content>
<Card />
</template>
</Popover>
</div>
</template>
<script setup lang="ts">
import { Popover } from 'li-daisy'
import Card from '../demo/Card.vue'
</script>设置位置
可选值有
top,top-start,top-end,bottom,bottom-start,bottom-end,left,left-start,left-end,right,right-start,right-end,默认为bottom
智能定位
为了保证 content 插槽的内容尽量全部可见,Popover 组件会根据视口边界自动调整位置:
- 优先使用指定位置:首先尝试在设定的 position 位置显示
- 自动避让边界:当内容可能被视口边缘裁剪时,会自动切换到相反或相邻的位置
- 保持对齐关系:调整后仍会尽量保持与触发器的对齐关系(如
top-start可能调整为bottom-start) - 智能回退机制:如果所有预设位置都无法完全显示,会选择显示区域最大的位置
- 唤起后不变性:当弹出框显示后,位置会保持固定,不会因为滚动或窗口大小变化而重新计算位置,直到下次重新唤起时才会根据新的环境重新定位
例如:设置 position="top" 但上方空间不足时,会自动调整为 bottom 位置显示
下述示例中可以尝试滚动屏幕让
top按钮处于当前屏幕较高位置,此时再去点击,悬浮框内容位置将变为bottom
top-start
top
top-end
left-start
left
left-end
Popover 位置示例
right-start
right
right-end
bottom-start
bottom
bottom-end
vue
<template>
<div class="flex flex-col items-center gap-y-6 p-8 min-w-md">
<!-- 顶部位置组 -->
<div class="flex items-center gap-x-4">
<Popover
v-for="position in topPositions"
:key="position"
:position="position"
trigger="click"
>
<template #trigger>
<div class="btn btn-sm min-w-24">{{ position }}</div>
</template>
<template #content>
<Card />
</template>
</Popover>
</div>
<!-- 中间位置组 -->
<div class="flex items-center justify-between w-full">
<!-- 左侧组 -->
<div class="flex flex-col gap-y-3">
<Popover
v-for="position in leftPositions"
:key="position"
:position="position"
trigger="click"
:z-index="999"
>
<template #trigger>
<div class="btn btn-sm min-w-24">{{ position }}</div>
</template>
<template #content>
<Card />
</template>
</Popover>
</div>
<!-- 中心说明 -->
<div class="flex items-center justify-center">
<div class="badge badge-outline">Popover 位置示例</div>
</div>
<!-- 右侧组 -->
<div class="flex flex-col gap-y-3">
<Popover
v-for="position in rightPositions"
:key="position"
:position="position"
trigger="click"
>
<template #trigger>
<div class="btn btn-sm min-w-24">{{ position }}</div>
</template>
<template #content>
<Card />
</template>
</Popover>
</div>
</div>
<!-- 底部位置组 -->
<div class="flex items-center gap-x-4">
<Popover
v-for="position in bottomPositions"
:key="position"
:position="position"
trigger="click"
>
<template #trigger>
<div class="btn btn-sm min-w-24">{{ position }}</div>
</template>
<template #content>
<Card />
</template>
</Popover>
</div>
</div>
</template>
<script setup lang="ts">
import { Popover, type PopoverPositon } from 'li-daisy'
import Card from '../demo/Card.vue'
const topPositions: Array<PopoverPositon> = ['top-start', 'top', 'top-end']
const leftPositions: Array<PopoverPositon> = ['left-start', 'left', 'left-end']
const rightPositions: Array<PopoverPositon> = ['right-start', 'right', 'right-end']
const bottomPositions: Array<PopoverPositon> = ['bottom-start', 'bottom', 'bottom-end']
</script>API
Attributes
Props
| 属性值 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| position | 对齐位置 | PopoverPositon | bottom |
| trigger | 触发时机 | ['hover','click'] | click |
| offset | 偏移距离(px) | number | 6 |
| duration | 弹出过渡动画持续时间 | number | 250 |
| close-on-click-outside | 点击外部关闭弹出框 | boolean | true |
| close-on-escape | esc关闭弹出框 | boolean | true |
| z-index | z-index值 | number | 0 |
Event
| 名称 | 说明 | 类型 |
|---|---|---|
| show | 弹出框显示时 | () => void |
| hide | 弹出框隐藏时 | () => void |
| toggle | 弹出框状态切换 | (visible: boolean) => void |
Expose
| 方法名 | 说明 | 类型 |
|---|---|---|
| show | 显示弹出框 | () => void |
| hide | 隐藏弹出框 | () => void |
| toggle | 切换弹出框状态 | () => void |
| visible | 弹出框状态值 | boolean |
Slots
| 名称 | 说明 |
|---|---|
| trigger | 触发器内容 |
| content | 弹出框内容 |