Skip to content

Dialog - Vant 4

Dialog

💬 Capture attention with elegant modal dialogs that users can't ignore!

🎭 Intro

Meet the Dialog component - your go-to solution for creating impactful user interactions! 🚀 Whether you need to alert users, confirm critical actions, or gather input, this versatile modal component delivers a polished experience every time. With both functional and component-based approaches, you have the flexibility to implement dialogs exactly how you want them! ✨

📦 Install

Get your Dialog component ready in no time! Register it globally via app.use and start creating engaging user interactions immediately! 🎯 Check out our Component Registration guide for more installation options.

js
import { createApp } from'vue'; import { Dialog } from'vant'; const app = createApp(); app.use(Dialog);

⚡ Function Call

Experience the magic of instant dialogs! Vant provides powerful utility functions that let you create beautiful dialogs with just a single function call - no template setup required! 🪄

For example, calling the showDialog function will render a Dialog directly in the page like magic! ✨

js
import { showDialog } from'vant'; showDialog({ message: 'Alert' });

🎨 Usage

🚨 Alert Dialog

Perfect for delivering important messages to your users! 📢 This simple yet effective dialog includes just one confirm button, making it ideal for notifications and alerts.

js
import { showDialog } from'vant'; showDialog({ title: 'Title', message: 'Content', }).then(() => { // on close });

❓ Confirm Dialog

Need user confirmation before proceeding? This powerful dialog includes both confirm and cancel buttons, giving users full control over their decisions! 🤔💭

js
import { showConfirmDialog } from'vant'; showConfirmDialog({ title: 'Title', message: 'Content', }) .then(() => { // on confirm }) .catch(() => { // on cancel });

🎪 Round Button Style

Transform your dialogs with a modern, sleek appearance! Setting the theme option to round-button creates beautifully rounded buttons that add a contemporary touch to your interface. 🌟

js
import { showDialog } from'vant'; showDialog({ title: 'Title', message: 'Content', theme: 'round-button', }).then(() => { // on close });

⏳ Async Close

Take full control over dialog closing behavior! Perfect for validating user input or performing async operations before allowing the dialog to close. 🔄

js
import { showConfirmDialog } from'vant'; const beforeClose = (action) => new Promise((resolve) => { setTimeout(() => { if (action === 'confirm') { resolve(true); } else { // Prevent the default behavior resolve(false); } }, 1000); }); showConfirmDialog({ title: 'Title', message: 'Content', beforeClose, });

🧩 Use Dialog Component

Want complete creative control? Use the Dialog component directly to embed custom content, images, forms, or any Vue components within your dialogs! 🎨 Perfect for complex interactions that go beyond simple text messages.

html
<van-dialog v-model:show="show" title="Title" show-cancel-button> <img src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-3.jpeg" /> </van-dialog>
js
import { ref } from'vue'; export default { setup() { const show = ref(false); return { show }; }, };

📚 API

🛠️ Methods

Vant exports these powerful Dialog utility functions to make your life easier! 🚀

MethodsAttributeReturn valueDescription
showDialogoptionsPromise<void>Show dialog
showConfirmDialogoptionsPromise<void>Show confirm dialog
closeDialog-voidClose dialog
setDialogDefaultOptionsoptionsvoidSet default options of all dialogs
resetDialogDefaultOptions-voidReset default options of all dialogs

DialogOptions

AttributeDescriptionTypeDefault
titleTitlestring-
widthDialog width*numberstring*
messageMessage*string() => JSX.ELement*
messageAlignMessage text align, can be set to left``rightstringcenter
themeTheme style, can be set to round-buttonstringdefault
classNameCustom className*stringArray
showConfirmButtonWhether to show confirm buttonbooleantrue
showCancelButtonWhether to show cancel buttonbooleanfalse
cancelButtonTextCancel button textstringCancel
cancelButtonColorCancel button colorstringblack
cancelButtonDisabledWhether to disable cancel buttonbooleanfalse
confirmButtonTextConfirm button textstringConfirm
confirmButtonColorConfirm button colorstring#ee0a24

| confirmButtonDisabled | Whether to disable confirm button | boolean | false | | destroyOnClose v4.9.18 | Whether to destroy content when closed | boolean | false | | overlay | Whether to show overlay | boolean | true | | overlayClass | Custom overlay class | string | Array | object | - | | overlayStyle | Custom overlay style | object | - | | closeOnPopstate | Whether to close when popstate | boolean | true | | closeOnClickOverlay | Whether to close when overlay is clicked | boolean | false | | lockScroll | Whether to lock body scroll | boolean | true | | allowHtml | Whether to allow HTML rendering in message | boolean | false | | beforeClose | Callback function before close | (action: string) => boolean | Promise<boolean> | - | | transition | Transition, equivalent to name prop of transition | string | - |

| teleport | Specifies a target element where Dialog will be mounted | string | Element | body | | keyboardEnabled | Whether to enable keyboard capabilities. When displaying the confirm and cancel buttons, the keyboard's Enter and Esc will call the confirm and cancel functions by default | boolean | true |

Props

AttributeDescriptionTypeDefault
v-model:showWhether to show dialogboolean-
titleTitlestring-
widthWidth*numberstring*
messageMessage*string() => JSX.ELement*
message-alignMessage align, can be set to left``right``justifystringcenter
themeTheme style, can be set to round-buttonstringdefault
show-confirm-buttonWhether to show confirm buttonbooleantrue
show-cancel-buttonWhether to show cancel buttonbooleanfalse
cancel-button-textCancel button textstringCancel
cancel-button-colorCancel button colorstringblack
cancel-button-disabledWhether to disable cancel buttonbooleanfalse
confirm-button-textConfirm button textstringConfirm
confirm-button-colorConfirm button colorstring#ee0a24

| confirm-button-disabled | Whether to disable confirm button | boolean | false | | destroy-on-close v4.9.18 | Whether to destroy content when closed | boolean | false | | z-index | Set the z-index to a fixed value | number | string | 2000+ | | overlay | Whether to show overlay | boolean | true | | overlay-class | Custom overlay class | string | - | | overlay-style | Custom overlay style | object | - | | close-on-popstate | Whether to close when popstate | boolean | true | | close-on-click-overlay | Whether to close when overlay is clicked | boolean | false | | lazy-render | Whether to lazy render util appeared | boolean | true | | lock-scroll | Whether to lock background scroll | boolean | true | | allow-html | Whether to allow HTML rendering in message | boolean | false | | before-close | Callback function before close | (action: string) => boolean | Promise<boolean> | - | | transition | Transition, equivalent to name prop of transition | string | - |

| teleport | Specifies a target element where Dialog will be mounted | string | Element | - | | keyboard-enabled | Whether to enable keyboard capabilities. When displaying the confirm and cancel buttons, the keyboard's Enter and Esc will call the confirm and cancel functions by default | boolean | true |

Events

EventDescriptionParameters
confirmEmitted when the confirm button is clicked-
cancelEmitted when the cancel button is clicked-
openEmitted when opening Dialog-
closeEmitted when closing Dialog-
openedEmitted when Dialog is opened-
closedEmitted when Dialog is closed-

Slots

NameDescription
defaultCustom message
titleCustom title
footerCustom footer

Types

The component exports the following type definitions:

ts
importtype { DialogProps, DialogTheme, DialogMessage, DialogOptions, DialogMessageAlign, } from'vant';

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-dialog-width320px-
--van-dialog-small-screen-width90%-
--van-dialog-font-sizevar(--van-font-size-lg)-
--van-dialog-transitionvar(--van-duration-base)-
--van-dialog-radius16px-
--van-dialog-backgroundvar(--van-background-2)-
--van-dialog-header-font-weightvar(--van-font-bold)-
--van-dialog-header-line-height24px-
--van-dialog-header-padding-top26px-
--van-dialog-header-isolated-paddingvar(--van-padding-lg) 0-
--van-dialog-message-paddingvar(--van-padding-lg)-
--van-dialog-message-font-sizevar(--van-font-size-md)-
--van-dialog-message-line-heightvar(--van-line-height-md)-
--van-dialog-message-max-height60vh-
--van-dialog-has-title-message-text-colorvar(--van-gray-7)-
--van-dialog-has-title-message-padding-topvar(--van-padding-xs)-
--van-dialog-button-height48px-
--van-dialog-round-button-height36px-
--van-dialog-confirm-button-text-colorvar(--van-primary-color)-

## 🔗 Related Docs

- [Toast](./toast) - Show lightweight feedback messages
- [Popup](./popup) - Create custom modal overlays
- [ActionSheet](./action-sheet) - Present action options to users
- [Notify](./notify) - Display notification messages
- [Overlay](./overlay) - Create backdrop overlays
- [Button](./button) - Interactive button components
- [ConfigProvider](./config-provider) - Global configuration
- [Advanced Usage](./advanced-usage) - Advanced component techniques

Enterprise-level mobile solution based on Vant