Dialog2
Summary
Modal dialogs are used to get a response from a user or reveal extra information related to a given scenario.
Usage
For the best practices on usage please refer to Atlas Kit Dialog usage documentation.
For the best practices on accessibility please refer to WAI Guidelines on Dialog.
There are also Dialog A11y specifications for AUI available inside of Atlassian.
Status
API status: | general |
---|---|
Web resource key: |
com.atlassian.auiplugin:aui-dialog2
|
AMD Module key: |
require('aui/dialog2')
|
Experimental API: | 5.3 |
General API: | 5.8 |
Examples
Anatomy of a dialog
Below you can see an overview of the dialog HTML pattern.
Result
The modal dialog title
Content for the modal dialog.
<!--
Renders a static dialog.
To ensure the dialog is not rendered inline in the page, add:
* class="aui-layer"
* hidden
to this element.
-->
<section
id="static-dialog"
class="aui-dialog2 aui-dialog2-medium"
role="dialog"
aria-modal="true"
tabindex="-1"
aria-labelledby="static-dialog--heading"
aria-describedby="static-dialog--description"
>
<!-- Dialog header -->
<header class="aui-dialog2-header">
<!-- The dialog's title -->
<h1 class="aui-dialog2-header-main" id="static-dialog--heading">The modal dialog title</h1>
</header>
<!-- Main dialog content -->
<div class="aui-dialog2-content" id="static-dialog--description">
<p>Content for the modal dialog.</p>
</div>
<!-- Dialog footer -->
<footer class="aui-dialog2-footer">
<!-- Actions to render on the right of the footer -->
<div class="aui-dialog2-footer-actions">
<button class="aui-button aui-button-primary">Okay</button>
<button class="aui-button" autofocus>Next</button>
<button class="aui-button aui-button-link">Close</button>
</div>
<!-- Hint text is rendered on the left of the footer -->
<div class="aui-dialog2-footer-hint">This is a hint.</div>
</footer>
</section>
On its own, the dialog HTML pattern itself is static — the contents are added to the page inline.
To ensure the dialog does not get rendered to the page, you should add aui-layer
class
and hidden
attribute to the dialog element.
Note that in the footer, the hint text (class="aui-dialog2-footer-hint"
) should be placed
in the DOM below the footer actions (class="aui-dialog2-footer-actions"
) even though the hint text
appears to the left of the footer actions.
Initial focus management
When working with Dialogs initial focus is one of the most important things impacting user experience.
This is how Dialog2 handles the focus:
- find autofocus attributes in Dialog and focus the first one if found
- if there are no autofocus attributes, find all focusable controls and focus the first one
- if still nothing was focused, focus Dialog container element
When adding Dialog to the page, you need to think about what would be the best initial focus element in this specific Dialog. If it's not focused by default, you should consider adding autofocus attribute to it, or restructuring Dialog contents.
If you are not sure what is the best approach in your case, please refer to guides from Usage section of the page.
Opening and closing a dialog
By default the dialog closes after the close button or the blanket is clicked.
To make the dialog open and close in other cases you can use .show
and .hide
methods.
Result
Captain...
We've detected debris of some sort in a loose orbit.
I suggest we beam a section aboard for analysis...
<!-- Create a trigger which will be used by the JavaScript -->
<button id="dialog-show-button" class="aui-button">Show dialog</button>
<section
id="demo-dialog"
class="aui-dialog2 aui-dialog2-small aui-layer"
role="dialog"
tabindex="-1"
aria-modal="true"
aria-labelledby="dialog-show-button--heading"
aria-describedby="dialog-show-button--description"
hidden
>
<header class="aui-dialog2-header">
<h1 class="aui-dialog2-header-main" id="dialog-show-button--heading">Captain...</h1>
</header>
<div class="aui-dialog2-content" id="dialog-show-button--description">
<p>We've detected debris of some sort in a loose orbit.</p>
<p>I suggest we beam a section aboard for analysis...</p>
</div>
<footer class="aui-dialog2-footer">
<div class="aui-dialog2-footer-actions">
<button id="dialog-submit-button" class="aui-button aui-button-primary">Make it so</button>
</div>
</footer>
</section>
// Shows the dialog when the "Show dialog" button is clicked
AJS.$("#dialog-show-button").on('click', function(e) {
e.preventDefault();
AJS.dialog2("#demo-dialog").show();
});
// Hides the dialog
AJS.$("#dialog-submit-button").on('click', function (e) {
e.preventDefault();
AJS.dialog2("#demo-dialog").hide();
});
Focus management after dialog is closed
By default after the dialog is closed the focus returns to the element that was focused before it was opened.
However there are cases in which you have to explicitly set the focus after the dialog is closed,
e.g. the element focused before dialog opening no longer exists in the document.
While deciding which element should be focused keep in mind the natural flow of operation and user journey.
Subscribe to hide
event to achieve the required behaviour.
// Listen for hide event of #demo-dialog and focus target element
AJS.dialog2("#demo-dialog").on('hide', function (e) {
$("#new-active-element").focus()
});
A warning dialog
Use this dialog type when you're representing a destructive action, and want the end-user to think more carefully about how they proceed.
Result
Confirm you want to delete this thing
If you do this, there's no going back. Are you certain that you want this thing to be gone forever?
<button id="warning-dialog-show-button" class="aui-button">Show warning dialog</button>
<section
id="demo-warning-dialog"
class="aui-dialog2 aui-dialog2-medium aui-dialog2-warning aui-layer"
role="dialog"
tabindex="-1"
aria-modal="true"
aria-labelledby="demo-warning-dialog--heading"
aria-describedby="demo-warning-dialog--description"
hidden
>
<header class="aui-dialog2-header">
<h1 class="aui-dialog2-header-main" id="demo-warning-dialog--heading">Confirm you want to delete this thing</h1>
</header>
<div class="aui-dialog2-content" id="demo-warning-dialog--description">
<p>If you do this, there's no going back. Are you certain that you want this thing to be gone forever?</p>
</div>
<footer class="aui-dialog2-footer">
<div class="aui-dialog2-footer-actions">
<button id="warning-dialog-confirm" class="aui-button aui-button-primary">Delete the thing</button>
<button id="warning-dialog-cancel" class="aui-button aui-button-link">Cancel</button>
</div>
</footer>
</section>
// Shows the warning dialog when the "Show warning dialog" button is clicked
AJS.$("#warning-dialog-show-button").on('click', function (e) {
e.preventDefault();
AJS.dialog2("#demo-warning-dialog").show();
});
AJS.$(document).on("click", "#demo-warning-dialog button", function (e) {
e.preventDefault();
AJS.dialog2("#demo-warning-dialog").hide();
});
Options
HTML attributes
Dialog2 configuration options are expressed through markup.
Class | Description | Example Usage |
---|---|---|
aui-dialog2-small | aui-dialog2-medium | aui-dialog2-large | aui-dialog2-xlarge |
Controls the size of the dialog according to ADG size specifications. |
<section class="aui-dialog2 aui-dialog2-small" role="dialog" hidden> <!-- inner content --> </section> |
.aui-dialog2-warning |
Gives the dialog's header a red background color. |
<section class="aui-dialog2 aui-dialog2-warning" role="dialog" hidden> <!-- inner content --> </section> |
Attribute | Values | Description | Example Usage |
---|---|---|---|
data-aui-modal |
true |
Specifies that the dialog is modal. Modal dialogs have no close icon in the top right corner, and cannot be closed by clicking on the blanket behind it. |
<section class="aui-dialog2" data-aui-modal="true" role="dialog" hidden> <!-- inner content --> </section> |
data-aui-remove-on-hide |
true |
Specifies that the dialog element should be removed from the DOM when it is hidden, either by clicking on the close icon, clicking on the blanket behind the dialog, or calling the hide() method. |
<section class="aui-dialog2" data-aui-remove-on-hide="true" role="dialog" hidden> <!-- inner content --> </section> |
data-aui-focus-selector Deprecated |
selector |
Controls the element that is focussed when the dialog is opened. By default the focus goes to the dialog element itself to meet the accessibility expectations in most common cases. |
<section class="aui-dialog2" data-aui-focus-selector=".aui-dialog2-content :input:visible:enabled" role="dialog" hidden> <!-- inner content --> </section> |
JavaScript API
To get a reference to the API for a dialog2 instance, call AJS.dialog2(selector), where selector can be a selector string, DOM node, or jQuery element.
var demoDialog = AJS.dialog2("#demo-dialog");
Methods
Method | Description | Example Usage |
---|---|---|
show |
Shows a dialog. |
AJS.dialog2("#demo-dialog").show(); |
hide |
Hides a dialog. |
AJS.dialog2("#demo-dialog").hide(); |
remove |
Removes the dialog from the DOM. |
AJS.dialog2("#demo-dialog").remove(); |
Events
Events are triggered when dialogs are shown or closed. These can be listened to for a single dialog instance, or for all dialogs.
Event | Description | Example Usage |
---|---|---|
show |
Triggered when a dialog instance is shown. | AJS.dialog2("#demo-dialog").on("show", function() { console.log("demo-dialog was shown"); }); |
hide |
Triggered when a dialog instance is hidden. | AJS.dialog2("#demo-dialog").on("hide", function() { console.log("demo-dialog was hidden"); }); |
global show |
Triggered when any dialog is shown. | AJS.dialog2.on("show", function() { console.log("a dialog was shown"); }); |
global hide |
Triggered when any dialog is hidden. | AJS.dialog2.on("hide", function() { console.log("a dialog was hidden"); }); |