Popup Popup
Present an overlay panel with guarded closing and scroll locking.
Import
Use <hui-popup> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
<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('No event yet')
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>
When scrolling is locked, the panel also prevents touchmove. Put long content inside a scroll region. Returning false or rejecting beforeClose keeps the popup open; changing visible directly remains parent control.
overlayStyle accepts a style string or object and applies directly to the overlay, including when rootPortal is enabled. The example reduces overlay opacity; use --hui-overlay-bg to configure its background. Style the panel content separately.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
visible | boolean | undefined | Controlled visibility; undefined is omitted and explicit false stays closed. |
defaultVisible | boolean | false | Initial uncontrolled visibility. |
position | PopupPosition | 'center' | Popup placement. |
round | boolean | false | Use rounded or pill styling. |
overlay | boolean | true | Show the overlay. |
overlayStyle | string | CSSProperties | — | String or object styles applied to the overlay, including inside rootPortal. |
closeOnClickOverlay | boolean | true | Request guarded closing when the overlay is clicked. |
closeable | boolean | false | Show the panel close action. |
beforeClose | ((reason: PopupCloseReason) => boolean | void | Promise<boolean | void>) | — | Close guard; false or rejection prevents closing. |
duration | number | { enter: number; leave: number; } | 300 | Animation duration; a number applies the same milliseconds to enter and leave. |
zIndex | number | — | Layer z-index; allocated from the current page stack when omitted. |
lockScroll | boolean | true | Lock background scroll while visible; release when hidden or deactivated. |
lazyRender | boolean | true | Render content after its first activation or opening. |
destroyOnClose | boolean | false | Unmount content after the leave animation. |
safeAreaInsetBottom | boolean | true | Reserve the bottom safe area. |
safeAreaInsetTop | boolean | false | Reserve the top safe area. |
rootPortal | boolean | false | Try to mount at the page root; support varies by platform. |
avoidKeyboard | boolean | false | Raise a bottom popup by the reported keyboard height. |
ariaLabel | string | — | Accessible name. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
update:visible | (visible: boolean) | Request a visibility update. |
open | () | Opening started. |
opened | () | Enter animation finished. |
close | (reason: PopupCloseReason) | Closing started after guards passed. |
closed | () | Leave animation finished. |
click-overlay | (event: unknown) | Overlay clicked, before close updates. |
Slots
| Slot | Parameters | Description |
|---|---|---|
default | — | Main content. |
close-icon | — | Custom close-icon region; replaces its fallback content. |
Methods
| Method | Signature | Description |
|---|---|---|
open | (): void | Open the uncontrolled panel or request a controlled update. |
close | (reason?: PopupCloseReason | undefined): Promise<boolean> | Request closing through configured guards. |
isTopLayer | (): boolean | Whether this Popup is the top layer on the active page. |
Types
PopupProps、PopupPosition、PopupCloseReason、PopupExpose、PopupEmits are available from @nscode/handy-uni.
export type PopupPosition = 'center' | 'top' | 'bottom' | 'left' | 'right'
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
| Scroll lock and stacking | Release on deactivation and restore on activation | Root-layer support degrades by platform | Verify leave animation and native navigation stacking | Locks the background container; configurable Teleport target; iOS scrolling pending verification |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.