Skip to content
Likable Aurora @likable-hair/svelte 5

ConfirmDialog

Dialog that asks to confirm an action, usually a delete: an icon in the tone of the action, the question as title, a description and two buttons, Cancel and the action. It is a Dialog with role="alertdialog", so it keeps its behaviour (top layer, focus trap, Escape, click on the backdrop). Focus starts on Cancel with variant="danger" and on the action with variant="primary". After onconfirm it closes by itself; when onconfirm returns a Promise it shows a spinner on the action and blocks every way of closing until it settles, then closes if it resolved and stays open if it rejected. closeOnConfirm={false} or event.preventDefault() in onconfirm keep it open. oncancel runs on every other way of closing: Cancel, the close button, Escape, the backdrop. Its state is exposed as data-variant and data-loading on the <dialog> for app CSS.

import { ConfirmDialog } from '@likable-hair/svelte';

src/lib/components/composed/dialogs/ConfirmDialog.svelte · Markdown for LLMs

Delete

The usual case: onconfirm returns the Promise of the request, the dialog shows a spinner on the action and blocks closing while it runs, then closes by itself. The title names the thing; without confirmText the button says "Delete".

Result: none
Code
<script lang="ts">
	import { mdiTrashCanOutline } from '@mdi/js';
	import { Button, ConfirmDialog } from '@likable-hair/svelte';

	let open = $state(false);
	let result = $state('none');

	function deleteClient() {
		return new Promise((resolve) => setTimeout(resolve, 1200)).then(() => (result = 'deleted'));
	}
</script>

<Button variant="danger" icon={mdiTrashCanOutline} onclick={() => (open = true)}>Delete client</Button>
<span class="result">Result: {result}</span>

<ConfirmDialog
	bind:open
	title="Delete the client?"
	description="Giulia Marini and her 48 appointments will be removed for good."
	icon={mdiTrashCanOutline}
	onconfirm={deleteClient}
	oncancel={() => (result = 'cancelled')}
/>

<style>
	.result {
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>

Not destructive

variant="primary" is for actions that can be undone or are not dangerous: the icon and the button take the primary color, the button says "Confirm" unless confirmText changes it, and it gets the focus. An array in description renders one paragraph per item.

Sent: 0
Code
<script lang="ts">
	import { mdiSendOutline } from '@mdi/js';
	import { Button, ConfirmDialog } from '@likable-hair/svelte';

	let open = $state(false);
	let sent = $state(0);
</script>

<Button icon={mdiSendOutline} onclick={() => (open = true)}>Send invoices</Button>
<span class="result">Sent: {sent}</span>

<ConfirmDialog
	bind:open
	variant="primary"
	title="Send 12 invoices?"
	description={[
		'Every client gets an email with the PDF of the invoice.',
		'You can send them again from the invoice page.'
	]}
	icon={mdiSendOutline}
	confirmText="Send"
	onconfirm={() => {
		sent += 12;
	}}
/>

<style>
	.result {
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>

Errors

When the request fails, event.preventDefault() keeps the dialog open, so the user can try again or cancel; children shows the error under the description. A Promise that rejects keeps it open too, and the error goes on to the console.

Code
<script lang="ts">
	import { mdiArchiveOutline } from '@mdi/js';
	import { AlertBanner, Button, ConfirmDialog } from '@likable-hair/svelte';

	let open = $state(false);
	let error = $state<string>();
	let attempts = 0;

	async function archive(event: MouseEvent) {
		error = undefined;
		await new Promise((resolve) => setTimeout(resolve, 800));
		if (++attempts % 2 === 1) {
			event.preventDefault();
			error = 'The server did not answer. Try again.';
		}
	}
</script>

<Button
	variant="secondary"
	icon={mdiArchiveOutline}
	onclick={() => {
		error = undefined;
		open = true;
	}}>Archive project</Button
>

<ConfirmDialog
	bind:open
	variant="primary"
	title="Archive the project?"
	description="It leaves the dashboard; you can restore it from the archive."
	icon={mdiArchiveOutline}
	confirmText="Archive"
	onconfirm={archive}
>
	{#if error}
		<AlertBanner variant="error" description={error} />
	{/if}
</ConfirmDialog>

Type to confirm

For actions that cannot be undone and touch a lot of data, a field in children and confirmDisable make the user type the name before the button turns on; autofocus moves the focus to the field.

Code
<script lang="ts">
	import { mdiDatabaseRemoveOutline } from '@mdi/js';
	import { Button, ConfirmDialog, SimpleTextField } from '@likable-hair/svelte';

	const name = 'north-salon';

	let open = $state(false);
	let typed = $state('');
</script>

<Button
	variant="danger"
	icon={mdiDatabaseRemoveOutline}
	onclick={() => {
		typed = '';
		open = true;
	}}>Delete workspace</Button
>

<ConfirmDialog
	bind:open
	title="Delete the workspace?"
	description="Clients, appointments and invoices of {name} will be deleted. This cannot be undone."
	icon={mdiDatabaseRemoveOutline}
	confirmText="Delete workspace"
	confirmDisable={typed !== name}
	onconfirm={() => new Promise((resolve) => setTimeout(resolve, 1000))}
>
	<!-- svelte-ignore a11y_autofocus -->
	<SimpleTextField label="Type {name} to confirm" bind:value={typed} autocomplete="off" autofocus />
</ConfirmDialog>

Closed by the app

With closeOnConfirm={false} the app decides when to close by setting open, and drives the spinner with loading, as the copies of the dialog in the apps did. actionsSnippet replaces the buttons: here a third action, with confirm and cancel from the snippet to keep the behaviour.

Result: none
Code
<script lang="ts">
	import { mdiFileDocumentRemoveOutline } from '@mdi/js';
	import { Button, ConfirmDialog } from '@likable-hair/svelte';

	let open = $state(false);
	let loading = $state(false);
	let result = $state('none');

	async function remove(what: string) {
		loading = true;
		await new Promise((resolve) => setTimeout(resolve, 1000));
		loading = false;
		result = what;
		open = false;
	}
</script>

<Button variant="secondary" icon={mdiFileDocumentRemoveOutline} onclick={() => (open = true)}
	>Remove report</Button
>
<span class="result">Result: {result}</span>

<ConfirmDialog
	bind:open
	{loading}
	closeOnConfirm={false}
	title="Remove the report?"
	description="You can remove only this version or every version of the report."
	icon={mdiFileDocumentRemoveOutline}
	onconfirm={() => remove('this version')}
>
	{#snippet actionsSnippet({ confirm, cancel, loading })}
		<Button variant="secondary" disabled={loading} onclick={cancel}>Cancel</Button>
		<Button variant="danger" buttonType="text" disabled={loading} onclick={() => remove('all versions')}
			>All versions</Button
		>
		<Button variant="danger" {loading} onclick={confirm}>This version</Button>
	{/snippet}
</ConfirmDialog>

<style>
	.result {
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>

Props

NameTypeDefaultDescription
title requiredstringThe question, as the title of the dialog. It names the thing and the action ("Delete Mario Rossi?"), not "Are you sure?".
description string | string[]Text under the title. An array renders one paragraph per item; line breaks are kept.
variant 'danger' | 'primary''danger'Tone of the icon and of the action button: danger for destructive actions, primary for the others.
icon stringSVG path of the icon above the title. Defaults to an icon for each variant; an empty string removes it.
confirmText stringText of the action button. Defaults to "Delete" with variant="danger" and "Confirm" with variant="primary", in the language of locale.
cancelText stringText of the cancel button. Defaults to "Cancel", in the language of locale.
loading booleanfalseShows a spinner on the action button and blocks every way of closing. A Promise returned by onconfirm does the same by itself.
confirmDisable booleanfalseDisables the action button.
cancelDisable booleanfalseDisables the cancel and close buttons, Escape and the click on the backdrop.
closable booleantrueShows a close button in the top right corner.
closeOnConfirm booleantrueCloses the dialog after onconfirm, once its Promise resolves. With false the app closes it by setting open.
class { dialog?: string; surface?: string; icon?: string; title?: string; description?: string; content?: string; actions?: string; cancel?: string; confirm?: string; }{}Extra classes for each part.

From Dialog

It accepts the props, snippets and events of Dialog, listed in the tables "From Dialog". Except onclose, closeSnippet, topRightSnippet, centerLeftSnippet, centerRightSnippet.

NameTypeDefaultDescription
open bindable booleanWhether the dialog is open. undefined counts as closed.
persistent booleanfalseKeeps the dialog open on Escape and on a click on the backdrop. The close button, close from a snippet and a <form method="dialog"> still close it.
closeLabel stringAccessible name of the close button. Defaults to "Close", in the language of locale.
locale stringBCP 47 locale of the texts: it and en are built in, other languages fall back to English. Defaults to the one set with setDefaultLocale, else en.
dialogElement bindable HTMLDialogElementThe <dialog> element.

It also accepts the native attributes and events of <dialog> (for example id, title, aria-label, onkeydown), which are forwarded to the element.

Snippets

NameParametersDescription
childrenContent under the description, for example a field to type before confirming.
titleSnippet{ title: string }Replaces the title text, inside the heading.
iconSnippet{ icon: string; variant: 'danger' | 'primary' }Replaces the icon box.
actionsSnippet{ confirm: (event: MouseEvent) => void; cancel: (event: Event) => void; loading: boolean; }Replaces the two buttons. Call confirm and cancel from your buttons to keep the behaviour.

Events

NameTypeDescription
onconfirm(event: MouseEvent) => void | Promise<unknown>Called on a click on the action button. It may return a Promise: the dialog waits for it with a spinner, closes if it resolves and stays open if it rejects. event.preventDefault() keeps the dialog open.
oncancel(event: Event) => voidCalled when the dialog closes without confirming: Cancel, the close button, Escape, the click on the backdrop.

CSS variables

For a single instance, pass them as style props: <ConfirmDialog --confirm-dialog-icon-color="tomato">. The --confirm-dialog-default-* defaults apply to the whole app: override them in :root or in a theme.

It also takes the variables of Dialog.

Per-instance variableDefault
--confirm-dialog-max-width
--confirm-dialog-default-max-width 420px
--confirm-dialog-icon-gap
--confirm-dialog-default-icon-gap 16px
--confirm-dialog-icon-glyph-size
--confirm-dialog-default-icon-glyph-size 20px
--confirm-dialog-icon-size
--confirm-dialog-default-icon-size 40px
--confirm-dialog-icon-border-radius
--confirm-dialog-default-icon-border-radius var(--global-radius-md)
--confirm-dialog-icon-background
--confirm-dialog-default-danger-icon-background var(--global-color-error-soft)
--confirm-dialog-default-primary-icon-background var(--global-color-primary-soft)
--confirm-dialog-icon-color
--confirm-dialog-default-danger-icon-color var(--global-color-error)
--confirm-dialog-default-primary-icon-color var(--global-color-primary)
--confirm-dialog-icon-ring-width
--confirm-dialog-default-icon-ring-width 6px
--confirm-dialog-icon-ring-color
--confirm-dialog-default-danger-icon-ring-color color-mix(in oklab, var(--global-color-error) 6%, transparent)
--confirm-dialog-default-primary-icon-ring-color var(--global-color-primary-soft)
--confirm-dialog-paragraph-gap
--confirm-dialog-default-paragraph-gap 8px
--confirm-dialog-content-gap
--confirm-dialog-default-content-gap 16px