alertDialog()

A JavaScript wrapper for awaiting a confirmation from an alert dialog

The alertDialog() function is a confirmation-focused wrapper around the dialog() helper for use with the Alert Dialog component. Since an alert dialog's whole purpose is "await a confirmation before doing something", the wrapper exposes a boolean-returning confirmAsync() method: it resolves true when the user activates the action button, and false when the dialog is cancelled or dismissed with the Esc key.

Usage

const alertDialog = window.stellarAdmin.alertDialog(document.getElementById("myAlertDialog"));
if (await alertDialog.confirmAsync()) {
  // perform the confirmed action
}

Examples

Basic usage

Open the alert dialog with confirmAsync() and await the boolean result. The <sa-alert-dialog-action> button confirms the dialog, while <sa-alert-dialog-cancel> or the Esc key cancels it.

Theme
<sa-stack align="StackAlign.Start" class="min-w-md">
    <sa-button variant="ButtonVariant.Destructive" id="--js-alert-dialog-intro-button">
        Cancel Booking
    </sa-button>
    <label class="text-sm font-bold">Result:</label>
    <div class="font-mono w-full rounded-md border border-border bg-muted p-3 text-foreground" id="--js-alert-dialog-intro-result">-</div>
</sa-stack>
<sa-alert-dialog id="--js-alert-dialog-intro-dialog">
    <sa-alert-dialog-header>
        <sa-alert-dialog-title>Cancel this booking?</sa-alert-dialog-title>
        <sa-alert-dialog-description>
            Your reservation for the Marrakech Desert Escape will be cancelled. Cancellation fees may apply.
        </sa-alert-dialog-description>
    </sa-alert-dialog-header>
    <form method="dialog">
        <sa-alert-dialog-footer>
            <sa-alert-dialog-cancel>Keep Booking</sa-alert-dialog-cancel>
            <sa-alert-dialog-action variant="ButtonVariant.Destructive">Cancel Booking</sa-alert-dialog-action>
        </sa-alert-dialog-footer>
    </form>
</sa-alert-dialog>
<script type="module">
    (function () {
        const alertDialog = window.stellarAdmin.alertDialog(document.getElementById("--js-alert-dialog-intro-dialog"));
        const triggerButton = document.getElementById("--js-alert-dialog-intro-button");
        const resultDisplay = document.getElementById("--js-alert-dialog-intro-result");

        triggerButton.addEventListener("click", async () => {
            const confirmed = await alertDialog.confirmAsync();
            resultDisplay.innerHTML = confirmed ? "Booking cancelled" : "Booking kept";
        });
    })();
</script>

Manual result

You can resolve the confirmation programmatically using the confirm() and cancel() methods. This closes the dialog and resolves the pending promise with true or false respectively.

Theme
<sa-stack align="StackAlign.Start" class="min-w-md">
    <sa-button variant="ButtonVariant.Outline" id="--js-alert-dialog-manual-button">
        Open Alert Dialog
    </sa-button>
    <label class="text-sm font-bold">Result:</label>
    <div class="font-mono w-full rounded-md border border-border bg-muted p-3 text-foreground" id="--js-alert-dialog-manual-result">-</div>
</sa-stack>
<sa-alert-dialog id="--js-alert-dialog-manual-dialog">
    <sa-alert-dialog-header>
        <sa-alert-dialog-title>Manual Result</sa-alert-dialog-title>
        <sa-alert-dialog-description>
            Demonstrates how you can manually resolve the confirmation.
        </sa-alert-dialog-description>
    </sa-alert-dialog-header>
    <sa-alert-dialog-footer>
        <sa-button id="--js-alert-dialog-manual-button-cancel" variant="ButtonVariant.Outline">
            Cancel
        </sa-button>
        <sa-button id="--js-alert-dialog-manual-button-confirm" variant="ButtonVariant.Outline">
            Confirm
        </sa-button>
    </sa-alert-dialog-footer>
</sa-alert-dialog>
<script type="module">
    (function () {
        const alertDialog = window.stellarAdmin.alertDialog(document.getElementById("--js-alert-dialog-manual-dialog"));
        const resultDisplay = document.getElementById("--js-alert-dialog-manual-result");
        const triggerButton = document.getElementById("--js-alert-dialog-manual-button");
        const cancelButton = document.getElementById("--js-alert-dialog-manual-button-cancel");
        const confirmButton = document.getElementById("--js-alert-dialog-manual-button-confirm");

        triggerButton.addEventListener("click", async () => {
            const confirmed = await alertDialog.confirmAsync();
            resultDisplay.innerHTML = confirmed ? "Confirmed" : "Cancelled";
        });
        cancelButton.addEventListener("click", () => {
            alertDialog.cancel();
        });
        confirmButton.addEventListener("click", () => {
            alertDialog.confirm();
        });
    })();
</script>

API Reference

alertDialog(selector)

The alertDialog() factory function, exposed as window.stellarAdmin.alertDialog, wraps an existing <dialog> element (such as the one rendered by <sa-alert-dialog>) and returns a wrapper object with the methods below. It throws if the element cannot be found or is not a HTMLDialogElement.

Prop

Type

Wrapper methods

Prop

Type

On this page