Dialog
Dialogs are used to display crucial information during a user's workflow.
In addition to directly using this component, mdui also provides four functions: mdui.dialog
, mdui.alert
, mdui.confirm
, mdui.prompt
. These functions simplify the use of the Dialog component.
Import the component:
import 'mdui/components/dialog.js';
Import the TypeScript type:
import type { Dialog } from 'mdui/components/dialog.js';
Example:
Dialog
Close
Open Dialog
<mdui-dialog class="example-dialog">
Dialog
<mdui-button>Close</mdui-button>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-dialog");
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector("mdui-button");
openButton.addEventListener("click", () => dialog.open = true);
closeButton.addEventListener("click", () => dialog.open = false);
</script>
Add the close-on-overlay-click
attribute to close the dialog when the overlay is clicked.
Dialog
Open Dialog
<mdui-dialog close-on-overlay-click class="example-overlay">Dialog</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-overlay");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
Code
Add the close-on-esc
attribute to enable closing the dialog when the ESC key is pressed.
Dialog
Open Dialog
<mdui-dialog
close-on-esc
close-on-overlay-click
class="example-ecs"
>Dialog</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-ecs");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
Code
Add the fullscreen
attribute to make the dialog fullscreen.
Dialog
Close
Open Dialog
<mdui-dialog fullscreen class="example-fullscreen">
Dialog
<mdui-button>Close</mdui-button>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-fullscreen");
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector("mdui-button");
openButton.addEventListener("click", () => dialog.open = true);
closeButton.addEventListener("click", () => dialog.open = false);
</script>
Code
Set the icon
attribute to add a Material Icon above the dialog.
Dialog
Open Dialog
<mdui-dialog
icon="restart_alt"
close-on-overlay-click
class="example-icon"
>Dialog</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-icon");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
CodeAlternatively, add an icon element above the dialog using the icon
slot.
Dialog
Open Dialog
<mdui-dialog close-on-overlay-click class="example-icon-slot">
Dialog
<mdui-icon slot="icon" name="restart_alt"></mdui-icon>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-icon-slot");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
Code
Use the headline
and description
attributes to set the dialog's title and description.
Open Dialog
<mdui-dialog
headline="Delete selected images?"
description="Images will be permenantly removed from you account and all synced devices."
close-on-overlay-click
class="example-headline"
></mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-headline");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
CodeAlternatively, use the headline
and description
slots to set the title and description elements.
Delete selected images?
Images will be permenantly removed from you account and all synced devices.
Open Dialog
<mdui-dialog close-on-overlay-click class="example-headline-slot">
<span slot="headline">Delete selected images?</span>
<span slot="description">Images will be permenantly removed from you account and all synced devices.</span>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-headline-slot");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
Code
Use the action
slot to add action buttons at the bottom of the dialog.
Cancel
Delete
Open Dialog
<mdui-dialog
close-on-overlay-click
headline="Delete selected images?"
class="example-action"
>
<mdui-button slot="action" variant="text">Cancel</mdui-button>
<mdui-button slot="action" variant="tonal">Delete</mdui-button>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-action");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
CodeAdd the stacked-actions
attribute to stack the action buttons vertically.
Turn on speed boost
No thanks
Open Dialog
<mdui-dialog
stacked-actions
close-on-overlay-click
headline="Use location service?"
class="example-stacked-actions"
>
<mdui-button slot="action" variant="text">Turn on speed boost</mdui-button>
<mdui-button slot="action" variant="text">No thanks</mdui-button>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-stacked-actions");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
Code
Use the header
slot to set the top content of the dialog.
Open Dialog
<mdui-dialog close-on-overlay-click class="example-header">
<mdui-top-app-bar slot="header">
<mdui-button-icon icon="close"></mdui-button-icon>
<mdui-top-app-bar-title>Title</mdui-top-app-bar-title>
<mdui-button variant="text">Save</mdui-button>
</mdui-top-app-bar>
<div style="height: 100px"></div>
</mdui-dialog>
<mdui-button>Open Dialog</mdui-button>
<script>
const dialog = document.querySelector(".example-header");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
CodeName |
---|
open |
Emitted when the dialog starts to open. Can be prevented with event.preventDefault() .
|
opened |
Emitted after the dialog has opened and the animations are completed.
|
close |
Emitted when the dialog starts to close. Can be prevented with event.preventDefault(). .
|
closed |
Emitted after the dialog has closed and the animations are completed.
|
overlay-click |
Emitted when the overlay is clicked.
|
Name |
---|
Top element, contains the icon slot and headline slot by default.
|
icon |
Top icon.
|
headline |
Top headline.
|
description |
Text below the title.
|
(default) |
Main content of the dialog.
|
action |
Elements in the bottom action bar.
|
Name |
---|
--shape-corner |
The size of the component corner. You can use a specific pixel value, but it's recommended to reference design tokens.
|
--z-index |
The CSS z-index value of the component.
|