Skip to content

Dialog

Encapsulated based on element-plus dialog component, more suitable for rapid business development.

TIP

  • Clicking cancel button closes dialog by default
  • Dialog is draggable by default, does not support closing by clicking mask layer (can be customized)

Basic Dialog

<script lang="ts" setup>
import { ref } from 'vue'

const isShowDialog = ref(false)
</script>

<template>
  <el-button link type="primary" @click="isShowDialog = true">
    Click to open Dialog
  </el-button>
  <z-dialog
    v-model="isShowDialog"
    title="Dialog Title"
    @confirm="isShowDialog = false"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
</template>

Info, Warning, Error Dialog

Pass info, warning, danger to type

<script lang="ts" setup>
import { ref } from 'vue'

const isShowInfoDialog = ref(false)
const isShowWarningDialog = ref(false)
const isShowDangerDialog = ref(false)
</script>

<template>
  <el-button link type="primary" @click="isShowInfoDialog = true">
    Info Dialog
  </el-button>
  <el-button link type="primary" @click="isShowWarningDialog = true">
    Warning Dialog
  </el-button>
  <el-button link type="primary" @click="isShowDangerDialog = true">
    Error Dialog
  </el-button>
  <z-dialog
    v-model="isShowInfoDialog"
    type="info"
    title="Info"
    @confirm="isShowInfoDialog = false"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
  <z-dialog
    v-model="isShowWarningDialog"
    type="warning"
    title="Warning"
    @confirm="isShowWarningDialog = false"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
  <z-dialog
    v-model="isShowDangerDialog"
    type="danger"
    title="Error"
    @confirm="isShowDangerDialog = false"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
</template>

Import Usage

Supports opening dialog through method calls, with default title configuration internally. We can pass strings through title, message or function parameters to customize title and content.

TIP

Due to documentation rendering reasons, ZDialogTip function is directly bound to window. If you use it in your project, you can import it directly from the ideaz-element package.

<script lang="ts" setup>
import { h } from 'vue'

function openInfoDialog() {
  window.ZDialogTip({
    type: 'info',
    message: 'Notification message',
    onConfirm: ({ done, confirmButtonLoading }) => {
      confirmButtonLoading.value = true
      done()
    },
    onCancel: ({ done }) => {
      done()
    },
  })
}

function openWarningDialog() {
  window.ZDialogTip.warning('Warning message', 'Dialog Title', {
    type: 'warning',
    onConfirm: ({ confirmButtonLoading }) => {
      confirmButtonLoading.value = true
    },
    onCancel: ({ done }) => {
      done()
    },
  })
}

function openDangerDialog() {
  window.ZDialogTip({
    type: 'danger',
    message: () => h('span', {}, 'custom message'),
    title: () => h('span', {}, 'custom title'),
    onConfirm: ({ confirmButtonLoading }) => {
      confirmButtonLoading.value = true
    },
    onCancel: ({ done }) => {
      done()
    },
  })
}
</script>

<template>
  <el-button link type="primary" @click="openInfoDialog">
    Info Dialog
  </el-button>
  <el-button link type="primary" @click="openWarningDialog">
    Warning Dialog
  </el-button>
  <el-button link type="primary" @click="openDangerDialog">
    Error Dialog
  </el-button>
</template>

Operation Buttons

  • Use confirmButtonLabel, confirmButtonLoading, cancelButtonLabel, cancelButtonLoading attributes to configure button text and loading state
  • Use confirmButtonProps, cancelButtonProps attributes to configure all dialog button properties
  • In function mode, onConfirm, onCancel receive an object containing done method and button state. Calling done method can close dialog, modifying button state can achieve button loading effect
<script lang="ts" setup>
import { reactive, ref } from 'vue'

const isShowDialog = ref(false)
const isShow = ref(false)

const confirmButtonProps = reactive({
  label: 'Confirm',
  type: 'danger',
  loading: false,
})

const cancelButtonProps = reactive({
  label: 'Cancel',
  type: 'primary',
  loading: false,
})

const isConfirmBtnLoading = ref(false)
const isCancelBtnLoading = ref(false)

function openWarningDialog() {
  window.ZDialogTip({
    type: 'warning',
    message: 'Warning message',
    onConfirm: async ({ done, confirmButtonLoading }) => {
      confirmButtonLoading.value = true
      await delay(200)
      confirmButtonLoading.value = false
      done()
    },
    onCancel: async ({ done, cancelButtonLoading }) => {
      cancelButtonLoading.value = true
      await delay(200)
      cancelButtonLoading.value = false
      done()
    },
  })
}

async function handleConfirm() {
  confirmButtonProps.loading = true
  try {
    await delay(200)
    confirmButtonProps.loading = false
    isShowDialog.value = false
  }
  catch {}
  confirmButtonProps.loading = false
}

async function handleCancel() {
  cancelButtonProps.loading = true
  try {
    await delay(200)
    cancelButtonProps.loading = false
    isShowDialog.value = false
  }
  catch {}
  cancelButtonProps.loading = false
}

async function confirm() {
  isConfirmBtnLoading.value = true
  try {
    await delay(200)
    isConfirmBtnLoading.value = false
    isShow.value = false
  }
  catch {}
  isConfirmBtnLoading.value = false
}

async function cancel() {
  isCancelBtnLoading.value = true
  try {
    await delay(200)
    isCancelBtnLoading.value = false
    isShow.value = false
  }
  catch {}
  isCancelBtnLoading.value = false
}

function delay(time: number) {
  return new Promise((resolve) => {
    setTimeout(resolve, time)
  })
}
</script>

<template>
  <el-button link type="primary" @click="openWarningDialog">
    Event configuration
  </el-button>
  <el-button link type="primary" @click="isShowDialog = true">
    Object prop configuration
  </el-button>
  <el-button link type="primary" @click="isShow = true">
    Basic prop configuration
  </el-button>
  <z-dialog
    v-model="isShowDialog"
    title="Dialog Title"
    :confirm-button-props="confirmButtonProps"
    :cancel-button-props="cancelButtonProps"
    @confirm="handleConfirm"
    @cancel="handleCancel"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
  <z-dialog
    v-model="isShow"
    title="Dialog Title"
    confirm-button-label="Confirm"
    cancel-button-label="Cancel"
    :confirm-button-loading="isConfirmBtnLoading"
    :cancel-button-loading="isCancelBtnLoading"
    @confirm="confirm"
    @cancel="cancel"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
</template>

Custom Title

You can use title slot or render function to customize title

<script lang="ts" setup>
import { h, ref } from 'vue'

const isShowDialog = ref(false)
const isShowRenderDialog = ref(false)

function renderTitle() {
  return h('span', 'Render title')
}

function handleClick() {
  window.ZDialogTip({
    type: 'danger',
    message: () => h('span', {}, 'custom message'),
    title: () => h('span', {}, 'custom title'),
    onConfirm: ({ done, confirmButtonLoading }) => {
      confirmButtonLoading.value = true
      done()
    },
    onCancel: ({ done, cancelButtonLoading }) => {
      cancelButtonLoading.value = true
      done()
      cancelButtonLoading.value = false
    },
  })
}
</script>

<template>
  <el-button link type="primary" @click="isShowDialog = true">
    Click to open slot dialog
  </el-button>
  <el-button link type="primary" @click="isShowRenderDialog = true">
    Click to open render dialog
  </el-button>
  <el-button link type="primary" @click="handleClick">
    Click to open render dialog
  </el-button>
  <z-dialog
    v-model="isShowDialog"
    @confirm="isShowDialog = false"
  >
    <template #title>
      Slot title
    </template>
    <span>This is a piece of information.</span>
  </z-dialog>
  <z-dialog
    v-model="isShowRenderDialog"
    :title="renderTitle"
    @confirm="isShowRenderDialog = false"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
</template>

Pass footer slot or render function to customize footer buttons. Setting footer to false can disable footer buttons.

<script lang="ts" setup>
import { h, ref } from 'vue'

const isShowDialog = ref(false)
const isShowRenderDialog = ref(false)
const isShowFooterDialog = ref(false)

function renderFooter() {
  return h('div', 'Render footer')
}
</script>

<template>
  <ElButton link type="primary" @click="isShowDialog = true">
    Click to open slot dialog
  </ElButton>
  <ElButton link type="primary" @click="isShowRenderDialog = true">
    Click to open render dialog
  </ElButton>
  <ElButton link type="primary" @click="isShowFooterDialog = true">
    Click to open footer dialog
  </ElButton>
  <z-dialog
    v-model="isShowDialog"
    title="Dialog Title"
  >
    <template #footer>
      <ElButton size="default" @click="isShowDialog = false">
        Close dialog
      </ElButton>
    </template>
    <span>This is a piece of information.</span>
  </z-dialog>
  <z-dialog
    v-model="isShowRenderDialog"
    title="Dialog Title"
    :footer="renderFooter"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
  <z-dialog
    v-model="isShowFooterDialog"
    title="Dialog Title"
    :footer="false"
  >
    <span>This is a piece of information.</span>
  </z-dialog>
</template>

before-close

Callback before closing

<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'

const isShowDialog = ref(false)

function handleConfirm(done: () => void) {
  console.log(done, 'sdf')
}

async function handleBeforeClose(done: () => void) {
  console.log('handleBeforeClose')
  await delay(200)
  done()
}

function handleOpen() {
  isShowDialog.value = true
}

function openDialog() {
  window.ZDialogTip({
    type: 'warning',
    message: 'Content message',
    title: 'Dialog Title',
    beforeClose: (done) => {
      console.log('before close extend')
      done()
    },
    onConfirm: ({ confirmButtonLoading }) => {
      confirmButtonLoading.value = true
    },
    onCancel: ({ done }) => {
      done()
    },
  })
}

function handleClose() {
  console.log('handleClose')
}

function delay(time: number) {
  return new Promise((resolve) => {
    setTimeout(resolve, time)
  })
}
</script>

<template>
  <el-button link type="primary" @click="handleOpen">
    Click to open Dialog
  </el-button>
  <el-button link type="primary" @click="openDialog">
    Click to open Dialog
  </el-button>
  <z-dialog
    v-model="isShowDialog"
    type="warning"
    :before-close="handleBeforeClose"
    @closed="handleClose"
    @confirm="handleConfirm"
  >
    <template #header>
      <span>slotTitle</span>
    </template>
    <span>This is a piece of information.</span>
  </z-dialog>
</template>

z-dialog Attributes

AttributeDescriptionTypeDefault
model-value / v-modelWhether to show Dialogboolean
titleDialog titlestring / () => VNode''
typeDialog typenormal / info / warning / danger'normal'
footerCustom Dialog footer() => VNode''
confirmButtonLabelConfirm button textstring'Confirm'
confirmButtonLoadingConfirm button loading statebooleanfalse
confirmButtonPropsConfirm button propertiesobject
cancelButtonLabelCancel button textstring'Cancel'
cancelButtonLoadingCancel button loading statebooleanfalse
cancelButtonPropsCancel button propertiesobject
widthDialog width, default value is 50%string / number''
fullscreenWhether Dialog is fullscreenbooleanfalse
topDialog CSS margin-top value, default is 15vhstring''
modalWhether to show modalbooleantrue
modal-classCustom class name for modalstring
append-to-bodyWhether Dialog itself is inserted into body element. Nested Dialog must specify this attribute and assign it to truebooleanfalse
lock-scrollWhether to lock body scroll when Dialog appearsbooleantrue
custom-class deprecatedCustom class name for Dialogstring''
open-delayDialog opening delay time in millisecondsnumber0
close-delayDialog closing delay time in millisecondsnumber0
close-on-click-modalWhether Dialog can be closed by clicking modalbooleantrue
close-on-press-escapeWhether Dialog can be closed by pressing ESCbooleantrue
show-closeWhether to show close buttonbooleantrue
before-closeCallback before closing, will pause Dialog closing. Dialog will only close when done parameter method is executed in callback function.Function
draggableEnable draggable feature for Dialogbooleanfalse
centerWhether to center align Dialog header and footerbooleanfalse
align-center 2.2.16Whether to horizontally and vertically align dialogbooleanfalse
destroy-on-closeDestroy elements in Dialog when closingbooleanfalse
close-iconCustom close icon, default Closestring / Component
z-indexSame as native CSS z-index, changes z-axis ordernumber
header-aria-level a11yaria-level attribute for headerstring2

z-dialog Slots

Slot NameDescription
Dialog content
headerContent for dialog title; replaces title section but does not remove close button.
footerContent for Dialog button operation area

z-dialog Events

Event NameDescriptionType
openCallback when Dialog opensFunction
openedCallback when Dialog opening animation endsFunction
closeCallback when Dialog closesFunction
closedCallback when Dialog closing animation endsFunction
open-auto-focusCallback when input focus is on Dialog contentFunction
close-auto-focusCallback when input focus leaves Dialog contentFunction
confirmCallback when confirm button is clickedFunction
cancelCallback when cancel button is clickedFunction

Released under the MIT License.