Dialog
<ahi-dialog> | AhiDialog
Dialogs, sometimes called “modals” are overlays that require users to take an action before they can continue interacting with the rest of the application. They can be disruptive and should be used thoughtfully and sparingly.
<ahi-dialog label="Export patients" class="dialog-default">
<ahi-radio-group label="Export" value="1">
<ahi-radio value="1">Current page</ahi-radio>
<ahi-radio value="2">All patients</ahi-radio>
<ahi-radio value="3">Selected patients</ahi-radio>
</ahi-radio-group>
<br />
<ahi-radio-group label="Export as">
<ahi-radio value="1"> CSV for Excel, Numbers, or other spreadsheet programs </ahi-radio>
<ahi-radio value="2">Plain CSV file</ahi-radio>
</ahi-radio-group>
<ahi-button slot="footer">Cancel</ahi-button>
<ahi-button slot="footer" variant="primary">Export patients</ahi-button>
</ahi-dialog>
<ahi-button>Open Dialog</ahi-button>
<script>
const dialog = document.querySelector('.dialog-default');
const openButton = dialog.nextElementSibling;
const cancelButton = dialog.querySelector('ahi-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
cancelButton.addEventListener('click', () => dialog.hide());
</script>
Footer buttons align to the end of the dialog, so the primary action goes last, after Cancel. See Order of actions.
Examples
Scrolling
By design, a dialog’s height will never exceed that of the viewport. As such, dialogs will not scroll with the page ensuring the header and footer are always accessible to the user.
Scroll down and give it a try! 👇
<ahi-dialog label="Dialog" class="dialog-scrolling">
<div style="height: 150vh; border: dashed 2px var(--ahi-divider); padding: 0 1rem;">
<p>Scroll down and give it a try! 👇</p>
</div>
<ahi-button slot="footer" variant="primary">Close</ahi-button>
</ahi-dialog>
<ahi-button>Open Dialog</ahi-button>
<script>
const dialog = document.querySelector('.dialog-scrolling');
const openButton = dialog.nextElementSibling;
const cancelButton = dialog.querySelector('ahi-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
cancelButton.addEventListener('click', () => dialog.hide());
</script>
Preventing the dialog from closing
By default, dialogs will close when the user clicks the close button, clicks the overlay, or presses the
Escape key. In most cases, the default behavior is the best in terms of UX. However, there are
situations where this may be undesirable, such as when data loss will occur.
To keep the dialog open in such cases, you can cancel the ahi-request-close event. When
canceled, the dialog will remain open and pulse briefly to draw the user’s attention to it.
You can use event.detail.source to determine what triggered the request to close. This example
prevents the dialog from closing when the overlay is clicked, but allows the close button or
Escape to dismiss it.
<ahi-dialog label="Dialog" class="dialog-prevent-closing">
This dialog will not close when you click on the overlay.
<ahi-button slot="footer" variant="primary">Close</ahi-button>
</ahi-dialog>
<ahi-button>Open Dialog</ahi-button>
<script>
const dialog = document.querySelector('.dialog-prevent-closing');
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector('ahi-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
closeButton.addEventListener('click', () => dialog.hide());
// Prevent the dialog from closing when the user clicks on the overlay
dialog.addEventListener('ahi-request-close', event => {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
});
</script>
Customizing initial focus
By default, the dialog’s panel will gain focus when opened. This allows a subsequent tab press to focus on
the first tabbable element in the dialog. If you want a different element to have focus, add the
autofocus attribute to it as shown below.
<ahi-dialog label="Dialog" class="dialog-initial-focus">
<ahi-input autofocus placeholder="I will have focus when the dialog is opened"></ahi-input>
<ahi-button slot="footer" variant="primary">Close</ahi-button>
</ahi-dialog>
<ahi-button>Open Dialog</ahi-button>
<script>
const dialog = document.querySelector('.dialog-initial-focus');
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector('ahi-button[slot="footer"]');
openButton.addEventListener('click', () => dialog.show());
closeButton.addEventListener('click', () => dialog.hide());
</script>
You can further customize initial focus behavior by canceling the ahi-initial-focus event and
setting focus yourself inside the event handler.
Importing
To import this component using a script tag:
<script type="module" src="/path/to/azalea-kit-ui/dist/components/dialog/dialog.js"></script>
To import this component using a bundler:
import '@azaleahealth/azalea-kit-ui/dist/components/dialog/dialog.js';
Slots
| Name | Description |
|---|---|
| (default) | The dialog’s main content. |
label
|
The dialog’s label. Alternatively, you can use the label attribute. |
header-actions
|
Optional actions to add to the header. Works best with <ahi-icon-button>. |
footer
|
The dialog’s footer, usually one or more buttons representing various options. |
Learn more about using slots.
Properties
| Name | Description | Type | Default | |
|---|---|---|---|---|
open
|
Indicates whether or not the dialog is open. You can toggle this attribute to show and hide the
dialog, or you can use the show() and hide() methods and this attribute
will reflect the dialog’s open state.
|
boolean
|
false
|
|
label
|
The dialog’s label as displayed in the header. You should always include a relevant label even when
using
no-header, as it is required for proper accessibility. If you need to display HTML, use
the label slot instead.
|
string
|
''
|
|
noHeader
no-header
|
Disables the header. This will also remove the default close button, so please ensure you provide an easy, accessible way for users to dismiss the dialog. |
boolean
|
false
|
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about properties and attributes.
Events
| Name | Description | Event Detail |
|---|---|---|
ahi-show |
Emitted when the dialog opens. | - |
ahi-after-show |
Emitted after the dialog opens and all animations are complete. | - |
ahi-hide |
Emitted when the dialog closes. | - |
ahi-after-hide |
Emitted after the dialog closes and all animations are complete. | - |
ahi-initial-focus |
Emitted when the dialog opens and is ready to receive focus. Calling
event.preventDefault() will prevent focusing and allow you to set it on a different
element, such as an input.
|
- |
ahi-request-close |
Emitted when the user attempts to close the dialog by clicking the close button, clicking the
overlay, or pressing escape. Calling event.preventDefault() will keep the dialog open.
Avoid using this unless closing the dialog will result in destructive behavior such as data loss.
Note that the browser may override prevention for repeated Escape presses (or other platform close
gestures) without intervening user activation — the dialog then closes natively and the
open property is kept in sync.
|
{ source: 'close-button' | 'keyboard' | 'overlay' }
|
Learn more about events.
Methods
| Name | Description | Arguments |
|---|---|---|
show() |
Shows the dialog. | - |
hide() |
Hides the dialog | - |
Learn more about methods.
CSS Parts
| Name | Description |
|---|---|
base |
Deprecated. Use the dialog part instead. |
dialog |
The component’s outer wrapper. |
overlay |
The overlay that covers the screen behind the dialog. |
panel |
The dialog’s panel (where the dialog and its content are rendered). |
header |
The dialog’s header. This element wraps the title and header actions. |
header-actions |
Optional actions to add to the header. Works best with <ahi-icon-button>. |
title |
The dialog’s title. |
close-button |
The close button, an <ahi-icon-button>. |
close-button__base |
The close button’s exported base part. |
body |
The dialog’s body. |
footer |
The dialog’s footer. |
Learn more about customizing CSS parts.
Animations
| Name | Description |
|---|---|
dialog.show |
The animation to use when showing the dialog. |
dialog.hide |
The animation to use when hiding the dialog. |
dialog.denyClose |
The animation to use when a request to close the dialog is denied. |
dialog.overlay.show |
The animation to use when showing the dialog’s overlay. |
dialog.overlay.hide |
The animation to use when hiding the dialog’s overlay. |
Learn more about customizing animations.
Dependencies
This component automatically imports the following dependencies.
<ahi-icon><ahi-icon-button>