Dialog Dialog
Confirm actions with asynchronous confirmation and close guards.
Import
Use <hui-dialog> 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 dialog</button
><hui-dialog
ref="control"
v-model:visible="visible"
title="提交报告 / Submit report"
message="确认提交当前内容? / Submit the current content?"
show-cancel-button
:before-confirm="beforeConfirm"
@update:visible="event0"
@confirm="event1"
@cancel="event2"
@close="event3"
@open="event4"
@opened="event5"
@closed="event6"
/>
<hui-button @click="() => control?.open()">open()</hui-button>
<hui-button @click="() => control?.close('close')">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)
const beforeConfirm = async () => {
await Promise.resolve()
return true
}
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:visible' + ': ' + [describe(arg0)].join(', ')
}
const event1 = () => {
lastEvent.value = 'confirm'
}
const event2 = () => {
lastEvent.value = 'cancel'
}
const event3 = (arg0: unknown) => {
lastEvent.value = 'close' + ': ' + [describe(arg0)].join(', ')
}
const event4 = () => {
lastEvent.value = 'open'
}
const event5 = () => {
lastEvent.value = 'opened'
}
const event6 = () => {
lastEvent.value = 'closed'
}
</script>
Confirmation runs beforeConfirm and then beforeClose. Pending buttons expose aria-disabled. Handle rejection when a function-based confirmation is cancelled.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
visible | boolean | undefined | Controlled visibility; undefined is omitted and explicit false stays closed. |
defaultVisible | boolean | false | Initial uncontrolled visibility. |
title | string | — | Title. |
message | string | — | Dialog message. |
messageAlign | "left" | "center" | 'center' | Dialog message alignment. |
showCancelButton | boolean | false | Show the cancel button. |
showConfirmButton | boolean | true | Show the confirm button. |
confirmButtonText | string | — | Confirm button text; falls back to the current locale. |
cancelButtonText | string | — | Cancel button text; falls back to the current locale. |
confirmButtonType | ButtonType | 'primary' | Semantic type of the confirm button. |
closeOnClickOverlay | boolean | false | Request guarded closing when the overlay is clicked. |
beforeConfirm | (() => unknown) | — | Confirmation guard; false or rejection retains the current content. |
beforeClose | ((action: DialogAction) => unknown) | — | Close guard; false or rejection prevents closing. |
width | string | number | 320 | Width; numeric values use px. |
zIndex | number | — | Layer z-index; allocated from the current page stack when omitted. |
rootPortal | boolean | false | Try to mount at the page root; support varies by platform. |
lockScroll | boolean | true | Lock background scroll while visible; release when hidden or deactivated. |
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 | (value: boolean) | Request a visibility update. |
confirm | () | Confirmation action. |
cancel | () | Cancellation action. |
close | (action: DialogAction) | Closing started after guards passed. |
open | () | Opening started. |
opened | () | Enter animation finished. |
closed | () | Leave animation finished. |
Slots
| Slot | Parameters | Description |
|---|---|---|
title | — | Custom title region; replaces its fallback content. |
default | — | Main content. |
footer | — | Custom footer region; replaces its fallback content. |
Methods
| Method | Signature | Description |
|---|---|---|
open | (): void | Open the uncontrolled panel or request a controlled update. |
close | (action?: DialogAction | undefined): Promise<boolean> | Request closing through configured guards. |
Function API and back navigation
showDialog(options) and showConfirmDialog(options) return a Promise. Confirmation resolves with { action: 'confirm' }. Cancellation, closing, concurrency rejection, replacement and page disposal reject with cancel, close, rejected, replaced and unmounted. A false return or rejected hook keeps the dialog open. closeDialog() requests closure through beforeClose.
Place hui-page-host on each page. The default concurrency: 'queue' waits for the current leave animation before displaying the next item. replace replaces the current dialog; reject rejects a new request when a dialog exists. The Promise settles when closing starts; closed fires after the animation. Without PageHost, plain requests can use the native modal. Requests with hooks reject with rejected instead of bypassing the hooks.
import { showConfirmDialog } from '@nscode/handy-uni'
async function confirmRemoval() {
try {
await showConfirmDialog({
title: 'Delete record',
message: 'Delete the selected record? This cannot be undone.',
confirmButtonType: 'danger'
})
// Perform the confirmed deletion here.
} catch {
// Cancellation, closure or replacement must not delete data.
}
}
Use this page handler on App and uni-H5. Checking the event source keeps programmatic uni.navigateBack() calls from closing an overlay instead of navigating.
import { onBackPress } from '@dcloudio/uni-app'
import { closeTopLayer } from '@nscode/handy-uni'
onBackPress((e) => e.from === 'backbutton' && closeTopLayer())
Types
DialogProps、DialogAction、DialogRejectReason、DialogExpose、DialogOptions、DialogResult、DialogEmits are available from @nscode/handy-uni.
export type DialogAction = 'confirm' | 'cancel' | 'close'
export type DialogRejectReason =
| Exclude<DialogAction, 'confirm'>
| 'rejected'
| 'replaced'
| 'unmounted'
export interface DialogOptions {
title?: string
message?: string
messageAlign?: 'left' | 'center'
showCancelButton?: boolean
showConfirmButton?: boolean
confirmButtonText?: string
cancelButtonText?: string
confirmButtonType?: ButtonType
closeOnClickOverlay?: boolean
beforeConfirm?: () => unknown
beforeClose?: (action: DialogAction) => unknown
width?: number | string
concurrency?: 'queue' | 'replace' | 'reject'
}
export interface DialogResult {
action: 'confirm'
}
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 |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.