Skip to content

Modal

Modal

基础用法

show dialog modal

源码使用方式

vue
<template>
  <div @click="dialogModalShow = true" class="resetButton">
    show dialog modal
  </div>
  <dialog-modal v-model="dialogModalShow" ref="dialogModalRef" title="Tips" width="50%" :before-close="handleClose"
    @cancel="handleModalCancel" @confirm="handleModalConfirm" :has-cancel="true" :has-confirm="true"
    :cancel-text="'Cancel'" :confirm-text="'Ok'">
    <template #header>header tip text</template>
    <span>This is a message</span>
  </dialog-modal>
</template>

<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { dialogModal } from '@mini-element-plus/components/dialogModal'
const dialogModalShow = ref(false)
const dialogModalRef = ref()
const handleClose = (done: () => void) => {
  alert('dialog close')
  done()
}
const handleModalCancel = () => {
  alert('dialog cancel')
  dialogModalShow.value = false
}
const handleModalConfirm = () => {
  alert('dialog confirm')
  dialogModalShow.value = false
}
onMounted(() => {
  document.onkeydown = handleKeydown
})
onUnmounted(() => {
  document.onkeydown = null
})
const handleKeydown = (e: Event) => {
  console.log(e)
  if (e.key === 'ArrowRight') {
    e.action = 'Right'
  } else if (e.key === 'ArrowLeft') {
    e.action = 'Left'
  } else if (e.key === 'Enter') {
    e.action = 'Enter'
  } else if (e.key === 'Backspace') {
    e.action = 'Back'
  }
  dialogModalRef.value.KEYDOWN(e)
}
</script>
<style lang="scss" scoped>
.resetButton {
  width: fit-content;
  height: fit-content;
  font-size: 18px;
  background: green;
  color: aqua;
  border: 1px solid aqua;
  z-index: 1
}
</style>

md


title: Dialog lang: en-US

Dialog

Informs users while preserving the current page state.

Basic usage

Dialog pops up a dialog box, and it's quite customizable.

::: Set model-value / v-model attribute with a Boolean, and Dialog shows when it is true. The Dialog has two parts: body and footer, and the latter requires a slot named footer. The optional title attribute (empty by default) is for defining a title. Finally, this example demonstrates how before-close is used.

:::

TIP

before-close only works when user clicks the close icon or the backdrop. If you have buttons that close the Dialog in the footer named slot, you can add what you would do with before-close in the buttons' click event handler.

Customized Content

The content of Dialog can be anything, even a table or a form. This example shows how to use Element Plus Table and Form with Dialog.

Customized Header

The header slot can be used to customize the area where the title is displayed. In order to maintain accessibility, use the title attribute in addition to using this slot, or use the titleId slot property to specify which element should be read out as the dialog title.

:::demo

dialog/customization-header

:::

Centered content

Dialog's content can be centered.

:::demo Setting center to true will center dialog's header and footer horizontally. center only affects Dialog's header and footer. The body of Dialog can be anything, so sometimes it may not look good when centered. You need to write some CSS if you wish to center the body as well.

dialog/centered-content

:::

TIP

The content of Dialog is lazily rendered, which means the default slot is not rendered onto the DOM until it is firstly opened. Therefore, if you need to perform a DOM manipulation or access a component using ref, do it in the open event callback.

Attributes

AttributeDescriptionTypeAccepted ValuesDefault
model-value / v-modelvisibility of Dialogboolean
titletitle of Dialog. Can also be passed with a named slot (see the following table)string
widthwidth of Dialogstring / number50%
topvalue for margin-top of Dialog CSSstring15vh
modalwhether a mask is displayedbooleantrue
custom-classcustom class names for Dialogstring
close-on-click-modalwhether the Dialog can be closed by clicking the maskbooleantrue
show-closewhether to show a close buttonbooleantrue
before-closecallback before Dialog closes, and it will prevent Dialog from closingfunction(done),done is used to close the Dialog
centerwhether to align the header and footer in centerbooleanfalse
hasCancelwhether dialog has cancel buttonbooleantrue
hasConfirmwhether dialog has confirm buttonbooleantrue
cancelTextset dialog cancel button css textstring'cancel_btn'
confirmTextset dialog confirm button css textstring'confirm_btn'
defaultFocusset dialog default button focusstringBUTTON_TYPE.CANCEL
focusControlwhether dialog need set focusbooleantrue

Slots

NameDescription
content of Dialog
headercontent of the Dialog header; Replacing this removes the title, but does not remove the close button.
titleWorks the same as the header slot. Use that instead.
footercontent of the Dialog footer

Events

Event NameDescriptionParameters
opentriggers when the Dialog opens
closetriggers when the Dialog closes
canceltriggers when the Dialog cancel button pressed
confirmtriggers when the Dialog confirm button pressed