Dialog
The basics of the dialog plugin for Aurelia.
Introduction
This article covers the dialog plugin for Aurelia. This plugin is created for showing dialogs (sometimes referred to as modals) in our application. The plugin supports the use of dynamic content for all aspects and is easily configurable / overridable.
Here's what you'll learn...
How to install & configure the plugin
How to use default dialog service
How to enhance & replace parts of the default implementations
The lifeycle of a dialog
Installing The Plugin
There's a set of default implementations for the main interfaces of the Dialog plugin, which includes:
IDialogService
IDialogGlobalSettings
IDialogDomRenderer
IDialogEventManager
IDialogAnimator
(only used by the defaultDialogDomRenderer
)
These default implementation are grouped in the export named DialogDefaultConfiguration
of the dialog plugin, which can be used per the following:
Configuring the Plugin
The export DialogDefaultConfiguration
is a preset of default behaviors & implementations that are done in a way suitable to most common scenarios.
If it's desirable to change some of the behaviors or implementations of we can either change the first or the 2nd parameter of the customize
function on this object.
An example of changing the behavior for configuring the global settings is:
If it's desirable to change some of the default implementations, we can instead use the export named DialogConfiguration
and pass in the list of implementation for the main interfaces:
If there's a need to only swap some implementation, say IDialogDomRenderer
for example, then the default implementation can be imported and mixed like the following example:
Using The Default Implementation
The Dialog Settings
There are two levels where dialog behavior can be configured:
Global level via
IDialogGlobalSettings
Single dialog level via property
settings
on a dialog controller.
Normally, the global settings would be changed during the app startup/or before, while the single dialog settings would be changed during the contruction of the dialog view model, via the open
method.
An example of configuring the global dialog settings:
Make all dialogs, by default:
not dismissable by clicking outside of it, or hitting the ESC key
have starting CSS
z-index
of 5if not locked, closable by hitting the
ESC
key
An example of configuring a single dialog, via open
method of the dialog service:
Displaying an alert dialog, which has z-index
value as 10 to stay on top of all other dialogs, and will be dismissed when the user hits the ESC
key.
The settings that are available in the open
method of the dialog service:
component
can be class reference or instance, or a function that resolves to such, or a promise of such.template
can be HTML elements, string or a function that resolves to such, or a promise of such.model
the data to be passed to thecanActivate
andactivate
methods of the view model if implemented.host
allows providing the element which will parent the dialog - if not provided the document body will be used.renderer
allows providing a custom renderer to be used, instead of the pre-registered one. This allows a single dialog service to be able to use multiple renderers for different purposes: default for modals, and a different one for notifications, alert etc...container
allows specifying the DI Container instance to be used for the dialog. If not provided a new child container will be created from the root one.lock
makes the dialog not dismissable via clicking outside, or using keyboard.keyboard
allows configuring keyboard keys that close the dialog. To disable set to an empty array[]
. To cancel close a dialog when the ESC key is pressed set to an array containing'Escape'
-['Escape']
. To close with confirmation when the ENTER key is pressed set to an array containing'Enter'
-['Enter']
. To combine the ESC and ENTER keys set to['Enter', 'Escape']
- the order is irrelevant. (takes precedence overlock
)overlayDismiss
if set totrue
cancel closes the dialog when clicked outside of it. (takes precedence overlock
)rejectOnCancel
is a boolean that must be set totrue
if cancellations should be treated as rejection. The reason will be anIDialogCancelError
- the propertywasCancelled
will be set totrue
and if cancellation data was provided it will be set to thevalue
property.
The default global settings has the following values:
lock
is truestartingZIndex
is1000
rejectOnCancel
isfalse
If rejectOnCancel
behavior is desired, it should only be applied to individual dialog via open
method of the dialog service.
The Dialog Service APIs
The interface that a dialog service should follow:
The interface that a dialog controller should follow:
An important feature of the dialog plugin is that it is possible to resolve and close (using cancel
/ok
/error
methods) a dialog in the same context where it's open.
Example of controlling the opening and closing of a dialog in promise style:
Example of controlling the opening and closing of a dialog using
async/await
:
By default, when an application is destroyed, the dialog service of that application will also try to close all the open dialogs that are registered with it via closeAll
method. It can also be used whenever there's a need to forcefully close all open dialogs, as per following example:
Given an error list, open a dialog for each error, and close all of them after 5 seconds.
If there's no need for the opening result of a dialog, and only the response of it after the dialog has been closed, there is a whenClosed
method exposed on the returned promise of the open
method of the dialog service, that should help reduce some boilerplate code, per following example:
Template Only Dialogs
The dialog service supports rendering dialogs with only template specified. A template only dialog can be open like the following examples:
Retrieving the dialog controller
By default, the dialog controller of a dialog will be assigned automatically to the property $dialog
on the component view model. To specify this in TypeScript, the component class can implement the interface IDialogCustomElementViewModel
:
Note that the property $dialog
will only be ready after the contructor.
If it's desirable to retrieve the associated dialog controller of a dialog during the constructor of the component, IDialogController
can be inject to achieve the same effect:
This means it's also possible to control the dialog from template only dialog via the $dialog
property. An example of this is: Open an alert dialog, and display an "Ok" button to close it, without using any component:
The Default Dialog Renderer
By default, the dialog DOM structure is rendered as follow:
The Dialog host element is the target where an application chooses to add the dialog to, this is normally the document body, if not supplied in the settings of the open
method of the dialog service.
An example of the html structure when document body is the dialog host:
Centering/Uncentering dialog position
By default, the dialog content host is centered horizontally and vertically. It can be changed via IDialogDom
injection:
Note that the contentHost
property on a DefaultDialogDom
object is the same with the host element of a component. You can inject IDialogDom
and retrieve the host element via contentHost
property, or inject INode
/Element
/HTMLElement
to retrieve it.
Styling the overlay
By default, the overlay of a dialog is transparent. Though it's often desirable to add 50% opacity and a background color of black to the modal. To achieve this in dialog, retrieve the IDialogDom
instance and modify the overlay
element style
:
Use your own Dialog Renderer
There are two ways to use your own dialog renderer: register your own default dialog renderer, or specify the renderer via renderer
setting of dialog service .open()
.
To register your own default dialog renderer, you can follow the below example:
Notice the returned object of the
render()
method, it should satisfy the interface of anIDialogDom
:To specify the renderer via
renderer
setting in the dialog service open call, you can follow the below example:Notice the object given to the
renderer
property, it should satisfy the interface of anIDialogDomRenderer
:
Animation
Using the IDialogDomAnimator
IDialogDomAnimator
If you use the default implementation of the interface IDialogRenderer
, then the interface IDialogDomAnimator
can be used to register an animator responsible for animating the default dialog dom.
An IDialogDomAnimator
implementation should satisfy the following interface:
An example implementation would look like the following:
If you wished to use a different animators for different dialogService.open()
calls (modal vs notification etc...), you can do so via the container option in the dialogService.open()
call, per the following example:
Using component lifecycles
The lifecycles attaching
and detaching
can be used to animate a dialog, as in those lifecycles, if a promise is returned, it will be awaited during the activation/deactivation phases.
An example of animating a dialog on attaching and detaching, with the animation duration of 200 milliseconds:
The Default Dialog Event Manager
Any implemetattion of IDialogEventManager
should follow the following interface:
The default implementation of IDialogEventManager
is DefaultDialogEventManager
. It will listen to the keyboard keydown
event on the window object, and close off the last open dialog. It's also responsible for adding a click listener to the overlay of a dialog dom to close off the associated dialog.
Using your own dialog event manager
If you wish to handle dialog events differently, you can register your implementation with the interface key IDialogEventManager
, like the following examples:
Component Lifecycles With The Dialog Plugin
In adition to the lifecycle hooks defined in the core templating, the dialog
defines additional ones. All dialog specific hooks can return a Promise
, that resolves to the appropriate value for the hook, and will be awaited.
.canActivate()
.canActivate()
This hook can be used to cancel the opening of a dialog. It is invoked with one parameter - the value of the model
setting passed to .open()
. To cancel the opening of the dialog return false
- null
and undefined
will be coerced to true
.
.activate()
.activate()
This hook can be used to do any necessary init work. The hook is invoked with one parameter - the value of the model
setting passed to .open()
.
.canDeactivate(result: DialogCloseResult)
.canDeactivate(result: DialogCloseResult)
This hook can be used to cancel the closing of a dialog. To do so return false
- null
and undefined
will be coerced to true
. The passed in result parameter has a property status
, indicating if the dialog was closed or cancelled, or the deactivation process itself has been aborted, and an value
property with the dialog result which can be manipulated before dialog deactivation.
The DialogCloseResult
has the following interface (simplified):
Warning When the
error
method of aDialogController
is called this hook will be skipped.
.deactivate(result: DialogCloseResult)
.deactivate(result: DialogCloseResult)
This hook can be used to do any clean up work. The hook is invoked with one result parameter that has a property status
, indicating if the dialog was closed (Ok
) or cancelled (Cancel
), and an value
property with the dialog result.
Order of Invocation
Each dialog instance goes through the full lifecycle once.
--- activation phase:
constructor()
.canActivate()
-dialog
specific.activate()
-dialog
specifichydrating
hydrated
.created()
.binding()
.bound()
attaching
attached
--- deactivation phase:
.canDeactivate()
-dialog
specific.deactivate()
-dialog
specific.detaching()
.unbinding()
V1 Dialog Migration
viewModel
setting inDialogService.prototype.open
is changed tocomponent
.view
setting inDialogService.prototype.open
is changed totemplate
.keyboard
setting inDialogService.prototype.open
is changed to accept an array ofEnter
/Escape
only. Boolean variants are no longer valid. In the future, the API may become less strict.The resolved of
DialogService.prototype.open
is changed from:to:
closeResult
is removed from the returned object. Usesclosed
property on the dialog controller instead, example of open a dialog with hello world text, and automaticlly close after 2 seconds:The interface of dialog close results is changed from:
to:
The dialog controller is assigned to property
$dialog
(v2) on the view model, instead of propertycontroller
(v1)
TODO: links to advanced examples/playground
Last updated