import{o as s,a as e,y as d}from"./vue-libs.b44bc779.js";const a={class:"van-doc-markdown-body"},n=d(`

Dialog

Intro

A modal box pops up on the page, which is often used for message prompts, message confirmation, or to complete specific interactive operations in the current page. It supports two methods: function call and component call.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { Dialog } from 'vant';

const app = createApp();
app.use(Dialog);

Usage

Alert dialog

Used to prompt for some messages, only including one confirm button.

Dialog.alert({
  title: 'Title',
  message: 'Content',
}).then(() => {
  // on close
});

Dialog.alert({
  message: 'Content',
}).then(() => {
  // on close
});

Confirm dialog

Used to confirm some messages, including a confirm button and a cancel button.

Dialog.confirm({
  title: 'Title',
  message: 'Content',
})
  .then(() => {
    // on confirm
  })
  .catch(() => {
    // on cancel
  });

Round Button Style

Use round button style.

Dialog.alert({
  title: 'Title',
  message: 'Content',
  theme: 'round-button',
}).then(() => {
  // on close
});

Dialog.alert({
  message: 'Content',
  theme: 'round-button',
}).then(() => {
  // on close
});

Async Close

const beforeClose = (action) =>
  new Promise((resolve) => {
    setTimeout(() => {
      resolve(action === 'confirm');
    }, 1000);
  });

Dialog.confirm({
  title: 'Title',
  message: 'Content',
  beforeClose,
});

Global Method

After registering the Dialog component through app.use, the $dialog method will be automatically mounted on all subComponents of the app.

export default {
  mounted() {
    this.$dialog.alert({
      message: 'Content',
    });
  },
};

Advanced Usage

If you need to render vue components within a dialog, you can use dialog component.

<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>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    return { show };
  },
};

API

Methods

NameDescriptionAttributeReturn value
DialogShow dialogoptions: DialogOptionsPromise<void>
Dialog.alertShow alert dialogoptions: DialogOptionsPromise<void>
Dialog.confirmShow confirm dialogoptions: DialogOptionsPromise<void>
Dialog.setDefaultOptionsSet default options of all dialogsoptions: DialogOptionsvoid
Dialog.resetDefaultOptionsReset default options of all dialogs-void
Dialog.closeClose dialog-void

DialogOptions

AttributeDescriptionTypeDefault
titleTitlestring-
widthDialog widthnumber | string320px
messageMessagestring | () => JSX.ELement-
messageAlignMessage text align, can be set to left rightstringcenter
themeTheme style, can be set to round-buttonstringdefault
classNameCustom classNamestring | Array | object-
showConfirmButtonWhether to show confirm buttonbooleantrue
showCancelButtonWhether to show cancel buttonbooleanfalse
cancelButtonTextCancel button textstringCancel
cancelButtonColorCancel button colorstringblack
confirmButtonDisabled v3.5.0Whether to disable cancel buttonbooleanfalse
confirmButtonTextConfirm button textstringConfirm
confirmButtonColorConfirm button colorstring#ee0a24
confirmButtonDisabled v3.5.0Whether to disable confirm buttonbooleanfalse
overlayWhether to show overlaybooleantrue
overlayClassCustom overlay classstring | Array | object-
overlayStyleCustom overlay styleobject-
closeOnPopstateWhether to close when popstatebooleantrue
closeOnClickOverlayWhether to close when overlay is clickedbooleanfalse
lockScrollWhether to lock body scrollbooleantrue
allowHtmlWhether to allow HTML rendering in messagebooleanfalse
beforeCloseCallback function before close(action: string) => boolean | Promise<boolean>-
transitionTransition, equivalent to name prop of transitionstring-
teleportSpecifies a target element where Dialog will be mountedstring | Elementbody

Props

AttributeDescriptionTypeDefault
v-model:showWhether to show dialogboolean-
titleTitlestring-
widthWidthnumber | string320px
messageMessagestring | () => 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-disabled v3.5.0Whether to disable cancel buttonbooleanfalse
confirm-button-textConfirm button textstringConfirm
confirm-button-colorConfirm button colorstring#ee0a24
confirm-button-disabled v3.5.0Whether to disable confirm buttonbooleanfalse
overlayWhether to show overlaybooleantrue
overlay-classCustom overlay classstring-
overlay-styleCustom overlay styleobject-
close-on-popstateWhether to close when popstatebooleantrue
close-on-click-overlayWhether to close when overlay is clickedbooleanfalse
lazy-renderWhether to lazy render util appearedbooleantrue
lock-scrollWhether to lock background scrollbooleantrue
allow-htmlWhether to allow HTML rendering in messagebooleanfalse
before-closeCallback function before close(action: string) => boolean | Promise<boolean>-
transitionTransition, equivalent to name prop of transitionstring-
teleportSpecifies a target element where Dialog will be mountedstring | Element-

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
footer v3.0.11Custom footer

Types

The component exports the following type definitions:

import type {
  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-animation-duration-base)-
--van-dialog-border-radius16px-
--van-dialog-background-colorvar(--van-background-color-light)-
--van-dialog-header-font-weightvar(--van-font-weight-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-danger-color)-
`,19),o=[n],h={__name:"README",setup(l,{expose:t}){return t({frontmatter:{}}),(c,i)=>(s(),e("div",a,o))}};export{h as default};