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.
<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>import { AlertDialog } from "@/components/vin/alert-dialog";
import { Button } from "@/components/vin/button";
export function AlertDialogBasic() {
return (
<AlertDialog
trigger={<Button variant="danger">Delete project</Button>}
title="Delete this project?"
description={`This deletes "Website redesign" and its 24 files for everyone. You can't undo it.`}
action="Delete project"
onAction={() => console.log("deleted")}
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import AlertDialog from "@/components/vin/alert-dialog.vue";
import Button from "@/components/vin/button.vue";
const description = `This deletes "Website redesign" and its 24 files for everyone. You can't undo it.`;
const onDelete = () => console.log("deleted");
</script>
<template>
<AlertDialog
title="Delete this project?"
:description="description"
action="Delete project"
@action="onDelete"
>
<template #trigger><Button variant="danger">Delete project</Button></template>
</AlertDialog>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import AlertDialog from "$lib/components/vin/alert-dialog.svelte";
import Button from "$lib/components/vin/button.svelte";
</script>
<AlertDialog
title="Delete this project?"
description={`This deletes "Website redesign" and its 24 files for everyone. You can't undo it.`}
action="Delete project"
onAction={() => console.log("deleted")}
>
{#snippet trigger(props)}<Button variant="danger" {...props}>Delete project</Button>{/snippet}
</AlertDialog>With an icon
A warning icon beside the title.
<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>import { AlertDialog } from "@/components/vin/alert-dialog";
import { Button } from "@/components/vin/button";
export function AlertDialogIcon() {
return (
<AlertDialog
trigger={<Button variant="outline">Revoke key</Button>}
icon
title="Revoke this API key?"
description="Apps that use it stop working at once. Make a new key first if you still need access."
cancel="Keep key"
action="Revoke key"
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import AlertDialog from "@/components/vin/alert-dialog.vue";
import Button from "@/components/vin/button.vue";
</script>
<template>
<AlertDialog
icon
title="Revoke this API key?"
description="Apps that use it stop working at once. Make a new key first if you still need access."
cancel="Keep key"
action="Revoke key"
>
<template #trigger><Button variant="outline">Revoke key</Button></template>
</AlertDialog>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import AlertDialog from "$lib/components/vin/alert-dialog.svelte";
import Button from "$lib/components/vin/button.svelte";
</script>
<AlertDialog
icon
title="Revoke this API key?"
description="Apps that use it stop working at once. Make a new key first if you still need access."
cancel="Keep key"
action="Revoke key"
>
{#snippet trigger(props)}<Button variant="outline" {...props}>Revoke key</Button>{/snippet}
</AlertDialog>Discard changes
A softer question: the confirm button isn't red (variant="default").
<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>import { AlertDialog } from "@/components/vin/alert-dialog";
import { Button } from "@/components/vin/button";
export function AlertDialogDiscard() {
return (
<AlertDialog
trigger={<Button variant="outline">Close editor</Button>}
title="Discard your changes?"
description="You have edits that aren't saved yet."
cancel="Keep editing"
action="Discard"
variant="default"
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import AlertDialog from "@/components/vin/alert-dialog.vue";
import Button from "@/components/vin/button.vue";
</script>
<template>
<AlertDialog
title="Discard your changes?"
description="You have edits that aren't saved yet."
cancel="Keep editing"
action="Discard"
variant="default"
>
<template #trigger><Button variant="outline">Close editor</Button></template>
</AlertDialog>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import AlertDialog from "$lib/components/vin/alert-dialog.svelte";
import Button from "$lib/components/vin/button.svelte";
</script>
<AlertDialog title="Discard your changes?" description="You have edits that aren't saved yet." cancel="Keep editing" action="Discard" variant="default">
{#snippet trigger(props)}<Button variant="outline" {...props}>Close editor</Button>{/snippet}
</AlertDialog>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:
npx @vinish/ui add alert-dialogimport { AlertDialog } from "@/components/vin/alert-dialog";import AlertDialog from "@/components/vin/alert-dialog.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
triggerThe button that opens it. | ReactElement | — | button[commandfor="id"][command="show-modal"] |
title / description | ReactNode | — | .vin-dialog__title, .vin-dialog__description |
actionThe confirm button's text. | string | "Continue" | — |
cancelThe cancel button's text. It has focus when the dialog opens. | string | "Cancel" | — |
variantThe confirm button's look. | defaultdanger | danger | — |
iconA warning icon beside the title. | ReactNode | — | svg.vin-alert-dialog__icon as the header's first child |
onActionCalled when the user confirms. | () => void | — | dialog.returnValue in the close event |
open / defaultOpenUse onOpenChange to hear about changes. | boolean | — | — |
Parts
| React, Vue, Svelte | HTML 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").
Customize
Saved in this browser. Style and accent are in the header.