Dialog
Quasar Dialogs are a great way to offer the user the ability to choose a specific action or list of actions. They also can provide the user with important information, or require them to make a decision (or multiple decisions).
From a UI perspective, you can think of Dialogs as a type of “floating” modal, which covers only a portion of the screen. This means Dialogs should only be used for quick actions, like password verification, small App notifications or quick options. More in depth user flows should be reserved for Modals.
Basic Usage
import { Dialog } from 'quasar' |
WARNING
Dialog is meant to be imported and used directly.
It is not a Vue component and should not be declared in components object.
A real life example:import { Dialog } from 'quasar'
Dialog.create({
title: 'Warning',
message: 'You are about to run out of disk space.',
buttons: [
'Cancel',
{
label: 'Empty Trash Bin',
handler: () => {
// empty the trash bin, yo
// Note the ES6 arrow function in the handler definition.
// See the `Tip on using "this" in the handler` below for an explanation
}
}
]
})
IMPORTANT
When the user hits the browser/phone/tablet back button, the Dialog will be closed automatically..
Also, when on a browser, hitting the <ESCAPE> key also closes the Dialog.
Dialog Config Object
In order to create a Dialog, you’ll need an object as a parameter to configure it (configObj above). Below are the properties of this object (all properties are optional):
Property Name | Type | Description |
---|---|---|
title | String | Title of the Dialog. |
message | String | Additional message below the title. |
position | String | Optional. One of ‘top’, ‘right’, ‘bottom’, ‘left’. |
form | Object | Configure what types of form components to show. |
buttons | Array of Objects | Bottom buttons for the Dialog. Regardless of the handler that you specify, each button closes the Dialog. You can also specify a String instead of an Object as part of your Array for buttons that only closes the Dialog. |
stackButtons | Boolean | If you want your buttons placed one below the previous one instead of in the same row. |
noButtons | Boolean | When you don’t want any buttons on your Dialog. By default, if no buttons are specified, an “OK” button is added. This property avoids this default addition. |
progress | Object | When you want to make your Dialog display a progress bar. Check Progress Dialog below. |
onDismiss | Function | Function to be called when the Dialog gets closed (or dismissed). |
noBackdropDismiss | Boolean | If set true, the Dialog cannot be dismissed by clicking/tapping on backdrop. |
noEscDismiss | Boolean | If set true, the Dialog cannot be dismissed by hitting Escape key. |
Tip on using “this” in handlers
A common use case is this scenario in script part of a Vue file, where you’d like the button handler to be able to access the Vue component scope with this
.export default {
...,
methods: {
data () {
// defining a variable for this example
// to highlight how you can access it
// later when we call the Dialog
return {
variable: 'alright'
}
},
showDialog () {
// "this" here refers to current component
// prints: "alright"
console.log(this.variable)
Dialog.create({
title: 'Some title',
message: 'Some message',
buttons: [
'Cancel',
{
label: 'Empty Trash Bin',
handler () {
// "this" refers to the scope of this method only,
// not your Vue component
// prints: undefined
console.log(this.variable)
}
}
]
})
}
}
}
What you need to do is use ES6 arrow functions instead:// instead of:
handler () { .... }
// use this:
handler: () => {
// now "this" points to the method's outer JS scope,
// in this case your Vue component scope
// correctly prints out "alright"
console.log(this.variable)
}
Since using ES6, it’s time to forget about temporary variables in your code, like in the following example:// use ES6 arrow function instead!
// it's much cleaner
var self = this
Dialog.create({
...,
buttons: [{
handler () {
console.log(self.variable)
}
}]
})
Progress Dialog
There are two types of progress bars you can display in a Dialog: determinate (when you can quantify the progress) or indeterminate (when you don’t know the moment the progress will be done).
Determinate Mode
import { Dialog, Toast } from 'quasar' |
Indeterminate Mode
import { Dialog, Toast } from 'quasar' |
Dialog with Form Components
You can combine multiple form components (textfields, chips, radios, checkboxes, …), to configure your Dialog through the form
Object property.
Each property of form
is an Object itself. The key will be used later, when the user closes the Dialog. For example:{form: {name: {...}}}
// will pass a "name" property later:
handler (data) {
// data.name --> model of your form component
}
Each Form Component has a certain syntax that you must follow as described below. At the end, you’ll learn how to separate these into sections by using a “heading”.
The default value for the following Form Components is taken from the
model
property.
Textfields
Supported types: ‘text’, ‘textarea’, ‘email’, ‘tel’, ‘file’, ‘number’, ‘password’, ‘url’, ‘chips’:import { Dialog, Toast } from 'quasar'
Dialog.create({
title: 'Prompt',
form: {
name: {
type: 'text',
label: 'Textbox',
model: ''
},
pass: {
type: 'password',
label: 'Password',
model: ''
},
age: {
type: 'number',
label: 'Numeric',
model: 10,
min: 1,
max: 100
},
tags: {
type: 'chips',
label: 'Chips',
model: ['Joe', 'John']
},
comments: {
type: 'textarea',
label: 'Textarea',
model: ''
}
},
buttons: [
'Cancel',
{
label: 'Ok',
handler (data) {
Toast.create('Returned ' + JSON.stringify(data))
// data.name is 'Quasar'
// data.pass is 'rulz!'
// data.age is 1
// data.tags is ['Joe', 'John'],
// data.comments is 'Some comments...'
}
}
]
})
Radios
import { Dialog, Toast } from 'quasar' |
Checkboxes & Toggles
import { Dialog, Toast } from 'quasar' |
Slider & Range
import { Dialog, Toast } from 'quasar' |
Rating
import { Dialog, Toast } from 'quasar' |
Headings
Since you can combine different components within the Dialog, sometimes you may need to separate them into sections. For this purpose Quasar offers the “heading” type:import { Dialog, Toast } from 'quasar'
Dialog.create({
...,
form: {
header1: {
type: 'heading',
label: 'Checkboxes'
},
group1: {
type: 'checkbox',
items: [...]
},
...,
header2: {
type: 'heading',
label: 'Toggles'
},
group2: {
type: 'toggle',
items: [...]
},
...
},
buttons: [
'Cancel',
{
label: 'Ok',
handler (data) {
Toast.create('Returned ' + JSON.stringify(data))
}
}
]
})
See demo with “Multiple Selections”.
More Examples
Alert
import { Dialog } from 'quasar' |
Confirm
import { Dialog } from 'quasar' |
Styling Buttons
Dialog.create({ |
Stacked Buttons
If you have many buttons or buttons with lots of text, you can set the stackButtons
property to true
when creating your Dialog. This will display your in separate rows:import { Dialog } from 'quasar'
Dialog.create({
// ...
stackButtons: true,
buttons: [......]
})
Prevent Closing the Dialog
You can prevent a button from closing the Dialog. This is useful, when you need to do some validations on form fields.
Add preventClose: true
to the button definition. This will make the handler()
method receive a second parameter, which when called, closes the Dialog. Not calling it obviously keeps the Dialog opened.import { Dialog, Toast } from 'quasar'
Dialog.create({
title: 'Prevent Close',
message: 'Having "Prevent" checkbox ticked and then hitting "Try to Close" button will prevent the dialog from closing.',
form: {
prevent: {
type: 'checkbox',
model: ['prevent'],
items: [
{label: 'Prevent dialog close', value: 'prevent'}
]
}
},
buttons: [
{
label: 'Try to Close',
preventClose: true,
handler (data, close) {
if (!data.prevent.length) {
close(() => {
Toast.create(`Finally. It's closed now.`)
})
return
}
Toast.create('Untick "Prevent" checkbox to be able to close the Dialog.')
}
}
]
})
Complex Dialog with Form
import { Dialog, Toast } from 'quasar' |
Sticking Dialog to an Edge
import { Dialog } from 'quasar' |