Dialog
Modal dialogs on the native <dialog> element — the browser handles focus trapping, the inert background, Esc and returning focus. On phones they become bottom sheets.
<button class="btn btn-default" data-moey-open="#invite">Invite people</button>
<dialog class="dialog" id="invite" aria-labelledby="invite-title">
<div class="dialog-header">
<div>
<h2 class="dialog-title" id="invite-title">Invite people</h2>
<p class="dialog-description">They'll get an email with a link.</p>
</div>
<button class="btn-close" aria-label="Close"></button>
</div>
<div class="dialog-body">…</div>
<div class="dialog-footer">
<button class="btn btn-gray btn-ghost" data-moey-dismiss>Cancel</button>
<button class="btn btn-default">Send invites</button>
</div>
</dialog>
With moey.js it all works on its own:
- Open — any element with
data-moey-open="#id". - Close — the
.btn-close, anydata-moey-dismissbutton, or Esc. A<form method="dialog">closes it too, and the clicked button'svaluebecomesreturnValue. - Clicking outside doesn't close it — a stray click never throws away a half-filled form; it gives a small bump instead. Opt in with
data-moey-close-outside="true". Same for drawers and sheets — see the examples. - Static —
data-moey-static: nothing but your own buttons closes it (no outside click, no Esc). - Focus — goes to the dialog itself, or to the element with
autofocus(e.g. the first input of a form). - On phones — it becomes a bottom sheet with a handle you can drag down to close. Want a sheet on every screen size? Use
.sheet. Rather have it cover the phone's screen? Add.dialog-fullscreen-phone.
| Class | What it does |
|---|---|
.dialog-sm · -lg · -xl | 384 · 768 · 1152px wide (default 512px) — never wider than the screen |
.dialog-fit | As wide as its content |
--mo-dialog-width | Any width: 600px, 40rem, 80%, fit-content, or auto (as wide as the screen allows). On the dialog or any parent. |
.dialog-fullscreen | Covers the whole screen (big editors, previews); content clears the notch and home bar |
.dialog-divided | Lines under the header and above the footer — for long, scrolling content |
.dialog-body.dialog-flush | Edge-to-edge body — a list-group-flush inside gets full-width rows lined up with the dialog's padding (action lists, pickers) |
data-moey-close-outside="true" | A click outside closes it (off by default). On <body> it sets the page default. |
data-moey-static | Only your own buttons close it |
.dialog-center | Stays centered on phones instead of becoming a bottom sheet |
.dialog-fullscreen-phone | A normal dialog on larger screens, the whole screen on phones (under 576px) instead of a bottom sheet: a post, a long form, a reader |
.dialog-icon + theme-* | Round icon for confirm dialogs |
--mo-dialog-padding · --mo-dialog-footer-padding | Inner spacing (24px) · space above and below the footer buttons (14px) |
--mo-dialog-bg · --mo-dialog-fg | Background and text of dialogs, drawers and sheets. Set them on :root to give every one your page's colors (a theme with its own palette), or on one dialog |
Full screen on phones
A post, an article or a long form reads better on the whole screen of a phone than in a sheet. .dialog-fullscreen-phone keeps a normal dialog on larger screens and covers the screen under 576px (clearing the notch and home bar). Make the window narrow to see it switch:
<dialog class="dialog dialog-lg dialog-fullscreen-phone" id="post" aria-labelledby="post-title">…</dialog>
Your page has its own colors (a theme, a blog)? Give the dialogs the same ones, once, for the whole page:
:root {
--mo-dialog-bg: #fbf7f0; /* dialogs, drawers and sheets */
--mo-dialog-fg: #2b2622;
}
Clicking outside
By default a click on the dimmed area doesn't close a dialog, drawer or sheet — it gives a small bump, so a stray click never throws away a half-filled form. Esc and your own buttons still close it. Opt in where closing is harmless:
| Default | data-moey-close-outside="true" | |
|---|---|---|
| Dialog | ||
| Drawer | ||
| Sheet |
<dialog class="drawer" data-moey-close-outside="true">…</dialog> <!-- this one closes on a click outside -->
<body data-moey-close-outside="true"> <!-- every dialog, drawer and sheet on the page -->
<dialog class="dialog" data-moey-close-outside="false"> <!-- …except this one -->
| How it closes | Default | data-moey-close-outside="true" | data-moey-static |
|---|---|---|---|
✕, data-moey-dismiss, your buttons | ✓ | ✓ | ✓ |
| Esc | ✓ | ✓ | — (bumps) |
| Click outside | — (bumps) | ✓ | — (bumps) |
| Drag down (sheets) | ✓ | ✓ | — (springs back) |
From code & events
Moey.open(document.querySelector('#invite'));
Moey.close(dialog, 'saved');
dialog.addEventListener('moey:close', (e) => {
if (formHasUnsavedChanges) e.preventDefault(); // keep it open
});
document.addEventListener('moey:closed', (e) => console.log(e.detail.returnValue));
Events: moey:open (cancelable) · moey:opened · moey:close (cancelable) · moey:closed.
Sizes
<dialog class="dialog dialog-sm">…</dialog> <!-- 384px -->
<dialog class="dialog">…</dialog> <!-- 512px -->
<dialog class="dialog dialog-lg">…</dialog> <!-- 768px -->
<dialog class="dialog dialog-fit">…</dialog> <!-- as wide as its content -->
Form with validation
Required fields are checked before the dialog closes — the browser does it, no code needed.
<dialog class="dialog" id="new-customer">
<form class="contents" method="dialog">
<div class="dialog-body">
<input class="form-control" required autofocus>
<input class="form-control" type="email" required>
<div class="form-error">Enter a valid email address.</div>
</div>
<div class="dialog-footer">
<button type="button" class="btn btn-ghost" data-moey-dismiss>Cancel</button> <!-- not a submit button, so Enter submits the form -->
<button class="btn btn-default" value="save">Add customer</button>
</div>
</form>
</dialog>
Type to confirm
For things that can2019t be undone: the delete button only works once the name is typed (pattern does the check).
<input class="form-control" required pattern="Moey Supply">
<button class="btn btn-red" value="delete">Delete store</button>