<wa-dialog>
Dialogs, sometimes called "modals", appear above the page and require the user's immediate attention.
<wa-dialog label="Dialog" with-header with-footer class="dialog-overview"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-overview'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
Headers can be used to display titles and more. Use the with-header
attribute to add a header to
the dialog.
<wa-dialog label="Dialog" with-header class="dialog-header"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-header'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
Footers can be used to display titles and more. Use the with-footer
attribute to add a footer to
the dialog.
<wa-dialog label="Dialog" with-footer class="dialog-footer"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-footer'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
You can add the special data-dialog="close"
attribute to a button inside the dialog to
tell it to close without additional JavaScript. Alternatively, you can set the open
property to
false
to close the dialog programmatically.
<wa-dialog label="Dialog" with-header with-footer class="dialog-dismiss"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-dismiss'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
Use the --width
custom property to set the dialog's width.
<wa-dialog label="Dialog" with-header with-footer class="dialog-width" style="--width: 50vw;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-width'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
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! 👇
<wa-dialog label="Dialog" with-header with-footer class="dialog-scrolling"> <div style="height: 150vh; border: dashed 2px var(--wa-color-surface-border); padding: 0 1rem;"> <p>Scroll down and give it a try! 👇</p> </div> <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-scrolling'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
The header shows a functional close button by default. You can use the header-actions
slot to add
additional icon buttons if needed.
<wa-dialog label="Dialog" with-header with-footer class="dialog-header-actions"> <wa-icon-button class="new-window" slot="header-actions" name="arrow-up-right-from-square" variant="solid"></wa-icon-button> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-header-actions'); const openButton = dialog.nextElementSibling; const newWindowButton = dialog.querySelector('.new-window'); openButton.addEventListener('click', () => dialog.open = true); newWindowButton.addEventListener('click', () => window.open(location.href)); </script>
If you want the dialog to close when the user clicks on the overlay, add the
light-dismiss
attribute.
<wa-dialog label="Dialog" light-dismiss with-header with-footer class="dialog-light-dismiss"> This dialog will close when you click on the overlay. <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-light-dismiss'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
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 behavior 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 wa-hide
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 which element 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.
<wa-dialog label="Dialog" with-header with-footer class="dialog-deny-close"> This dialog will only close when you click the button below. <wa-button slot="footer" variant="brand" data-dialog="close">Only this button will close it</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-deny-close'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('wa-button[slot="footer"]'); openButton.addEventListener('click', () => dialog.open = true); // Prevent the dialog from closing unless the close button was clicked dialog.addEventListener('wa-hide', event => { if (event.detail.source !== closeButton) { event.preventDefault(); } }); </script>
To give focus to a specific element when the dialog opens, use the autofocus
attribute.
<wa-dialog label="Dialog" with-header with-footer class="dialog-focus"> <wa-input autofocus placeholder="I will have focus when the dialog is opened"></wa-input> <wa-button slot="footer" variant="brand" data-dialog="close">Close</wa-button> </wa-dialog> <wa-button>Open Dialog</wa-button> <script> const dialog = document.querySelector('.dialog-focus'); const input = dialog.querySelector('wa-input'); const openButton = dialog.nextElementSibling; openButton.addEventListener('click', () => dialog.open = true); </script>
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 <wa-icon-button> .
|
footer
|
The dialog's footer, usually one or more buttons representing various options. |
Name | Description | Reflects | |
---|---|---|---|
open 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.
Type
boolean
Default
false
|
|
|
label label
|
The dialog's label as displayed in the header. You should always include a relevant label, as it is
required for proper accessibility. If you need to display HTML, use the
label slot
instead.
Type
string
Default
''
|
|
|
withHeader with-header
|
Renders the dialog with a header.
Type
boolean
Default
false
|
|
|
withFooter with-footer
|
Renders the dialog with a footer.
Type
boolean
Default
false
|
|
|
lightDismiss light-dismiss
|
When enabled, the dialog will be closed when the user clicks outside of it.
Type
boolean
Default
false
|
Name | Description |
---|---|
wa-show |
Emitted when the dialog opens. |
wa-after-show |
Emitted after the dialog opens and all animations are complete. |
wa-hide |
Emitted when the dialog is requested to close. Calling event.preventDefault() will
prevent the dialog from closing. You can inspect event.detail.source to see which element
caused the dialog to close. If the source is the dialog element itself, the user has pressed
Escape or the dialog has been closed programmatically. Avoid using this unless closing the
dialog will result in destructive behavior such as data loss.
|
wa-after-hide |
Emitted after the dialog closes and all animations are complete. |
Name | Description |
---|---|
--background-color |
The dialog's background color.
|
--border-radius |
The radius of the dialog's corners.
|
--box-shadow |
The shadow effects around the edges of the dialog.
|
--spacing |
The amount of space around and between the dialog's content.
|
--width |
The preferred width of the dialog. Note that the dialog will shrink to accommodate smaller screens.
|
--show-duration |
The animation duration when showing the dialog.
Default
200ms
|
--hide-duration |
The animation duration when hiding the dialog.
Default
200ms
|
Name | Description |
---|---|
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 <wa-icon-button> .
|
title |
The dialog's title. |
close-button |
The close button, a <wa-icon-button> . |
close-button__base |
The close button's exported base part. |
body |
The dialog's body. |
footer |
The dialog's footer. |
This component automatically imports the following elements. Subdependencies, if any exist, will also be included in this list.
The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
To manually import this component from the CDN, use the following code.
import 'https://early.webawesome.com/webawesome@3.0.0-alpha.4/dist/components/dialog/dialog.js';