Alert Dialog Examples
The examples below assume that you have imported the components and set up the theme.
Basic
Ask the user to confirm an action before it runs. confirm-label names the Confirm button, the actionclicked event reports a confirm, and the dismissed event reports Cancel and Escape.
The controls add an intent icon, color Confirm for a destructive action, move the first focus, and turn off Escape.
Sign out of all devices?
You will need to sign in again on every device you use.
<div class="basic-example"> <button type="button" class="btn" data-sv-dialog-open="sign-out">Sign out</button> <output id="sign-out-result">No choice yet</output></div>
<sv-alert-dialog id="sign-out" aria-labelledby="sign-out-title" aria-describedby="sign-out-message" confirm-label="Sign out"> <h2 id="sign-out-title" slot="heading">Sign out of all devices?</h2> <p id="sign-out-message">You will need to sign in again on every device you use.</p></sv-alert-dialog>
<script type="application/x-typescript" module> const alertDialog = document.querySelector<AlertDialog>("#sign-out")!; const result = document.querySelector("#sign-out-result")!;
alertDialog.addEventListener("actionclicked", () => (result.textContent = "Signed out")); alertDialog.addEventListener("dismissed", () => (result.textContent = "Canceled"));</script>
<style> .basic-example { display: grid; gap: 0.75em; place-content: center; place-items: center; }</style>OK Only
Show a notice that the user only acknowledges. cancel-label="" hides the Cancel button, confirm-label="OK" names the one button left, and variant="success" adds the success icon.
Draft saved
You can find it in Drafts and keep writing at any time.
<div class="ok-only-example"> <button type="button" class="btn" data-sv-dialog-open="draft-saved">Save draft</button></div>
<sv-alert-dialog id="draft-saved" aria-labelledby="draft-saved-title" aria-describedby="draft-saved-message" variant="success" cancel-label="" confirm-label="OK"> <h2 id="draft-saved-title" slot="heading">Draft saved</h2> <p id="draft-saved-message">You can find it in Drafts and keep writing at any time.</p></sv-alert-dialog>
<style> .ok-only-example { display: grid; place-content: center; }</style>Destructive
Confirm an action that cannot be undone. destructive colors the Confirm button with the error color, and variant="error" adds the error icon.
Delete this repository?
The code, issues, and releases are deleted for good.
<div class="destructive-example"> <button type="button" class="btn" data-sv-dialog-open="delete-repository">Delete</button></div>
<sv-alert-dialog id="delete-repository" aria-labelledby="delete-repository-title" aria-describedby="delete-repository-message" variant="error" destructive confirm-label="Delete"> <h2 id="delete-repository-title" slot="heading">Delete this repository?</h2> <p id="delete-repository-message">The code, issues, and releases are deleted for good.</p></sv-alert-dialog>
<style> .destructive-example { display: grid; place-content: center; }</style>Three Buttons
Offer a second action next to Confirm, like "Don't save" in a save prompt. secondary-label shows the Secondary button, and event.detail.action in actionclicked tells "confirm" and "secondary" apart.
Save changes?
Your changes are lost if you don't save them.
<div class="three-buttons-example"> <button type="button" class="btn" data-sv-dialog-open="unsaved-changes">Close document</button> <output id="unsaved-changes-result">No choice yet</output></div>
<sv-alert-dialog id="unsaved-changes" aria-labelledby="unsaved-changes-title" aria-describedby="unsaved-changes-message" variant="warning" confirm-label="Save" secondary-label="Don't save"> <h2 id="unsaved-changes-title" slot="heading">Save changes?</h2> <p id="unsaved-changes-message">Your changes are lost if you don't save them.</p></sv-alert-dialog>
<script type="application/x-typescript" module> const alertDialog = document.querySelector<AlertDialog>("#unsaved-changes")!; const result = document.querySelector("#unsaved-changes-result")!;
alertDialog.addEventListener("actionclicked", event => { result.textContent = event.detail.action === "confirm" ? "Saved" : "Not saved"; });
alertDialog.addEventListener("dismissed", () => (result.textContent = "Canceled"));</script>
<style> .three-buttons-example { display: grid; gap: 0.75em; place-items: center; }</style>Wait for the Result
Open the dialog from a script and wait for the user's choice. openAndWaitForResult() opens the dialog and resolves with "confirm", "secondary", or "" when it closes.
Leave the Design team?
You lose access to its projects until an admin invites you again.
<div class="wait-for-result-example"> <button id="leave-team-button" type="button" class="btn">Leave the team</button> <output id="leave-team-result">No choice yet</output></div>
<sv-alert-dialog id="leave-team" aria-labelledby="leave-team-title" aria-describedby="leave-team-message" destructive confirm-label="Leave"> <h2 id="leave-team-title" slot="heading">Leave the Design team?</h2> <p id="leave-team-message">You lose access to its projects until an admin invites you again.</p></sv-alert-dialog>
<script type="application/x-typescript" module> const alertDialog = document.querySelector<AlertDialog>("#leave-team")!; const button = document.querySelector("#leave-team-button")!; const result = document.querySelector("#leave-team-result")!;
button.addEventListener("click", async () => { const answer = await alertDialog.openAndWaitForResult(); result.textContent = answer === "confirm" ? "You left the team" : "You stayed"; });</script>
<style> .wait-for-result-example { display: grid; gap: 0.75em; place-items: center; }</style>