Skip to content
VinUIComponentsAlert Dialog

Alert Dialog

Asks the user to confirm before something that can't be undone, like deleting. Same markup and props in every style: switch the style in the header to see it change.

Basic

Confirm a delete. In HTML both buttons submit a form[method=dialog]; read the answer from dialog.returnValue.

Delete this project?

This deletes "Website redesign" and its 24 files for everyone. You can't undo it.

alert-dialog-basic.html
<button type="button" class="vin-btn vin-btn--danger" commandfor="adlg-delete" command="show-modal">Delete project</button>
<dialog class="vin-dialog vin-alert-dialog" id="adlg-delete" role="alertdialog" aria-labelledby="adlg-delete-title" aria-describedby="adlg-delete-desc">
  <div class="vin-dialog__header">
    <h2 class="vin-dialog__title" id="adlg-delete-title">Delete this project?</h2>
    <p class="vin-dialog__description" id="adlg-delete-desc">This deletes "Website redesign" and its 24 files for everyone. You can't undo it.</p>
  </div>
  <form method="dialog" class="vin-dialog__footer">
    <button type="submit" class="vin-btn vin-btn--outline" value="cancel" autofocus>Cancel</button>
    <button type="submit" class="vin-btn vin-btn--danger" value="delete">Delete project</button>
  </form>
</dialog>

With an icon

A warning icon beside the title.

Revoke this API key?

Apps that use it stop working at once. Make a new key first if you still need access.

alert-dialog-icon.html
<button type="button" class="vin-btn vin-btn--outline" commandfor="adlg-revoke" command="show-modal">Revoke key</button>
<dialog class="vin-dialog vin-alert-dialog" id="adlg-revoke" role="alertdialog" aria-labelledby="adlg-revoke-title" aria-describedby="adlg-revoke-desc">
  <div class="vin-dialog__header">
    <svg class="vin-alert-dialog__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10.3 3.9 1.8 18a2 2 0 0 0 1.7 3h17a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0Z"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>
    <h2 class="vin-dialog__title" id="adlg-revoke-title">Revoke this API key?</h2>
    <p class="vin-dialog__description" id="adlg-revoke-desc">Apps that use it stop working at once. Make a new key first if you still need access.</p>
  </div>
  <form method="dialog" class="vin-dialog__footer">
    <button type="submit" class="vin-btn vin-btn--outline" value="cancel" autofocus>Keep key</button>
    <button type="submit" class="vin-btn vin-btn--danger" value="revoke">Revoke key</button>
  </form>
</dialog>

Discard changes

A softer question: the confirm button isn't red (variant="default").

Discard your changes?

You have edits that aren't saved yet.

alert-dialog-discard.html
<button type="button" class="vin-btn vin-btn--outline" commandfor="adlg-discard" command="show-modal">Close editor</button>
<dialog class="vin-dialog vin-alert-dialog" id="adlg-discard" role="alertdialog" aria-labelledby="adlg-discard-title" aria-describedby="adlg-discard-desc">
  <div class="vin-dialog__header">
    <h2 class="vin-dialog__title" id="adlg-discard-title">Discard your changes?</h2>
    <p class="vin-dialog__description" id="adlg-discard-desc">You have edits that aren't saved yet.</p>
  </div>
  <form method="dialog" class="vin-dialog__footer">
    <button type="submit" class="vin-btn vin-btn--outline" value="cancel" autofocus>Keep editing</button>
    <button type="submit" class="vin-btn" value="discard">Discard</button>
  </form>
</dialog>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add alert-dialog
React
import { AlertDialog } from "@/components/vin/alert-dialog";
Vue
import AlertDialog from "@/components/vin/alert-dialog.vue";
Svelte
import AlertDialog from "$lib/components/vin/alert-dialog.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. It needs no JavaScript.

API

From spec/alert-dialog.json, which every framework version is tested against.

PropValuesDefaultIn HTML
trigger
The button that opens it.
ReactElement—button[commandfor="id"][command="show-modal"]
title / descriptionReactNode—.vin-dialog__title, .vin-dialog__description
action
The confirm button's text.
string"Continue"—
cancel
The cancel button's text. It has focus when the dialog opens.
string"Cancel"—
variant
The confirm button's look.
defaultdangerdanger—
icon
A warning icon beside the title.
ReactNode—svg.vin-alert-dialog__icon as the header's first child
onAction
Called when the user confirms.
() => void—dialog.returnValue in the close event
open / defaultOpen
Use onOpenChange to hear about changes.
boolean——

Parts

React, Vue, SvelteHTML classes
<AlertDialog>.vin-alert-dialog.vin-alert-dialog__icon

Accessibility

  • role="alertdialog" tells screen readers it needs an answer; the title and description are read out when it opens.
  • Focus starts on the safe answer (autofocus), so Enter never deletes by accident.
  • There's no close button and a click outside doesn't close it. Escape cancels.
  • Say what will happen in the button text ("Delete project", not "OK").