Modals & Dialogs

The canonical pattern for share dialogs, confirmations, and any UI that overlays the page. Five framework features combine here, and each has a non-obvious "right" choice.

Key Concept: A modal is a composed pattern, not a primitive. Get the five small decisions right (click-outside, Esc-to-close, input binding, post-render focus, multi-class toggles) and avoid reaching for unneeded workarounds. The Project Management demo hosts every one of its forms through this pattern; the code below is the same shape, condensed.

The Complete Pattern

A working share dialog with click-outside, Esc-to-close, copy-to-clipboard, and auto-select on open. Read the code, then the decision table below explains every line that could have been written a different way.

<div data-component="share-document">
    <button class="btn btn-primary" data-action="openDialog">
        Share this document
    </button>
    <span class="ms-2 text-muted" data-show="lastAction" data-bind="lastAction"></span>

    <!-- data-event-self: only fires when the click target IS the scrim,
         not a click that bubbled up from the dialog box.
         position:fixed on .scrim escapes most stacking contexts;
         see "When you need a portal" below for the exceptions.
         data-cloak: hide the element until the framework has initialized
         and processed data-show, preventing a brief flash of the modal
         open on first paint. The framework removes the attribute on init. -->
    <div class="scrim" data-show="open"
         data-action="closeDialog" data-event-self data-cloak>
        <div class="dialog">
            <h3>Share link</h3>
            <p class="muted">Anyone with this URL can view the document.</p>

            <div class="urlbox">
                <!-- data-bind on <input> writes the .value PROPERTY,
                     bypassing the input dirty-value flag.
                     Do NOT use data-bind-attr={value: url}. -->
                <input type="text" data-bind="shareUrl" readonly>
                <button data-action="copyUrl"
                        data-bind-class="{ok: copied}"
                        data-bind="copyLabel">Copy</button>
            </div>

            <footer>
                <button class="btn btn-ghost" data-action="closeDialog">Done</button>
            </footer>
        </div>
    </div>
</div>
wildflower.component('share-document', {
    state: {
        open: false,
        shareUrl: '',
        copied: false,
        lastAction: '',
    },

    computed: {
        copyLabel() { return this.copied ? 'Copied' : 'Copy'; },
    },

    openDialog() {
        this.shareUrl = 'https://example.com/s/' + Math.random().toString(36).slice(2, 10);
        this.open = true;
        this.lastAction = '';        // clear "Dialog closed" from a prior cycle
        // Instance prop (not state): one-shot flag, no extra reactive pass.
        this._focusOnNextUpdate = true;
    },

    closeDialog() {
        this.open = false;
        this.copied = false;
        this.lastAction = 'Dialog was closed';
    },

    copyUrl() {
        try {
            navigator.clipboard.writeText(this.shareUrl);
        } catch (e) { /* older browsers: skip silently */ }
        this.copied = true;
        clearTimeout(this._copyTimer);
        this._copyTimer = setTimeout(() => { this.copied = false; }, 1500);
    },

    // onUpdate fires via requestAnimationFrame AFTER the DOM is committed.
    // This is WildflowerJS's $nextTick equivalent. Use it for focus,
    // .select(), scroll-into-view, third-party widget init.
    onUpdate() {
        if (this._focusOnNextUpdate) {
            this._focusOnNextUpdate = false;
            this.$el('input').el?.select();
        }
    },

    // Esc-to-close: a global shortcut while the modal is open, not a
    // widget-scoped key. Use a document listener. See "Esc-to-close: when
    // to use what" below for the data-event-key-* distinction.
    init() {
        this._onKey = (e) => {
            if (e.key === 'Escape' && this.open) this.closeDialog();
        };
        document.addEventListener('keydown', this._onKey);
    },

    destroy() {
        document.removeEventListener('keydown', this._onKey);
        clearTimeout(this._copyTimer);
    },
});
/* Uses the docs site's theme variables so the dialog adapts to
   light/dark mode. Outside this site, swap these for your own
   tokens (e.g. --modal-bg, --text-color, --border-color).         */

.scrim {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, .45);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
}
.dialog {
    width: 420px; max-width: calc(100vw - 32px);
    background: var(--modal-bg);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
    padding: 18px 20px;
}
.dialog h3 { margin: 0 0 4px; color: var(--text-color); }
.dialog .muted { color: var(--text-secondary); font-size: 13px; }
.urlbox {
    display: grid; grid-template-columns: 1fr auto; gap: 6px;
    margin: 12px 0;
}
.urlbox input {
    font-family: ui-monospace, monospace; font-size: 12px;
    padding: 8px 10px;
    background: var(--code-bg);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 3px; min-width: 0;
}
.urlbox button {
    padding: 0 14px;
    background: var(--accent-color); color: #fff;
    border: none; border-radius: 3px; font-weight: 600; cursor: pointer;
}
.urlbox button.ok { background: #2f8f4f; }
.dialog footer { display: flex; justify-content: flex-end; margin-top: 8px; }
.btn-ghost {
    background: transparent;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    padding: 6px 14px; border-radius: 3px; cursor: pointer;
}
.btn-ghost:hover { background: var(--code-bg); }
Live Preview

Decision Table

Each row is a place where two patterns can both work. The "Use" column is the idiomatic WildflowerJS primitive built for the job; the "Don't" column is the equivalent done by hand, more lines for the same result.

Concern Use Don't use Why
Click on scrim closes, click on dialog doesn't data-event-self on the scrim if (event.target === element) inside the handler, or stopPropagation() on the dialog Built-in modifier. Reads as: "fire this action only when the click originates ON this element, not on a descendant." One attribute beats four lines of handler logic, and the framework filters before your handler runs.
Display a value in a readonly text input data-bind="url" on <input> data-bind-attr="{value: url}" or imperative input.value = ... data-bind on form inputs writes the .value property. The attribute path (setAttribute) sets the value content attribute, which only seeds the initial render; the displayed .value property diverges from it on every subsequent update.
Focus / select / scroll after a conditional element appears Instance-prop flag + onUpdate setTimeout(..., 50) after toggling state onUpdate fires via requestAnimationFrame after the DOM commits. There is no fixed delay to tune, and no timing race when a slow device takes longer than 50ms.
One-shot flag for post-render imperative work Instance prop (this._flag = true) State field (this.flag = true) State-based costs two reactive passes for nothing: one to set, one to clear. Instance props are the documented escape hatch for "do this once, never bind." See "One-shot Flags" below.
Esc-to-close while a modal is open document.addEventListener('keydown', ...) in init / removed in destroy data-event-key-escape on a component element Esc-to-close-this-modal is conceptually a global shortcut. Declarative key modifiers require focus to be scoped to the listening element, which fights the focus-the-URL-input pattern. See "Esc-to-close: when to use what" below.
Toggle multiple CSS classes from booleans data-bind-class="{ok: copied, error: hasErr}" Ternary chain or string concatenation in a computed Object form reads as a truth table. Keys are bare JS identifiers; they don't need quoting inside the double-quoted HTML attribute.
Escape an ancestor that breaks fixed positioning data-portal="body" on the scrim Bumping z-index to escape a containing block Use a portal when an ancestor creates a containing block (transform, filter, perspective, will-change, contain) that traps position: fixed. Use z-index for stacking order within the same containing block (e.g., a confirm dialog on top of a share dialog, both un-portaled). The two solve different problems; don't reach for z-index when the issue is a trapped containing block, and don't reach for a portal when the issue is layering order. See "When you need a portal" below.

One-shot Flags: Instance Prop vs. State

The _focusOnNextUpdate flag is stored on this directly, not on this.state. Both work, but the instance-prop form is the right escape hatch for "do this once, post-render":

// GOOD: instance prop. Set once, read once, no reactive pass.
openDialog() {
    this.open = true;
    this._focusOnNextUpdate = true;
}

onUpdate() {
    if (this._focusOnNextUpdate) {
        this._focusOnNextUpdate = false;
        this.$el('input').el?.select();
    }
}

// WORKS BUT WASTEFUL: state-based. Setting and clearing each trigger a
// reactive flush. For a one-shot focus, two extra passes for no gain.
state: { open: false, focusFlag: false },
openDialog() {
    this.open = true;
    this.focusFlag = true;     // reactive pass #1
}
onUpdate() {
    if (this.focusFlag) {
        this.focusFlag = false; // reactive pass #2
        this.$el('input').el?.select();
    }
}

Use the instance-prop pattern any time the flag is read by lifecycle hooks or imperative code and never bound to the DOM. The same escape hatch is documented for third-party handles (AudioContext, IntersectionObserver, scroll observers) on the Lifecycle Hooks page.

Esc-to-close: When to Use What

WildflowerJS has two ways to handle keyboard input. They serve different jobs:

Document listener: global-while-X-is-true

When the key should fire regardless of where focus happens to be:

  • Esc closes the currently-open modal
  • Cmd/Ctrl+K opens a global command palette
  • ? shows a keyboard-shortcut overlay
init() {
    this._onKey = (e) => {
        if (e.key === 'Escape' && this.open) this.closeDialog();
    };
    document.addEventListener('keydown', this._onKey);
},
destroy() {
    document.removeEventListener('keydown', this._onKey);
},

data-event-key-*: widget-scoped shortcuts

When the key should only fire while a specific element is focused:

  • Enter submits a search input
  • Arrow keys navigate a focused listbox
  • Esc clears a focused autocomplete
<input data-action="keyup:search" data-event-key-enter>
<ul role="listbox" data-action="keydown:moveDown" data-event-key-down tabindex="0">...</ul>

Supported keys: enter, tab, delete, esc, escape, space, up, down, left, right, backspace. Modifier-only attributes: data-event-key-ctrl, data-event-key-alt, data-event-key-shift, data-event-key-meta. Any other key name works via data-event-key-<name>.

How to choose: ask where focus should be when the key fires. If the answer is "wherever the user happens to be," it's a document listener. If the answer is "in this specific widget," it's data-event-key-*. The most common modal mistake is using data-event-key-escape for modal close, then fighting focus management.

Confirmation Dialogs and Multiple Modes

For a single component that hosts several dialog flavors (share, confirm-revoke, settings), derive the dialog content from a single source-of-truth state field and switch modes with one flag.

The Complete Pattern above used a plain open boolean because there was only one dialog flavor. Once a component owns two or more flavors, replace the boolean with a dialogMode string ("share", "confirm", or null) and derive scrimOpen from it as a computed. Both conventions are correct; the choice is just "how many flavors does this component need to show":

wildflower.component('saved-documents', {
    state: {
        activeId: null,
        dialogMode: null,   // 'share' | 'confirm' | null
        pendingIds: [],
    },

    computed: {
        scrimOpen()      { return this.dialogMode !== null; },
        isShareMode()    { return this.dialogMode === 'share'; },
        isConfirmMode()  { return this.dialogMode === 'confirm'; },

        // Dialog content derived from activeId, no mirror state on open.
        activeDoc() {
            return this.activeId
                ? this.documents.find(d => d.id === this.activeId)
                : null;
        },
        dialogTitle() { return this.activeDoc?.name || ''; },
    },

    openShare(event, element, details) {
        this.activeId = details.item.id;
        this.dialogMode = 'share';
    },
    askConfirm(event, element, details) {
        this.pendingIds = [details.item.id];
        this.dialogMode = 'confirm';
    },
    closeDialog() {
        this.dialogMode = null;
        this.activeId = null;
    },
});
<div class="scrim" data-show="scrimOpen"
     data-action="closeDialog" data-event-self data-cloak>
    <div class="dialog" data-show="isShareMode">
        <h3 data-bind="dialogTitle"></h3>
        ...
    </div>
    <div class="dialog confirm" data-show="isConfirmMode">
        <h3>Are you sure?</h3>
        ...
    </div>
</div>

The scrim's data-event-self and Esc listener cover both modes without duplication. Each dialog body uses data-show against a mode flag, so only one is visible at a time. Dialog content like dialogTitle is a computed off activeId, so opening, switching, or reopening the dialog never requires copying fields into mirror state. The Esc handler from the Complete Pattern carries over unchanged except that the gate becomes this.scrimOpen (or equivalently this.dialogMode !== null) instead of this.open.

Transitioning Between Modes

When the next mode uses the same scrim (share to confirm, settings to about), just flip dialogMode directly. The scrim stays open, only the inner data-show flips, and the user sees one dialog replace the other instantly.

When the dialog box itself needs a visible exit animation before the next one appears, schedule the next mode flip after the animation completes. Either listen for the transitionend event on the dialog, or schedule the next flip via setTimeout matched to your animation duration:

revokeFromShare() {
    const id = this.activeId;
    this.dialogMode = null;         // close share dialog (triggers exit anim)
    setTimeout(() => {
        this.pendingIds = [id];
        this.dialogMode = 'confirm';  // open confirm after anim settles
    }, 160);                                // match your animation duration
}

Pick the timing path that fits your design: instant flip if the two dialogs feel like one continuous surface, animated handoff if they should feel distinct.

When You Need a Portal

The example above uses position: fixed; inset: 0 on the scrim and renders the modal in its natural DOM location. This works as long as no ancestor creates a containing block for fixed-positioned children. If yours does, the scrim renders relative to that ancestor instead of the viewport, and the modal can end up clipped, off-center, or behind unrelated content.

Ancestors that create a containing block for position: fixed:

  • transform with any value other than none
  • filter, backdrop-filter
  • perspective with any value other than none
  • will-change: transform or will-change: filter
  • contain: layout, contain: paint, contain: strict

When any of these apply to a parent of your modal, add data-portal="body" to the scrim:

<div class="scrim" data-show="open"
     data-action="closeDialog" data-event-self
     data-portal="body" data-cloak>
    <div class="dialog">...</div>
</div>

The framework teleports the scrim (and its dialog contents) to <body> at the moment open becomes true, and moves it back when open becomes false. All bindings, event handlers, and reactivity continue to work; the source component still owns the modal even though the DOM lives elsewhere. See Portals for the full contract.

If you're not sure whether you need a portal, ship without one first. The symptom is unmistakable: the scrim renders at the wrong size or position when opened, often clipped to the size of a parent container. Adding data-portal="body" at that point is a one-attribute fix.

Accessibility

The patterns above cover the reactive wiring. A production-ready modal also needs ARIA roles and focus management, which are responsibilities of your markup and surrounding code rather than the framework. At minimum:

<div class="scrim" data-show="open"
     data-action="closeDialog" data-event-self data-cloak
     role="dialog" aria-modal="true"
     aria-labelledby="dialog-title">
    <div class="dialog">
        <h3 id="dialog-title">Share link</h3>
        ...
    </div>
</div>
  • role="dialog" and aria-modal="true" tell assistive tech this is a modal region.
  • aria-labelledby (or aria-label) gives the modal an accessible name. Point it at the heading element so screen readers announce the dialog by its title.
  • Focus trap. Tab and Shift+Tab should cycle within the dialog while it's open, not leak out to background page elements. The framework doesn't ship a focus-trap primitive; either write a small handler that intercepts keydown on Tab and wraps focus, or pull in a small library (e.g., focus-trap) and start/stop it in openDialog/closeDialog.
  • Restore focus on close. Save document.activeElement in openDialog before any state flip and call .focus() on it in closeDialog, so keyboard users return to the trigger button they came from.
  • The Project Management demo's pm-modal-host uses this exact attribute set; read it for a working reference.
openDialog() {
    this._triggerEl = document.activeElement;   // capture before state flips
    this.open = true;
    this._focusOnNextUpdate = true;
},
closeDialog() {
    this.open = false;
    this._triggerEl?.focus();
},

Capturing document.activeElement on the same instance prop that hosts other one-shot flags keeps the pattern uniform. Capturing in init instead of openDialog would grab whatever happened to be focused at mount time, which is rarely the right target.

These are not in the canonical example to keep the wiring lessons clean, but every modal you ship should have them.

Anti-patterns

Reaching for setTimeout after a state toggle

If code looks like this, the framework has a hook that does what you want:

// BAD
this.open = true;
setTimeout(() => this.$el('input').el.select(), 50);

// GOOD
this.open = true;
this._focusOnNextUpdate = true;
// ... and check the flag in onUpdate()

Imperatively writing .value through $el

The framework writes .value for you when you use data-bind on an input. The only time you need imperative .value assignment is for inputs that aren't bound to component state at all (e.g., a third-party widget's hidden field).

// BAD
openDialog() {
    this.open = true;
    setTimeout(() => { this.$el('input').el.value = this.shareUrl; }, 50);
}

// GOOD: data-bind="shareUrl" on the <input> in HTML;
// no imperative .value assignment needed.

data-bind-attr for input values

data-bind-attr calls setAttribute, which sets an input's value content attribute but not its displayed .value property. The two diverge as soon as the input has been rendered once, so subsequent updates won't appear. Use data-bind on inputs (writes the property); reserve data-bind-attr for true HTML attributes like href, src, aria-*, and title.

Wrapping the dialog body in a click-stop handler

An older pattern (and one the Patterns page still shows) puts data-action="stopPropagation" on the dialog box to keep clicks inside the dialog from reaching the scrim. data-event-self on the scrim is the modern equivalent and doesn't need a custom stopPropagation method:

<!-- Older: stop-propagation guard -->
<div class="scrim" data-action="cancel">
    <div class="dialog" data-action="stopPropagation">...</div>
</div>

<!-- Better: event-self filter -->
<div class="scrim" data-action="cancel" data-event-self>
    <div class="dialog">...</div>
</div>

See Also

  • Portals: for understanding why data-portal="body" matters and what stacking contexts can clip your modal.
  • Lifecycle Hooks: for onUpdate, init, and destroy contracts.
  • Advanced Events: for the full event modifier surface (data-event-stop, data-event-prevent, data-event-debounce, data-event-key-*).
  • Content Binding: for how data-bind behaves on form inputs vs. other elements.
  • Patterns: Confirmation Modal: a shorter copy-and-paste recipe focused on confirm-and-delete flows (browse to the "Confirmation Modal" entry in the patterns sidebar).
Core Kanban Board

Card edit modals over a board that also does drag-and-drop, search filtering, and localStorage persistence.