A dialog asks for a decision, or holds a short task, before anything else continues: discard a draft, confirm a deletion, pick a ringtone. It is modal: the page behind it is out of reach until it closes. For a message that needs no answer, use a snackbar.
Usage
The buttons are its actions, and each closes it. layer: 'top' shows it in the browser's top
layer, as a native <dialog> above everything else; the web component always is.
import { createDialog } from 'material';
const dialog = createDialog({
title: 'Discard draft?',
content: 'Your draft will be deleted.',
layer: 'top',
buttons: [
{ text: 'Cancel', variant: 'text', closeDialog: true },
{ text: 'Discard', variant: 'text', closeDialog: true },
],
});
document.body.append(dialog.element);
function ask() {
dialog.open();
}
<m-dialog headline="Discard draft?">
<p>Your draft will be deleted.</p>
<m-button slot="actions" variant="text">Cancel</m-button>
<m-button slot="actions" variant="text">Discard</m-button>
</m-dialog>
<script type="module">
const dialog = document.querySelector('m-dialog');
dialog.querySelectorAll('m-button[slot="actions"]').forEach((child) => child.addEventListener('click', () => dialog.close()));
function ask() {
dialog.show();
}
</script>
import { useState } from 'react';
import { Dialog, Button } from 'material/react';
export function Example() {
const [open, setOpen] = useState(false);
const ask = () => setOpen(true);
return (
<Dialog open={open} onClose={() => setOpen(false)} headline="Discard draft?">
<p>Your draft will be deleted.</p>
<Button slot="actions" variant="text" onClick={() => setOpen(false)}>Cancel</Button>
<Button slot="actions" variant="text" onClick={() => setOpen(false)}>Discard</Button>
</Dialog>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MDialog, MButton } from 'material/vue';
const open = ref(false);
function ask() {
open.value = true;
}
</script>
<template>
<MDialog :open="open" @close="open = false" headline="Discard draft?">
<p>Your draft will be deleted.</p>
<template #actions>
<MButton variant="text" @click="open = false">Cancel</MButton>
<MButton variant="text" @click="open = false">Discard</MButton>
</template>
</MDialog>
</template>
<script lang="ts">
import { Dialog, Button } from 'material/svelte';
let open = $state(false);
function ask() {
open = true;
}
</script>
<Dialog open={open} onclose={() => (open = false)} headline="Discard draft?">
<p>Your draft will be deleted.</p>
{#snippet actions()}
<Button variant="text" onclick={() => (open = false)}>Cancel</Button>
<Button variant="text" onclick={() => (open = false)}>Discard</Button>
{/snippet}
</Dialog>
import { createSignal } from 'solid-js';
import { Dialog, Button } from 'material/solid';
export function Example() {
const [open, setOpen] = createSignal(false);
const ask = () => setOpen(true);
return (
<Dialog open={open()} onClose={() => setOpen(false)} headline="Discard draft?">
<p>Your draft will be deleted.</p>
<Button slot="actions" variant="text" onClick={() => setOpen(false)}>Cancel</Button>
<Button slot="actions" variant="text" onClick={() => setOpen(false)}>Discard</Button>
</Dialog>
);
}
Examples
Full screen
A full-screen dialog holds a task in a compact window. It has a close button in its header,
and is a dialog rather than an alertdialog.
import { createDialog } from 'material';
const dialog = createDialog({
title: 'New event',
content: 'Add a title, a time and guests.',
size: 'fullscreen',
layer: 'top',
buttons: [{ text: 'Save', variant: 'text', closeDialog: true }],
});
document.body.append(dialog.element);
<m-dialog size="fullscreen" headline="New event">
<p>Add a title, a time and guests.</p>
<m-button slot="actions" variant="text">Save</m-button>
</m-dialog>
<script type="module">
const dialog = document.querySelector('m-dialog');
dialog.querySelectorAll('m-button[slot="actions"]').forEach((child) => child.addEventListener('click', () => dialog.close()));
</script>
import { Dialog, Button } from 'material/react';
export function Example() {
return (
<Dialog size="fullscreen" headline="New event">
<p>Add a title, a time and guests.</p>
<Button slot="actions" variant="text">Save</Button>
</Dialog>
);
}
<script setup lang="ts">
import { MDialog, MButton } from 'material/vue';
</script>
<template>
<MDialog size="fullscreen" headline="New event">
<p>Add a title, a time and guests.</p>
<template #actions>
<MButton variant="text">Save</MButton>
</template>
</MDialog>
</template>
<script lang="ts">
import { Dialog, Button } from 'material/svelte';
</script>
<Dialog size="fullscreen" headline="New event">
<p>Add a title, a time and guests.</p>
{#snippet actions()}
<Button variant="text">Save</Button>
{/snippet}
</Dialog>
import { Dialog, Button } from 'material/solid';
export function Example() {
return (
<Dialog size="fullscreen" headline="New event">
<p>Add a title, a time and guests.</p>
<Button slot="actions" variant="text">Save</Button>
</Dialog>
);
}
Only its actions close it
For work that would be lost, the scrim and Escape can be kept from closing it; its
actions then have to include a way out.
import { createDialog } from 'material';
const dialog = createDialog({
title: 'Leave the survey?',
content: 'Your answers so far will be lost.',
closeOnOverlayClick: false,
closeOnEscape: false,
layer: 'top',
buttons: [
{ text: 'Stay', variant: 'text', closeDialog: true },
{ text: 'Leave', variant: 'text', closeDialog: true },
],
});
document.body.append(dialog.element);
<m-dialog headline="Leave the survey?" no-close-on-scrim-click no-close-on-escape>
<p>Your answers so far will be lost.</p>
<m-button slot="actions" variant="text">Stay</m-button>
<m-button slot="actions" variant="text">Leave</m-button>
</m-dialog>
<script type="module">
const dialog = document.querySelector('m-dialog');
dialog.querySelectorAll('m-button[slot="actions"]').forEach((child) => child.addEventListener('click', () => dialog.close()));
</script>
import { Dialog, Button } from 'material/react';
export function Example() {
return (
<Dialog headline="Leave the survey?" noCloseOnScrimClick noCloseOnEscape>
<p>Your answers so far will be lost.</p>
<Button slot="actions" variant="text">Stay</Button>
<Button slot="actions" variant="text">Leave</Button>
</Dialog>
);
}
<script setup lang="ts">
import { MDialog, MButton } from 'material/vue';
</script>
<template>
<MDialog headline="Leave the survey?" no-close-on-scrim-click no-close-on-escape>
<p>Your answers so far will be lost.</p>
<template #actions>
<MButton variant="text">Stay</MButton>
<MButton variant="text">Leave</MButton>
</template>
</MDialog>
</template>
<script lang="ts">
import { Dialog, Button } from 'material/svelte';
</script>
<Dialog headline="Leave the survey?" noCloseOnScrimClick noCloseOnEscape>
<p>Your answers so far will be lost.</p>
{#snippet actions()}
<Button variant="text">Stay</Button>
<Button variant="text">Leave</Button>
{/snippet}
</Dialog>
import { Dialog, Button } from 'material/solid';
export function Example() {
return (
<Dialog headline="Leave the survey?" noCloseOnScrimClick noCloseOnEscape>
<p>Your answers so far will be lost.</p>
<Button slot="actions" variant="text">Stay</Button>
<Button slot="actions" variant="text">Leave</Button>
</Dialog>
);
}
Subtitle and dividers
divider draws a line above and below the content, for a body that scrolls.
import { createDialog } from 'material';
const dialog = createDialog({
title: 'Terms of service',
subtitle: 'Updated September 2026',
content: 'These terms apply to your use of the service.',
divider: true,
layer: 'top',
buttons: [{ text: 'Accept', variant: 'text', closeDialog: true }],
});
document.body.append(dialog.element);
<m-dialog subtitle="Updated September 2026" divider headline="Terms of service">
<p>These terms apply to your use of the service.</p>
<m-button slot="actions" variant="text">Accept</m-button>
</m-dialog>
<script type="module">
const dialog = document.querySelector('m-dialog');
dialog.querySelectorAll('m-button[slot="actions"]').forEach((child) => child.addEventListener('click', () => dialog.close()));
</script>
import { Dialog, Button } from 'material/react';
export function Example() {
return (
<Dialog subtitle="Updated September 2026" divider headline="Terms of service">
<p>These terms apply to your use of the service.</p>
<Button slot="actions" variant="text">Accept</Button>
</Dialog>
);
}
<script setup lang="ts">
import { MDialog, MButton } from 'material/vue';
</script>
<template>
<MDialog subtitle="Updated September 2026" divider headline="Terms of service">
<p>These terms apply to your use of the service.</p>
<template #actions>
<MButton variant="text">Accept</MButton>
</template>
</MDialog>
</template>
<script lang="ts">
import { Dialog, Button } from 'material/svelte';
</script>
<Dialog subtitle="Updated September 2026" divider headline="Terms of service">
<p>These terms apply to your use of the service.</p>
{#snippet actions()}
<Button variant="text">Accept</Button>
{/snippet}
</Dialog>
import { Dialog, Button } from 'material/solid';
export function Example() {
return (
<Dialog subtitle="Updated September 2026" divider headline="Terms of service">
<p>These terms apply to your use of the service.</p>
<Button slot="actions" variant="text">Accept</Button>
</Dialog>
);
}
A button's onClick(event, dialog) runs its action, and returning false keeps the dialog
open; the web component's actions are buttons of your own. confirm({ message }) asks a
question in the dialog and resolves to the answer. Recipes such as confirming a deletion or a
form that validates before it closes are planned for Showcase.
API
Options
titleThe headline, which names the dialog
stringundefinedsubtitleA line below the headline
stringundefinedcontentThe body, as HTML
stringundefinedbuttonsThe actions, in order
DialogButton[][]sizeThe width; fullscreen fills the viewport
'small' | 'medium' | 'large' | 'fullwidth' | 'fullscreen''medium'animationHow it enters and leaves
'scale' | 'slide-up' | 'slide-down' | 'fade''scale'footerAlignmentWhere the actions sit
'right' | 'left' | 'center' | 'space-between''right'dividerLines above and below the content
booleanfalsecloseButtonA close button in the header
booleantrue at fullscreen, else falseopenWhether it starts open
booleanfalsecloseOnOverlayClickWhether a click on the scrim closes it
booleantruecloseOnEscapeWhether Escape closes it
booleantruelayerShows it in the top layer, as a native <dialog> with showModal(); without it, it is in an overlay of its own in container
'top'undefinedcontainerWhere it is mounted
HTMLElementdocument.bodymodalfalse leaves out aria-modal, and the page behind it keeps scrolling; outside the top layer it stays reachable too
booleantrueautofocusFocuses it when it opens
booleantruetrapFocusKeeps Tab inside it
booleantrueroleThe role, dialog at fullscreen and alertdialog otherwise
'alertdialog' | 'dialog'sizeariaLabelThe name without a title
stringundefinedanimationDurationWhen afteropen and afterclose follow, in ms
number500 to open, 150 to closezIndexThe overlay's z-index, outside the top layer
numberundefinedonEvent handlers registered at creation
{ [event]: handler }undefinedclassAdditional CSS classes
stringundefinedA button
textThe label
stringvariantThe button's variant
string'text'onClickIts action; false keeps the dialog open
(event, dialog) => void | booleanundefinedcloseDialogWhether it closes the dialog
booleantrueautofocusFocuses it when the dialog opens
booleanfalsesizeThe button's size
stringundefinedMethods
open() / close() / toggle(open?)Opens or closes it
open?: booleanDialogComponentisOpen()Whether it is open
booleansetTitle(title) / getTitle()The headline
title: stringDialogComponent / stringsetSubtitle(subtitle) / getSubtitle()The subtitle
subtitle: stringDialogComponent / stringsetContent(content) / getContent()The body, as HTML
content: stringDialogComponent / stringaddButton(button) / removeButton(indexOrText)Adds or removes an action
button: DialogButton, indexOrText: number | stringDialogComponentgetButtons()The actions
DialogButton[]setSize(size) / setFooterAlignment(alignment)The width, where the actions sit
size: DialogSize, alignment: DialogFooterAlignmentDialogComponenttoggleDivider(show) / hasDivider()The lines around the content
show: booleanDialogComponent / booleangetHeaderElement() / getContentElement() / getFooterElement()Its regions
HTMLElement | nullconfirm(options)Replaces the content and actions with a question, opens it, and resolves to the button pressed: true for the confirming one, which comes last, false for the other. Closed any other way (Escape, the scrim, close()), it resolves false. The message is text
{ message, title?, confirmText?, cancelText?, confirmVariant?, cancelVariant?, size? }Promise<boolean>on(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionDialogComponentdestroy()Removes it
voidelementThe dialog
HTMLElementoverlayThe scrim it sits in, outside the top layer
HTMLElementEvents
beforeopen / beforecloseIt is about to open or close; preventDefault() stops it
{ dialog, preventDefault, defaultPrevented }open / closeEmitted inside open() and close(), once the dialog is open or closed. The animation finishing is afteropen / afterclose
{ dialog }afteropen / aftercloseIts animation is over
{ dialog }The web component dispatches open and close, without a detail, and cancel when Escape
asks it to close, which preventDefault() refuses.
Styling
.mtrl-dialog, .mtrl-dialog--visible { }
.mtrl-dialog--small, .mtrl-dialog--large, .mtrl-dialog--fullwidth, .mtrl-dialog--fullscreen { }
.mtrl-dialog--slide-up, .mtrl-dialog--slide-down, .mtrl-dialog--fade { }
.mtrl-dialog__overlay { } /* the scrim, outside the top layer */
.mtrl-dialog__header, .mtrl-dialog__header-title, .mtrl-dialog__header-subtitle { }
.mtrl-dialog__header-close, .mtrl-dialog__content, .mtrl-dialog__footer { }
.mtrl-dialog__footer--left, .mtrl-dialog__footer--center, .mtrl-dialog__footer--space-between { }
.mtrl-dialog__header-divider, .mtrl-dialog__footer-divider { }
Measurements
| Attribute | Value |
|---|---|
| Container | surface-container-high, 28dp corners, elevation 3 |
| Width | 280dp to 560dp; small 360dp at most |
| Scrim | scrim at 32% |
| Headline | Headline Small, on-surface |
| Supporting text | Body Medium, on-surface-variant |
| Padding | 24dp; 16dp between the headline and the content |
| Actions | 8dp apart |
| Full screen | No corners; 56dp header, Title Large |