Simple dialog
// also available globally on window import { mcDialog } from '@thewebformula/materially/services'; // import { mcDialog } from '@thewebformula/materially'; const answer = await mcDialog.simple({ icon: 'settings', headline: 'Question', message: 'Are you sure?', preventClose: false, // clicking outside of dialog will close it // If prevent navigation is not set then it will be the opposite of preventClose preventNavigation: true, // default false actionConfirm: true, // default true actionConfirm: 'OK', // default OK actionCancel: true, // default false actionCancelLabel: 'Cancel' // default Cancel }); if (answer === 'confirm') console.log('User pressed ok'); if (answer === 'cancel') console.log('User pressed cancel');
Template dialog
// templateString <div slot="headline">Headline</div> <div>Here is some content for the dialog.</div> <mc-button slot="actions" value="response value">Close</mc-button>
// also available globally on window import { mcDialog } from '@thewebformula/materially/services'; // import { mcDialog } from '@thewebformula/materially'; const value = await mcDialog.template({ template: templateString, preventClose: false }); // value is passed in when closing the dialog: mcDialog.close('response value') console.log(value);
HTML Only
You can interact directly with dialogs added to the DOM
Headline
<!-- Added directly to document Adding the 'open' attribute will automatically show the dialog --> <mc-dialog id="html-only-dialog"> <div slot="headline">Headline</div> <form id="one" method="dialog" slot="content"> <mc-textfield required label="Required input"></mc-textfield> </form> <mc-button slot="actions" form="one" value="submit" type="submit">Submit</mc-button> <mc-button slot="actions" form="one" type="reset">Reset</mc-button> <mc-button slot="actions" form="one" formnovalidate value="cancel">Cancel (with formnovalidate)</mc-button> </mc-dialog> <!-- Prevent dialog close on ESC key and clicking outside of dialog --> <mc-dialog prevent-close></mc-dialog>
const dialog = document.querySelector('#html-only-dialog'); dialog.scrim = true; dialog.preventClose = true; // allow clicking scrim to close dialog. Default false dialog.show(); console.log(dialog.open); dialog.addEventListener('close', event => { console.log('close', event.target.returnValue); }); dialog.addEventListener('cancel', event => { console.log('cancel', event.target.returnValue); }); dialog.addEventListener('submit', event => { console.log('submit', event.target.returnValue); }); dialog.close('returnValue string'); console.log(dialog.returnValue);
Scrollable
Dialog header and actions sections will be fixed when dialog content is too large
Headline
<mc-dialog id="scrollable-dialog"> <div slot="headline">Headline</div> <div slot="content"> ...large content </div> <mc-button slot="actions" onclick="mcDialog.close()">Close</mc-button> </mc-dialog>
Fullscreen
Fullscreen dialogs are for compact window sizes only
Headline
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and
going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes
from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first
line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32
and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H. Rackham.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and
going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes
from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first
line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32
and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H. Rackham.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and
going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes
from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first
line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32
and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H. Rackham.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and
going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes
from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first
line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32
and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H. Rackham.
<mc-dialog id="fullscreen-dialog" fullscreen> <div slot="headline">Headline</div> <div slot="content"> ...content </div> </mc-dialog>
Fullscreen with form
Fullscreen dialogs with forms can prevent closing based on changes
Change form
<mc-dialog id="fullscreen-dialog" prevent-close fullscreen> <div slot="headline">Change form</div> <div slot="content"> <!-- form[id] required --> <form id="fullscreen-form" method="dialog" onsubmit="console.log('submitted')"> <div style="font-size: 14px; margin-bottom: 8px;"><strong>X</strong> checks for form changes and presents dialog</div> <div style="font-size: 14px; margin-bottom: 24px;"><strong>Save</strong> will validate the form</div> <mc-textfield label="Required" supporting-test="Required" required value="one" class="outlined"></mc-textfield> </form> </div> </mc-dialog>
Style
CSS variables that allow you override default values
/* Optional variables for overriding */ --mc-dialog-container-color: var(--mc-surface-container-high); --mc-dialog-headline-color: var(--mc-on-surface); --mc-dialog-supporting-text-color: var(--mc-on-surface-variant);