On this page Dialog Dialog

The m3e-dialog component presents important prompts, alerts, and actions in user flows.

Installation
npm i @m3e/dialog
Usage

This section outlines usage examples and configuration guidance for the components in this package.

NOTE: this page is presented in a nested iframe.

Anatomy

The m3e-dialog component supports structured composition through named slots, enabling flexible layout and expressive content delivery:

Open Dialog Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Close
<m3e-button variant="filled">
  <m3e-dialog-trigger for="dlg">Open Dialog</m3e-dialog-trigger>
</m3e-button>
<m3e-dialog id="dlg" dismissible>
  <span slot="header">Lorem ipsum dolor sit amet</span>
  <!-- Content omitted for brevity -->
  <div slot="actions" end>
    <m3e-button autofocus><m3e-dialog-action return-value="ok">Close</m3e-dialog-action></m3e-button>
  </div>
</m3e-dialog>
Opening

Use the m3e-dialog-trigger to open a dialog. This component should be nested inside a clickable element and be associated with a dialog by setting the for attribute to the id of the dialog to open.

When its parent is clicked, it opens the dialog. When the dialog is closed, focus is stored to the parenting element.

Alternately, you can programmatically open a dialog using the show method.

<m3e-button variant="filled">
  <m3e-dialog-trigger for="dlg">Open Dialog</m3e-dialog-trigger>
</m3e-button>
<m3e-dialog id="dlg" dismissible>
  <!-- Content omitted for brevity -->
</m3e-dialog>
Actions

When closed, the m3e-dialog emits a closed event. Actions are used to designate a return value, set on the dialog when closed. Use the m3e-dialog-action component define an action.

When the parenting element of a m3e-dialog-action is clicked, it sets the returnValue property of its parenting m3e-dialog to the value of the return-value attribute and closes the dialog.

Alternately, you can programmatically close a dialog using the hide method.

<m3e-button>
    <m3e-dialog-action return-value="ok">Close</m3e-dialog-action>
</m3e-button>
Dismissible

By default, a close (or dismiss) button is not presented for a dialog. Use the dismissible attribute of the m3e-dialog to control this behavior. You can change the icon using the close-icon slot and change the accessible label using the close-label attribute.

Disabling close

The component provides an accessible experience by allowing users to click a dialog's backdrop or press ESCAPE to close a dialog. When this occurs, the cancel event is emitted. Use the disable-close attribute of the m3e-dialog to disable this behavior.

Focus management

The component provides an accessible experience by providing a focus trap used to keep focus within a dialog. Use the no-focus-trap attribute of the m3e-dialog to disable this behavior.

In addition, you can use the autofocus attribute to specify which action should be focused when the dialog is opened.

Accessibility

The m3e-dialog presents an internal dialog element labelled by the content of the header slot. You can use the alert attribute to specify whether the ARIA role="alertdialog" is applied to the internal dialog.

Dialog triggers add aria-haspopup="dialog" to its parenting element indicating to assistive technologies that activating the control will open a dialog.

Native module support

This package uses JavaScript Modules. To use it directly in a browser without a bundler, use a module script similar to the following.

<script type="module" src="/node_modules/@m3e/dialog/dist/index.js"></script>

You also need a module script for @m3e/icon-button due to being a dependency.

<script type="module" src="/node_modules/@m3e/icon-button/dist/index.js"></script>

In addition, you must use an import map to include additional dependencies.

<script type="importmap">
  {
    "imports": {
      "lit": "https://cdn.jsdelivr.net/npm/lit@3.3.0/+esm",
      "@m3e/core": "/node_modules/@m3e/core/dist/index.js"
    }
  }
</script>

For production, use index.min.js for faster load times.

API

This package includes a Custom Elements Manifest (custom-elements.json), which documents the properties, attributes, slots, events and CSS custom properties of each component.

You can explore the API below, or integrate the manifest into your own tooling.