翻譯進行中
本頁尚未翻譯為繁體中文,以下顯示簡體中文原文。範例的預設元件文字仍使用繁體中文。
Popup 弹出层
提供遮罩、定位、关闭拦截和滚动锁。
引入
uni-app 配置 easycom 后使用 <hui-popup>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<template>
<view>
<button @click="visible = true">打开弹层 / Open</button
><hui-popup
ref="control"
v-model:visible="visible"
position="bottom"
:overlay-style="{ opacity: 0.6 }"
round
closeable
:before-close="guard"
@update:visible="event0"
@open="event1"
@opened="event2"
@close="event3"
@closed="event4"
@click-overlay="event5"
><view class="content"
><hui-text>先取消拦截,再关闭 / Allow closing after the first attempt</hui-text
><hui-button @click="visible = false">外部关闭 / Close externally</hui-button></view
></hui-popup
>
<hui-button @click="() => control?.open()">open()</hui-button>
<hui-button @click="() => control?.close('api')">close()</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const control = ref()
const visible = ref(false)
let attempts = 0
const guard = () => ++attempts > 1
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:visible' + ': ' + [describe(arg0)].join(', ')
}
const event1 = () => {
lastEvent.value = 'open'
}
const event2 = () => {
lastEvent.value = 'opened'
}
const event3 = (arg0: unknown) => {
lastEvent.value = 'close' + ': ' + [describe(arg0)].join(', ')
}
const event4 = () => {
lastEvent.value = 'closed'
}
const event5 = (arg0: unknown) => {
lastEvent.value = 'click-overlay' + ': ' + [describe(arg0)].join(', ')
}
</script>
锁定滚动时,面板也会拦截 touchmove;长内容放入业务侧 huix-scroll 对应的滚动区域。beforeClose 返回 false 或 Promise 拒绝都保留弹层;直接修改 visible 属于父级控制。
overlayStyle 接受样式字符串或对象,直接作用于遮罩节点,开启 rootPortal 后仍有效。示例通过 opacity 降低遮罩不透明度;遮罩背景可用 --hui-overlay-bg 配置,面板内容样式独立设置。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | undefined | 受控显示,配 v-model:visible |
defaultVisible | boolean | false | 非受控初始值 |
position | PopupPosition | 'center' | 面板出现位置。 |
round | boolean | false | 圆角(底部弹出只圆上边) |
overlay | boolean | true | 显示遮罩 |
overlayStyle | string | CSSProperties | — | 遮罩节点的字符串或对象样式;提升到 rootPortal 后仍有效 |
closeOnClickOverlay | boolean | true | 点击遮罩后请求关闭,仍执行关闭拦截。 |
closeable | boolean | false | 右上角关闭按钮 |
beforeClose | ((reason: PopupCloseReason) => boolean | void | Promise<boolean | void>) | — | 关闭前钩子,exclusive 模式(总体设计 6.3) |
duration | number | { enter: number; leave: number; } | 300 | 动画时长 ms |
zIndex | number | — | 显式层号;不传按层栈分配(总体设计 6.2) |
lockScroll | boolean | true | 锁背景滚动(总体设计 6.4) |
lazyRender | boolean | true | 第一次打开时才渲染内容 |
destroyOnClose | boolean | false | 关闭动画结束后卸载内容 |
safeAreaInsetBottom | boolean | true | position 为 bottom 时生效 |
safeAreaInsetTop | boolean | false | position 为 top 时生效;页面用了自定义导航栏时设 true |
rootPortal | boolean | false | 挂到页面根(总体设计 6.1) |
avoidKeyboard | boolean | false | 底部弹出时随键盘抬起(总体设计 7.4) |
ariaLabel | string | — | 对话框名称 |
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:visible | (visible: boolean) | 组件自己打开、关闭时发;父组件改 visible 不发 |
open | () | 开始打开 |
opened | () | 打开动画结束 |
close | (reason: PopupCloseReason) | 开始关闭;在 update:visible(false) 之后 |
closed | () | 关闭动画结束 |
click-overlay | (event: unknown) | 点了遮罩(无论是否因此关闭) |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
default | — | 主体内容。 |
close-icon | — | 自定义 close-icon 区域,覆盖默认内容。 |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
open | (): void | 打开非受控面板;受控时发更新请求。 |
close | (reason?: PopupCloseReason | undefined): Promise<boolean> | 经前置钩子请求关闭。 |
isTopLayer | (): boolean | 当前 Popup 是否为前台页面最上层弹层。 |
类型
PopupProps、PopupPosition、PopupCloseReason、PopupExpose、PopupEmits 可从 @nscode/handy-uni 导入。
ts
export type PopupPosition = 'center' | 'top' | 'bottom' | 'left' | 'right'
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
| 滚动锁和层级 | 失活归还锁,激活恢复 | 根层能力按平台降级 | 离开动画与原生导航层叠待验 | 固定背景滚动容器,Teleport 目标可配置;iOS 滚动待验 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。