Skip to content
🌏 Translated with the assistance of DeepSeek and ChatGPT

Dialog

When a dialog is triggered, the dialog will pop up.

Basic Usage

The visible property controls the display and hiding of the dialog. When visible is passed, it enters controlled mode. If not passed or set to undefined, it is in uncontrolled mode, and the defaultVisible property can be provided as the default value.

<template>
	<div>
		<px-dialog title="Title" v-model:visible="visible">
			Content Content Content Content Content Content Content Content Content Content Content
			Content Content Content Content Content Content Content Content Content Content Content
			Content Content Content Content Content Content Content Content Content Content
		</px-dialog>
		<px-dialog title="Uncontrolled Dialog" ref="dialog">
			Content Content Content Content Content Content Content Content Content Content Content
			Content Content Content Content Content Content Content Content Content Content Content
			Content Content Content Content Content Content Content Content Content Content
		</px-dialog>
		<px-space>
			<px-button @click="visible = true">Open Controlled Dialog</px-button>
			<px-button @click="dialog?.open()">Open Uncontrolled Dialog</px-button>
		</px-space>
	</div>
</template>

<script setup lang="ts">
import { ref, shallowRef } from 'vue'
import { Dialog } from '@pixelium/web-vue'

// If on-demand import
// import { Dialog } from '@pixelium/web-vue/es';

const visible = ref(false)
const dialog = shallowRef<InstanceType<typeof Dialog> | null>(null)
</script>

Form in Dialog

This is just an example of using a form inside a dialog. As demonstrated by the example, you can directly pass attrs such as class and style through to the dialog container element.

<template>
	<div>
		<px-dialog
			title="Form"
			v-model:visible="visible"
			style="max-height: 70vh; width: 600px"
			@before-ok="submitHandler"
		>
			<px-form
				:model="form"
				:rules="rules"
				style="width: 500px"
				@submit="submitHandler"
				ref="formRef"
				poll-size-change
			>
				<px-form-item label="Input" field="input">
					<px-input v-model="form.input" placeholder="Please input..."></px-input>
				</px-form-item>
				<px-form-item label="Radio" field="radio">
					<px-radio-group
						v-model="form.radio"
						:options="radioOptions"
						variant="retro"
					></px-radio-group>
				</px-form-item>
				<px-form-item label="Checkbox" field="checkbox">
					<px-checkbox-group
						v-model="form.checkbox"
						:options="checkboxOptions"
					></px-checkbox-group>
				</px-form-item>
				<px-form-item label="Number" field="number">
					<px-input-number
						v-model="form.number"
						placeholder="Please input..."
					></px-input-number>
				</px-form-item>
				<px-form-item label="Tags" field="tags">
					<px-input-tag
						v-model="form.tags"
						placeholder="Press enter to confirm..."
					></px-input-tag>
				</px-form-item>
				<px-form-item label="Text" field="text">
					<px-textarea v-model="form.text" placeholder="Please input text..."></px-textarea>
				</px-form-item>
				<px-form-item label="Switch" field="switch">
					<px-switch v-model="form.switch"></px-switch>
				</px-form-item>
				<px-form-item label="Select" field="select">
					<px-select
						:options="options"
						v-model="form.select"
						placeholder="Please select..."
					></px-select>
				</px-form-item>
				<px-form-item label="Email" field="email">
					<px-input v-model="form.email" placeholder="Please email..."></px-input>
				</px-form-item>
				<px-form-item label="URL" field="url">
					<px-input v-model="form.url" placeholder="Please URL..."></px-input>
				</px-form-item>
				<px-form-item label="Slider" field="slider">
					<px-slider v-model="form.slider"></px-slider>
				</px-form-item>
				<px-form-item label="Number String" field="numberString">
					<px-input v-model="form.numberString" placeholder="Please number..."></px-input>
				</px-form-item>
			</px-form>
		</px-dialog>

		<px-button @click="visible = true" variant="plain">Open Form Dialog</px-button>
	</div>
</template>

<script setup lang="ts">
import { ref, shallowRef } from 'vue'
import { Form } from '@pixelium/web-vue'

// If on-demand import
// import { Form } from '@pixelium/web-vue/es'

const visible = ref(false)

const form = ref({
	input: '',
	number: 0,
	tags: [] as string[],
	text: '',
	select: null as null | string,
	email: '',
	url: '',
	numberString: '',
	switch: false,
	radio: null as null | string,
	checkbox: [] as string[],
	slider: 10
})

const rules = {
	input: { required: true, message: 'Please input' },
	select: { required: true, message: 'Please select' },
	switch: { required: true, message: 'Please select' },
	number: { min: 10, message: 'Min is 10' },
	tags: [
		{ required: true, message: 'Please input tags' },
		{ maxLength: 5, message: 'Length less than or equal 5' }
	],
	text: { required: true, message: 'Just a tip', level: 'warning' },
	email: { required: true, email: true, message: 'Please input email' },
	url: { required: true, url: true, message: 'Please input a URL' },
	numberString: { required: true, numberString: true, message: 'Please input number' },
	radio: {
		required: true,
		validator(value: string) {
			return value !== 'Yes' ? 'Please select Yes' : undefined
		}
	},
	checkbox: [
		{ required: true, message: 'Please select' },
		{ maxLength: 2, message: 'You can select up to 2 items' }
	],
	slider: { max: 90, message: 'Max is 90' }
}

const options = ref(['vue', 'react', 'angular'])
const radioOptions = ref(['Yes', 'No'])
const checkboxOptions = ref(['A', 'B', 'C', 'D'])

const formRef = shallowRef<null | InstanceType<typeof Form>>(null)
const submitHandler = () => {
	return formRef.value?.validate().then(({ isValid }) => {
		if (isValid) {
			console.log('submit')
			return true
		} else {
			return false
		}
	})
}
</script>

Functional Usage

If you have fully registered the component library, you can use it in the following ways:

  • window.$dialog;
  • Inside a Vue component: getCurrentInstance().appContext.config.globalProperties.PixeliumVue.dialog;
  • Import from @pixelium/web-vue.

When using on-demand import, you can also import Dialog from @pixelium/web-vue/es for usage.

<template>
	<px-space>
		<px-button @click="info" variant="outline">Info</px-button>
		<px-button @click="notice" theme="notice" variant="outline">Notice</px-button>
		<px-button @click="success" theme="success" variant="outline">Success</px-button>
		<px-button @click="warning" theme="warning" variant="outline">Warning</px-button>
		<px-button @click="error" theme="danger" variant="outline">Error</px-button>
		<px-button @click="confirm" theme="info" variant="outline">Confirm</px-button>
	</px-space>
</template>

<script setup lang="ts">
const info = () => {
	$dialog.info('This is an informational dialog.')
}
const notice = () => {
	$dialog.notice('This is an notice dialog.')
}
const success = () => {
	$dialog.success('This is a success dialog.')
}
const warning = () => {
	$dialog.warning('This is a warning dialog.')
}
const error = () => {
	$dialog.error('This is an error dialog.')
}
const confirm = () => {
	$dialog.confirm('Are you sure to proceed?').then((ok) => {
		if (ok) {
			$dialog.success('You have confirmed.')
		} else {
			$dialog.info('You have cancelled.')
		}
	})
}
</script>

Asynchronous Confirmation

When onBeforeOk is provided, the confirmation process will wait for onBeforeOk to complete. If the result is not false, subsequent code will be executed.

<template>
	<px-space>
		<px-button @click="confirm" theme="warning">Async Confirm</px-button>
	</px-space>
</template>

<script setup lang="ts">
const confirm = () => {
	$dialog
		.confirm({
			title: 'Async Confirm',
			content: 'Are you sure to proceed with async operation?',
			onBeforeOk() {
				return new Promise((resolve) => {
					setTimeout(() => {
						resolve(Math.random() > 0.5)
					}, 2000)
				})
			},
			okText: 'Random Succeed',
			cancelText: 'Cancel',
			okButtonProps: {
				theme: 'warning'
			}
		})
		.then((ok) => {
			if (ok) {
				$message.success('You have confirmed.')
			} else {
				$message.info('You have cancelled.')
			}
		})
}
</script>

API

Dialog Function

After calling a function on the Dialog, a Promise<boolean> is returned. If the dialog is closed via confirmation, the promise is fulfilled with true; otherwise, it is false.

ts
type DialogReturn = Promise<boolean> & {
	close: () => void
}

// ...
{
	[key in DialogOptions['type'] & string]: (
		options:
			| (Omit<DialogOptions, 'type'> & Omit<EmitEvent<DialogEvents>, 'update:visible'>)
			| string
	) => DialogReturn
}

Just as demonstrated in the example above example, when used as a component, you can directly pass attributes such as class and style through to the dialog container element.

DialogOptions

AttributeTypeOptionalDefaultDescriptionVersion
contentValidContentFalseDialog content.0.1.0
titleValidContentTrueDialog title.0.1.0
iconValidVNodeContentTrueDialog title icon.0.1.0
footerValidVNodeContentTrueDialog footer area.0.1.0
type'info' | 'success' | 'warning' | 'error' | 'normal' | 'confirm' | 'notice'True'normal'Dialog type.0.1.0
closablebooleanTruetrueWhether to display the close button in the top-right corner.0.1.0
maskbooleanTruetrueWhether to display the mask layer.0.1.0
maskClosablebooleanTruetrueWhether the dialog can be closed by clicking the mask layer.0.1.0
escToClosebooleanTruetrueWhether the dialog can be closed by pressing the ESC key (requires focus to be within the dialog).0.1.0
showCancelbooleanTrueWhether to display the cancel button. If not set, it is influenced by the type field—only displayed when the type is 'confirm'.0.1.0
okTextstringTrueText content of the confirm button.0.1.0
cancelTextstringTrueText content of the cancel button.0.1.0
showFooterbooleanTruetrueWhether to display the dialog footer area.0.1.0
zIndexnumberTrueThe z-index property of the dialog.0.1.0
rootstring | HTMLElementTrue'body'The root element to which the dialog is mounted.0.1.0
okButtonPropsButtonProps & EmitEvent<ButtonEvents> & RestAttrsTrueProperties of the confirm button.0.1.0
cancelButtonPropsButtonProps & EmitEvent<ButtonEvents> & RestAttrsTrueProperties of the cancel button.0.1.0
maskPropsOmit<MaskProps, 'zIndex'>TrueProperties of the mask layer.0.1.0
containerPropsRestAttrsTrueProperties of the dialog container element.0.1.0
headerPropsRestAttrsTrueProperties of the dialog header area element.0.1.0
bodyPropsRestAttrsTrueProperties of the dialog content area element.0.1.0
footerPropsRestAttrsTrueProperties of the dialog footer area element.0.1.0

DialogProps

AttributeTypeOptionalDefaultDescriptionVersion
visibleboolean | nullTrueControls the display of the dialog (controlled mode), supports v-model.0.1.0
visibleboolean | nullTrueControls the display of the dialog (controlled mode), supports v-model.0.1.0
titlestringTrue''Dialog title.0.1.0
closablebooleanTruetrueWhether to display the close button in the top-right corner.0.1.0
maskbooleanTruetrueWhether to display the mask layer.0.1.0
maskClosablebooleanTruetrueWhether the dialog can be closed by clicking the mask layer.0.1.0
escToClosebooleanTruetrueWhether the dialog can be closed by pressing the ESC key (requires focus to be within the dialog).0.1.0
showCancelbooleanTruetrueWhether to display the cancel button. If not set, it is influenced by the type field—only displayed when the type is 'confirm'.0.1.0
okTextstringTrueText content of the confirm button.0.1.0
cancelTextstringTrueText content of the cancel button.0.1.0
booleanbooleanTruefalse0.1.0
showFooterbooleanTruetrueWhether to display the dialog footer area.0.1.0
rootstring | HTMLElementTrue'body'The root element to which the dialog is mounted.0.1.0
zIndexnumberTrueThe z-index property of the dialog.0.1.0
destroyOnHidebooleanTruefalseWhether the dialog content is destroyed after hiding.0.1.0
okButtonPropsButtonProps & EmitEvent<ButtonEvents> & RestAttrsTrueProperties of the confirm button.0.1.0
cancelButtonPropsButtonProps & EmitEvent<ButtonEvents> & RestAttrsTrueProperties of the cancel button.0.1.0
maskPropsOmit<MaskProps, 'zIndex'>TrueProperties of the mask layer.0.1.0
containerPropsRestAttrsTrueProperties of the dialog container element.0.1.0
headerPropsRestAttrsTrueProperties of the dialog header area element.0.1.0
bodyPropsRestAttrsTrueProperties of the dialog content area element.0.1.0
footerPropsRestAttrsTrueProperties of the dialog footer area element.0.1.0

DialogSlots

SlotParameterDescriptionVersion
defaultDialog content.0.1.0
titleDialog title.0.1.0
iconDialog title icon.0.1.0
footerDialog footer area.0.1.0

DialogEvents

EventParameterDescriptionVersion
update:visibleUsed to update the visible value in controlled mode.0.1.0
beforeOkFor async confirmation. Must return a value of type Promise<boolean | void> | boolean | void to indicate success. Confirmation is successful if the result after await is not false.0.1.0
okevent: MouseEventCallback triggered when the dialog is successfully closed via confirmation.0.1.0
cancelevent: MouseEvent | KeyboardEventCallback triggered when the dialog is closed via cancellation, close button, mask layer, ESC key, etc.0.1.0
openCallback triggered when the dialog opens.0.1.0
afterOpenCallback triggered after the dialog opening animation ends.0.1.0
closeCallback triggered when the dialog closes.0.1.0
afterCloseCallback triggered after the dialog closing animation ends.0.1.0

DialogExpose

AttributeTypeOptionalDefaultDescriptionVersion
close() => voidFalseClose the dialog.0.1.0
open() => voidFalseOpen the dialog.0.1.0

ValidContent, ValidVNodeContent

ts
export type ValidContent = string | ((...args: any[]) => VNode | string | JSX.Element | null | void)
export type ValidVNodeContent = (...args: any[]) => VNode | JSX.Element

RestAttrs

ts
import type { StyleValue } from 'vue'

export type VueClassValue = string | Record<string, any> | VueClassValue[]
export type VueStyleValue = StyleValue

export type RestAttrs = {
	style?: VueStyleValue | null
	class?: VueClassValue | null
	[x: string]: any
}

EmitEvent

ts
export type EmitEvent<T extends Record<string, any>> = {
	[K in keyof T as `on${Capitalize<K & string>}`]?: (...args: T[K]) => void
}