Appearance
Plugin Usage
The plugin for the Gitart Dialog is completely optional. It's only needed if you want adding dialogs by the method addDialog not including the dialog in the template of some component.
Installation
// main.js
import { plugin as dialogPlugin } from 'gitart-vue-dialog'
const app = Vue.createApp(/* ... */)
app.use(dialogPlugin, {
// options
})
Plugin options
closeDelay
Type:
NumberDefault:
500Details:
It's time in milliseconds after the dialog is closed when the dialogs are removed from the DOM. If you want a long leaving transition, set this value higher.
props
Type:
ObjectDefault:
{}Details:
It's default props for all dialogs if you run them byaddDialogmethod.app.use(dialogPlugin, { props: { maxWidth: '300px', // ... }, }) // You can override it $dialog.addDialog({ component: InfoDialog, props: { maxWidth: '500px', }, })// It works like this const defaultProps = options?.props ?? {} const $dialog = { addDialog: ({ component, props, id }, hooks) => { const dialogId = id ?? Date.now() + Math.random() dialogs.push({ component, id: dialogId, props: reactive({ modelValue: true, ...defaultProps, ...props, }), onClose: hooks?.onClose, }) return dialogId }, }
GDialogRoot
We strongly recommend using GDialogRoot. It renders your dialogs. Put it somewhere at the end of the App.vue.
Or write your own alternative. Look here how GDialogRoot works
<template >
<GDialogRoot />
</template>
<script>
import { defineComponent } from 'vue'
import { GDialogRoot } from 'gitart-vue-dialog'
export default defineComponent({
components: {
GDialogRoot,
},
})
</script>
Usage
There are two ways to access the methods and properties of the plugin (option and composition api). See below:
Option api:
In any method you have access to this.$dialog.
import { defineComponent } from 'vue'
export default defineComponent({
methods: {
someMethod() {
// this.$dialog
// $dialog.addDialog()
// $dialog.removeDialog()
// $dialog.dialogs
},
},
})
Composition api
Make injection inside you setup function by inject and dialogInjectionKey.
import { inject } from 'vue'
import { dialogInjectionKey } from 'gitart-vue-dialog'
import { defineComponent, inject } from 'vue'
import { dialogInjectionKey } from 'gitart-vue-dialog'
export default defineComponent({
setup() {
const $dialog = inject(dialogInjectionKey)
// $dialog.addDialog()
// $dialog.removeDialog()
// $dialog.dialogs
}
})
To know how all these properties work, look below.
Properties
$dialog has such properties:
| Name | Type |
|---|---|
| addDialog | Function |
| removeDialog | Function |
| dialogs | Array |
addDialog(data, hooks)
Type:
FunctionArguments:
- {Object} data -
{component: DialogComponent, props?: { ... }, id?: string | number } - {Object} hooks -
{ onClose: (event) => void }
- {Object} data -
Returns:
Number- dialog idDetails:
The method adds your extendeddataargument to dialogs array. Thedata.componentwill be rendered in the GDialogRoot with the props you add todata.propsOptionaly you can add
idto thedataargument. It's useful if you want to remove the dialog by id.const dialogId = $dialog.addDialog({ component: InfoDialog, props: { maxWidth: '500px', }, id: 'my-dialog-id', }) // ... $dialog.removeDialog(dialogId)The
hooksargument is an object with theonClosemethod. It's called when the dialog is closing. It allows you to do something before the dialog is closed or cancel the closing.$dialog.addDialog({ component: InfoDialog, props: { maxWidth: '500px', }, }, { onClose: (event) => { event.cancel() // cancel the closing event.id // dialog id event.item // dialog item from $dialog.dialogs (read more below) }, })WARNING
data.propsshould not contain modelValue. TheaddDialogoverwrites itINFO
To close the dialog (DialogComponent.vue) you need inside emit the event
update:modelValuewithfalseinside. Take a look at the example below
removeDialog(id)
Type:
FunctionArguments:
- {Number} id
Details:
The method removes item from dialogs by id. Useful if you want to close the dialog programmatically.const dialogId = $dialog.addDialog({ component: InfoDialog, props: { maxWidth: '500px', }, id: 'my-dialog-id', }) // ... $dialog.removeDialog(dialogId)With it you can also write your own GDialogRoot
dialogs
Type:
ArrayDetails:
Array of the IDialogItem. The array can be rendered in any part of your app. We recommend use GDialogRoot for it.interface IDialogItem { component: ShallowUnwrapRef<Component> id: number props: { modelValue: boolean // other props } onClose?: (event: { cancel: () => void id: number item: IDialogItem }) => void }
Example
INFO
In this example, we assume that you installed the GDialog globally and added GDialogRoot to your root component (like App.vue)
import {
GDialog,
plugin as dialogPlugin
} from 'gitart-vue-dialog';
import 'gitart-vue-dialog/dist/style.css';
const app = Vue.createApp(/* ... */)
app.use(dialogPlugin)
app.component('GDialog', GDialog)
First, create your Dialog component to launch it from a method. Let's name it InfoDialog.vue
<GDialog v-model="dialogState" max-width="300">
<div>
Info Alert
</div>
{{ info }}
</GDialog>
import { computed, defineComponent } from 'vue'
export default defineComponent({
props: {
modelValue: {
type: Boolean,
default: false,
},
info: {
type: String,
required: true,
},
},
emits: ['update:modelValue'],
setup(props, { emit }) {
const dialogState = computed({
get() {
return props.modelValue
},
set(value) {
emit('update:modelValue', value)
},
})
return {
dialogState,
}
},
})
Well. Now let's create a component where we launch InfoDialog. Name it Launcher.vue. There will be only a button in the template
<button @click="onOpen">Open</button>
import { inject, defineComponent } from 'vue';
import { dialogInjectionKey } from 'gitart-vue-dialog';
import InfoDialog from './InfoDialog.vue';
export default defineComponent({
setup() {
const $dialog = inject(dialogInjectionKey);
const onOpen = () => {
$dialog.addDialog({
component: InfoDialog,
props: {
info: 'Some kind of message from outside InfoDialog',
},
}, {
onClose: (event) => {
console.log('Dialog is closing')
console.log(event)
// event.cancel()
},
});
};
return {
onOpen,
};
},
});
Live Example
See live example here (stackblitz.com)
Wait until all dependencies will be installed and run npm run dev in the terminal below.
As you see, you don't need to insert <InfoDialog /> into Launcher.vue. Now, we can even put onOpen method in a separate file and reuse it everywhere. Here is an example with using composable and composition api.