Dialog¶
A modal over the page with a title, a body, and footer buttons. All dialog helpers are built on one panel. openDialog creates the markup, and Bound Dialog adds the behavior to markup already in the page. The Detail Drawer is the same panel docked to the right edge.
Decision¶
| When | Use |
|---|---|
| A short task or a detail view that needs the user's full attention | Dialog |
| A yes or no question | Confirm Dialog |
| A few values | Prompt Dialog |
| A record | Editor Dialog |
| The details of a row | Detail Drawer |
Usage¶
import { openDialog } from './configurationpage?name=myplugin_stolon.js';
var dialog = openDialog({
title: 'Session Log',
body: '<p>Backdrop click, the close button, and Escape all close it.</p>',
size: 'small',
buttons: [
{ text: 'Close', className: 'stolon-button secondary', action: function (close) { close(); } },
{ text: 'Retry', className: 'stolon-button primary', value: 'retry' }
],
onClose: function (value) { /* 'retry' or undefined */ }
});
The handle is { element, content, close, dismiss, setTitle }. element is the overlay and content is the body element.
The markup openDialog builds, which bindDialog also expects:
<div class="stolon-dialog">
<div class="stolon-dialog-content" role="dialog" aria-modal="true" aria-labelledby="stolon-dialog-title-1">
<button type="button" class="stolon-row-btn stolon-dialog-close" title="Close" aria-label="Close"><span class="material-icons" aria-hidden="true">close</span></button>
<h2 class="stolon-modal-title" id="stolon-dialog-title-1">Session Log</h2>
<div class="stolon-modal-body"></div>
<div class="stolon-modal-button-row">
<button type="button" class="raised stolon-modal-button stolon-button stolon-secondary">Close</button>
</div>
</div>
</div>
Classes: stolon-dialog, stolon-dialog-content, with the size modifiers stolon-small, stolon-medium, and stolon-large, stolon-dialog-close on a Row Button, stolon-modal-title, stolon-modal-body, stolon-modal-field, stolon-modal-field-label, stolon-modal-button-row, stolon-modal-button.
States and Modifiers¶
bodyis an HTML string or a DOM node. A string is inserted as HTML, so escape data in it.ariaLabelnames a dialog with notitle. Without it the name is the start of the body text, else "Dialog".sizesets the panel's width as a share of the window:smallis 40% (at least 320px),mediumis 60% (at least 420px, the default), andlargeis 80% (at least 560px). A panel never grows past the screen, and below 600px every size takes the screen's width.classNameadds classes to the panel.dismissable(default true) allows the backdrop, Escape, and the close button.closeButtonshows the corner button and is never shown when the dialog is not dismissable.beforeClose()returnsfalse, or a promise offalse, to keep the dialog open when it is dismissed.buttonsis[{ text, icon, className, value, action(close, dismiss, button) }].iconis a Material icon name drawn before the text. Withoutactiona button closes with itsvalue.dismisscloses the way Escape does, askingbeforeClose.autofocusis an element or selector to focus on open, orfalseto focus the panel.returnFocusis where focus goes on close when the opener is gone.- Focus on open goes to
autofocus, else the first visible field in the body, else a non destructive footer button, else the close button, else the panel. Astolon-button destructivebutton is never focused automatically. On a touch screen a text field is not focused, so the keyboard does not cover the dialog. - Tab and Shift+Tab cycle inside the panel. Only the top dialog answers Escape. A backdrop
mousedowndismisses. The page behind does not scroll while a dialog is open. - On close, focus returns to the control that opened it, else
returnFocus, else the first heading of the config page. A dialog still open underneath takes focus back. - A
largepanel is for an editor. Its button row is sticky at the bottom, with a destructive button apart on the left, and below 600px it fills the whole screen. Confirm and prompt dialogs aresmall. pageis the handle fromcreateConfigPage. The dialog closes when the page hides, without askingbeforeClose.setTitle(text)changes the title and adds or removes the heading andaria-labelledbyas needed.- The close button sits in the top right corner, inset by the panel's padding and level with the title, in every dialog. The padding is
--stolon-dialog-padon the panel, which the parts that reach its edges, such as a large dialog's button row, also read. - A footer button that is disabled, such as a typed confirm's button before the text matches, is dimmed.
- Links in a dialog that are not buttons take the accent color. The form rules apply inside a dialog without a class.
Gotchas¶
- A string
bodyis HTML. - A button with an
actionmust callcloseitself. - A dialog opened in the page body is outside
.pluginConfigurationPage. The field rules already cover.stolon-dialog, andclassName: 'pluginConfigurationPage'adds the page only rules.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
openDialog¶
function openDialog(opts?: DialogOptions): DialogHandle
Opens a modal that closes on backdrop click, Escape, and the corner close button, each asking opts.beforeClose first. Focus is trapped in the panel and restored on close.
| Option | Type | What it does |
|---|---|---|
title |
string |
heading text [optional] |
ariaLabel |
string |
accessible name when there is no title (default the start of the body text, else 'Dialog') [optional] |
body |
string | Node |
HTML string or a DOM node for the modal body [optional] |
size |
'small' | 'medium' | 'large' |
'small' (40% of the window width), 'medium' (60%), or 'large' (80%, filling the screen on a phone with its buttons kept in view) (default 'medium') |
className |
string |
classes added to the panel, such as pluginConfigurationPage for the page's form look [optional] |
dismissable |
boolean |
allow backdrop/Escape/close-button dismissal (default true) |
closeButton |
boolean |
render the corner close button, which also needs dismissable (default true) |
beforeClose |
(): boolean | PromiseLike<boolean | void> |
function() returning false, or a promise of false, to keep the dialog open when it is dismissed, such as to confirm discarding changes [optional] |
buttons |
DialogButton[] |
[{ text, icon, className, value, action(close, dismiss, button) }] rendered in the footer row, where dismiss closes as Escape does, asking beforeClose first, icon is a Material icon name before the text, and button is the clicked element [optional] |
onClose |
(value: unknown): void |
function(value) called once when the dialog closes [optional] |
autofocus |
HTMLElement | string | false | null |
the element or a selector within the dialog to focus on open, or false to focus the panel itself (default the first visible field, else a non-destructive footer button) [optional] |
| Handle member | Type |
|---|---|
element |
HTMLDivElement |
content |
HTMLDivElement |
close |
(value?: unknown): void |
dismiss |
(): void |
setTitle |
(text: string): void |
| Type | What it is |
|---|---|
DialogButton |
One footer button. icon is a Material icon name drawn before the text, action gets close, dismiss, and the clicked button, and without it a click closes with value. |
| Class | What it looks like |
|---|---|
stolon-dialog |
Full screen dimmed overlay that centers its panel |
stolon-dialog-content |
Centered, scrollable modal panel, 60% of the window wide at the medium size, whose padding every part inside it is set by |
stolon-dialog-content.stolon-small |
A small panel, 40% of the window wide, for a short prompt or confirmation |
stolon-dialog-content.stolon-large |
A large panel, 80% of the window wide, for an editor, with its buttons pinned at the bottom and the whole screen on a phone. A drawer's footer shares the pinned row |
stolon-dialog-close |
The corner close button, a row button pinned to the top right corner of the panel and inset by the panel's padding, level with the title |
stolon-modal-title |
Modal heading |
stolon-modal-body |
Modal body region between header and footer |
stolon-modal-field |
Field block with a label inside a modal |
stolon-modal-field-label |
Dimmed label above a modal field |
stolon-modal-button-row |
Footer row of equal width modal buttons |
stolon-modal-button |
Equal width button in the footer row |
Example¶
Example Code¶
<!-- title: Dialog -->
<!-- min-height: 460 -->
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
<button type="button" id="btnOpen" class="raised stolon-button stolon-primary"><span>Open Dialog</span></button>
<button type="button" id="btnWide" class="raised stolon-button stolon-secondary"><span>Open Wide Dialog</span></button>
</div>
<span id="dialogStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
shared.bindClick('btnOpen', () => base.openDialog({
title: 'Session Log',
body: '<p>The backdrop, the close button, and Escape all close it.</p>',
size: 'small',
buttons: [
{ text: 'Close', className: 'stolon-button stolon-secondary', action: (close) => close() },
{ text: 'Retry', className: 'stolon-button stolon-primary', value: 'retry' }
],
onClose: (value) => shared.setStatus('dialogStatus', 'Closed with ' + (value || 'nothing') + '.', false, { timeout: 0 })
}));
shared.bindClick('btnWide', () => {
let fields = '';
for (let i = 1; i <= 12; i++) {
fields += '<div class="inputContainer"><label>Field ' + i + '</label><input type="text" value="Value ' + i + '"></div>';
}
base.openDialog({
title: 'Wide Dialog',
size: 'large',
body: fields,
buttons: [
{ text: 'Remove', className: 'stolon-button stolon-destructive', action: (close) => close() },
{ text: 'Cancel', className: 'stolon-button stolon-secondary', action: (close) => close() },
{ text: 'Done', className: 'stolon-button stolon-primary', action: (close) => close() }
]
});
});
</script>