Skip to content

Confirm Dialog

A narrow dialog that asks a yes or no question and resolves to a boolean.

Decision

When Use
Before an action that removes or overwrites data Confirm Dialog
A user who must also edit values Editor Dialog

Usage

import { confirmDialog } from './configurationpage?name=myplugin_stolon.js';

confirmDialog('Remove this server? Its links stop playing.', {
    title: 'Remove Server', confirmText: 'Remove', destructive: true
}).then(function (confirmed) { if (confirmed === true) { removeServer(); } });

States and Modifiers

  • opts is { title, confirmText, cancelText, destructive, html }. The buttons default to "Confirm" and "Cancel", and there is no default title, so pass one that names the action.
  • destructive makes the confirm button stolon-button destructive. Otherwise it is stolon-button primary. A destructive confirm is never focused first, so Cancel takes focus and Enter cannot confirm by accident.
  • The message is plain text and is escaped unless html: true. Escape HTML you pass yourself.
  • It resolves true only for the confirm button. Escape, the backdrop, and Cancel resolve false. It never rejects.

Typed Confirm

typeToConfirm asks the reader to type a word before a confirm that cannot be undone, such as deleting a database.

confirmDialog('This deletes the sync database and every link in it. It cannot be undone.', {
    title: 'Delete Database', confirmText: 'Delete', destructive: true, typeToConfirm: 'Database'
}).then(function (confirmed) { if (confirmed === true) { deleteDatabase(); } });
  • A field under the message is labelled "Type Database to confirm.", from typeLabel, where {value} is the word.
  • The confirm button stays disabled until the field holds the word exactly, case and spaces included.
  • Enter confirms only when the word matches. Otherwise the field shows mismatchText, "The text does not match.", under it through the Field Message, which clears once the text matches or is emptied.
  • The field takes focus on open, except on a touch screen, and the destructive button never does.

Reference

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

confirmDialog

function confirmDialog(message: string, opts?: ConfirmDialogOptions): Promise<boolean>

Opens a yes or no dialog that resolves to true when confirmed and false otherwise. With typeToConfirm it is a typed confirm, whose confirm button stays off until the field holds that exact text, and Enter confirms only then.

Option Type What it does
title string the dialog title, which names the action [optional]
confirmText string the confirm button label (default 'Confirm')
cancelText string the cancel button label (default 'Cancel')
destructive boolean true makes the confirm button destructive (default false)
html boolean true treats the message as HTML (default false)
typeToConfirm string text the reader must type exactly before confirming, such as the name of what is deleted [optional]
typeLabel string the field's label, where {value} is the text to type (default 'Type {value} to confirm.')
mismatchText string the error under the field when Enter is pressed before it matches (default 'The text does not match.')

Example

Example Code

<!-- title: Confirm Dialog -->
<!-- min-height: 360 -->
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
    <button type="button" id="btnRemove" class="raised stolon-button stolon-destructive"><span>Remove Server</span></button>
    <button type="button" id="btnDelete" class="raised stolon-button stolon-destructive"><span>Delete Database</span></button>
</div>
<span id="confirmStatus" 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('btnRemove', () => {
    base.confirmDialog('Remove this server? Its links stop playing.', {
        title: 'Remove Server',
        confirmText: 'Remove',
        destructive: true
    }).then((confirmed) => shared.setStatus('confirmStatus', confirmed ? 'Removed.' : 'Kept.', !confirmed, { timeout: 0 }));
});
// A typed confirm keeps Delete off until the field holds the exact text.
shared.bindClick('btnDelete', () => {
    base.confirmDialog('This deletes the sync database and every link in it. It cannot be undone.', {
        title: 'Delete Database',
        confirmText: 'Delete',
        destructive: true,
        typeToConfirm: 'Database'
    }).then((confirmed) => shared.setStatus('confirmStatus', confirmed ? 'Deleted.' : 'Kept.', !confirmed, { timeout: 0 }));
});
</script>