Skip to content

Bound Dialog

The same dialog behavior for a stolon-dialog already written in the page markup, whose body is static markup the page fills in.

Decision

When Use
A detail dialog the page fills in before it opens Bound Dialog
Content built in code Dialog

Usage

<div id="itemDetailModal" class="stolon-dialog stolon-hidden" role="dialog" aria-modal="true" aria-labelledby="modalTitle">
    <div class="stolon-dialog-content">
        <button type="button" id="btnModalClose" class="stolon-dialog-close material-icons" aria-label="Close">close</button>
        <h2 id="modalTitle" class="stolon-modal-title"></h2>
        <div id="modalContent" class="stolon-modal-body"></div>
    </div>
</div>
import { bindDialog } from './configurationpage?name=myplugin_stolon.js';

var detail = bindDialog('itemDetailModal', { root: view, onClose: function (value) { onModalClosed(); } });
detail.open();

The handle is { element, open, close(value), isOpen, destroy }.

States and Modifiers

  • opts is { root, dismissable, beforeClose, onOpen, onClose(value), autofocus, returnFocus }. root is where an id is looked up, the document by default.
  • beforeClose() returns false, or a promise of false, to keep the dialog open when it is dismissed. close(value) called from code closes it without asking.
  • It shows and hides with the stolon-hidden class. It closes on a backdrop mousedown, on Escape when it is the top dialog, and on any .stolon-dialog-close inside, each asking beforeClose first.
  • It traps focus and restores it on close, with the same rules as Dialog.
  • It adds role="dialog", aria-modal, and a label from the first heading to .stolon-dialog-content when neither the panel nor the overlay carries a role.
  • destroy() closes the dialog and removes the listeners.

Gotchas

  • Closed markup must have the stolon-hidden class or it shows.
  • Keep the markup inside the page so it is hidden with it.
  • Unlike openDialog, there is no setTitle and no footer buttons. Set the title and the body in the markup.
  • It throws when the element is not found.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

bindDialog

function bindDialog(overlay: HTMLElement | string | null, opts?: BindDialogOptions): BoundDialogHandle

Gives a stolon-dialog already in the page markup the behavior openDialog builds, so it shows and hides by the stolon-hidden class, closes on backdrop click, Escape, and any .stolon-dialog-close button, each asking opts.beforeClose first, and traps and restores focus. The panel gets dialog semantics when the markup does not set them.

Option Type What it does
root ParentNode | null element to resolve an id within (default document)
dismissable boolean allow backdrop/Escape/close-button dismissal (default true)
onOpen (): void function() called after the dialog shows [optional]
onClose (value: unknown): void function(value) called once per close [optional]
Handle member Type
open (): void
close (value?: unknown): void
isOpen (): boolean
destroy (): void

Example

Example Code

<!-- title: Bound Dialog -->
<!-- min-height: 400 -->
<div id="itemDetailModal" class="stolon-dialog stolon-hidden" role="dialog" aria-modal="true" aria-labelledby="modalTitle">
    <div class="stolon-dialog-content stolon-small">
        <button type="button" id="btnModalClose" class="stolon-row-btn stolon-dialog-close" title="Close" aria-label="Close"><span class="material-icons" aria-hidden="true">close</span></button>
        <h2 id="modalTitle" class="stolon-modal-title">Item Detail</h2>
        <div id="modalContent" class="stolon-modal-body"></div>
    </div>
</div>
<button type="button" id="btnDetail" class="raised stolon-button stolon-primary"><span>Open Detail</span></button>
<span id="boundStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const shared = base.createShared(view, 'preview-plugin', 'Preview');
const detail = base.bindDialog('itemDetailModal', {
    root: view,
    onClose: () => shared.setStatus('boundStatus', 'Detail closed.', false, { timeout: 0 })
});
shared.bindClick('btnDetail', () => {
    document.getElementById('modalContent').textContent = 'The page filled this body before it opened the dialog.';
    detail.open();
});
</script>