Skip to content
Statix Design System

Overlays

Alert Dialog

A dialog that interrupts to confirm a step that is hard to undo, such as deleting a mailbox. It only closes through one of its buttons or Escape.

import * as AlertDialog from '@statix/ui/alert-dialog';

Playground

Controls
<AlertDialog.Root bind:open>
	<AlertDialog.Trigger class={buttonVariants({ variant: 'outline' })}>
		Delete mailbox
	</AlertDialog.Trigger>
	<AlertDialog.Content>
		<AlertDialog.Header>
			<AlertDialog.Media tone="destructive"><TrashIcon /></AlertDialog.Media>
			<AlertDialog.Title>Delete mailbox info@yoga-berger.de?</AlertDialog.Title>
			<AlertDialog.Description>
				All emails in this mailbox are deleted. You can’t undo this.
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel>Keep mailbox</AlertDialog.Cancel>
			<AlertDialog.Action variant="destructive" onclick={() => (open = false)}>
				Delete mailbox
			</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>

Examples

Delete confirmation

Name the exact thing in the title and say what is lost. The destructive button repeats the verb from the title.

<script lang="ts">
	import * as AlertDialog from '@statix/ui/alert-dialog';
	import { buttonVariants } from '@statix/ui/button';

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

<AlertDialog.Root bind:open>
	<AlertDialog.Trigger class={buttonVariants({ variant: 'outline' })}>
		Delete mailbox
	</AlertDialog.Trigger>
	<AlertDialog.Content>
		<AlertDialog.Header>
			<AlertDialog.Title>Delete mailbox info@yoga-berger.de?</AlertDialog.Title>
			<AlertDialog.Description>
				All 2,412 emails and 8 folders in this mailbox are deleted. You can’t undo this.
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel>Keep mailbox</AlertDialog.Cancel>
			<AlertDialog.Action variant="destructive" onclick={() => (open = false)}>
				Delete mailbox
			</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>

With an icon

AlertDialog.Media puts a round icon next to the title. Pick a tone that matches the risk: destructive, warning, primary, success or neutral.

<script lang="ts">
	import * as AlertDialog from '@statix/ui/alert-dialog';
	import { buttonVariants } from '@statix/ui/button';
	import TriangleAlertIcon from '@lucide/svelte/icons/triangle-alert';

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

<AlertDialog.Root bind:open>
	<AlertDialog.Trigger class={buttonVariants({ variant: 'outline' })}>
		Cancel contract
	</AlertDialog.Trigger>
	<AlertDialog.Content>
		<AlertDialog.Header>
			<AlertDialog.Media tone="warning">
				<TriangleAlertIcon />
			</AlertDialog.Media>
			<AlertDialog.Title>Cancel the Business plan for yoga-berger.de?</AlertDialog.Title>
			<AlertDialog.Description>
				Your website stays online until 31 December 2026. After that we delete it together with its
				backups and mailboxes.
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel>Keep plan</AlertDialog.Cancel>
			<AlertDialog.Action variant="dark" onclick={() => (open = false)}>
				Cancel contract
			</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>

Small

size="sm" centres the content in a 360px card and gives both buttons the same width. Good for short yes or no questions.

<script lang="ts">
	import * as AlertDialog from '@statix/ui/alert-dialog';
	import { buttonVariants } from '@statix/ui/button';
	import LogOutIcon from '@lucide/svelte/icons/log-out';

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

<AlertDialog.Root bind:open>
	<AlertDialog.Trigger class={buttonVariants({ variant: 'outline' })}>
		Sign out everywhere
	</AlertDialog.Trigger>
	<AlertDialog.Content size="sm">
		<AlertDialog.Header>
			<AlertDialog.Media>
				<LogOutIcon />
			</AlertDialog.Media>
			<AlertDialog.Title>Sign out on all devices?</AlertDialog.Title>
			<AlertDialog.Description>
				You stay signed in here. Everywhere else you need your password again.
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
			<AlertDialog.Action onclick={() => (open = false)}>Sign out</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>

Waiting for the server

AlertDialog.Action does not close the dialog by itself. Bind open, run the request, then close it. Disable both buttons while it runs.

<script lang="ts">
	import * as AlertDialog from '@statix/ui/alert-dialog';
	import { buttonVariants } from '@statix/ui/button';
	import { Spinner } from '@statix/ui/spinner';
	import TrashIcon from '@lucide/svelte/icons/trash-2';

	let open = $state(false);
	let deleting = $state(false);

	async function deleteBackup() {
		deleting = true;
		await new Promise((resolve) => setTimeout(resolve, 1200));
		deleting = false;
		open = false;
	}
</script>

<AlertDialog.Root bind:open>
	<AlertDialog.Trigger class={buttonVariants({ variant: 'ghost' })}>
		<TrashIcon data-icon="inline-start" />
		Delete backup
	</AlertDialog.Trigger>
	<AlertDialog.Content>
		<AlertDialog.Header>
			<AlertDialog.Media tone="destructive">
				<TrashIcon />
			</AlertDialog.Media>
			<AlertDialog.Title>Delete the backup from 17 November, 03:10?</AlertDialog.Title>
			<AlertDialog.Description>
				You can no longer restore yoga-berger.de to this state. Newer backups stay.
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel disabled={deleting}>Keep backup</AlertDialog.Cancel>
			<AlertDialog.Action variant="destructive" disabled={deleting} onclick={deleteBackup}>
				{#if deleting}<Spinner data-icon="inline-start" />{/if}
				Delete backup
			</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>

Guidelines

Do

  • Use it only for actions that delete data, cost money or are hard to undo.
  • Write the title as a question that names the object: "Delete mailbox info@yoga-berger.de?"
  • Label the cancel button with what the user keeps: "Keep mailbox".
  • Use variant="destructive" on the action when data is deleted.

Don't

  • Don’t use it for routine confirmations. Too many warnings teach people to click through.
  • Don’t label the buttons "Yes" and "No".
  • Don’t add a close button in the corner. Cancel is the way out.

Accessibility

  • Renders role="alertdialog" with aria-modal, named by AlertDialog.Title and described by AlertDialog.Description.
  • Statix focuses AlertDialog.Cancel when the dialog opens, so Enter never confirms by accident. Pass onOpenAutoFocus to change that.
  • A click on the scrim does nothing. Escape closes the dialog like Cancel.
  • Focus is trapped inside and returns to the trigger on close.

API reference

AlertDialog.Root

Holds the open state. Renders no element.

PropTypeDefault
open bindboolean

Whether the dialog is open.

false
onOpenChange (open: boolean) => void

Called when the dialog opens or closes.

–

AlertDialog.Trigger

renders <button> and accepts its attributes

Opens the dialog. Style it with buttonVariants or use the child snippet.

Data attributeMeaning
data-state"open" or "closed".

AlertDialog.Content

renders <div> and accepts its attributes

The card. Renders the portal and the scrim for you.

PropTypeDefault
size 'default' | 'sm'

default is 480px and left-aligned. sm is 360px, centred, with a two-column footer.

'default'
onOpenAutoFocus (event: Event) => void

Runs before Statix focuses Cancel. Call event.preventDefault() to focus something else.

–
portalProps AlertDialog.PortalProps

Props for the portal.

–
ref bindHTMLDivElement | null

The content element.

–
class ClassValue

Merged with the card classes.

–
Data attributeMeaning
data-slot="alert-dialog-content"Always present.
data-size"default" or "sm".
data-state"open" or "closed".

AlertDialog.Header

renders <div> and accepts its attributes

Grid for media, title and description.

AlertDialog.Media

renders <div> and accepts its attributes

A 48px round icon holder. Place it first inside the header.

PropTypeDefault
tone 'neutral' | 'primary' | 'success' | 'warning' | 'destructive'

Soft fill and icon colour. Statix addition.

'neutral'
Data attributeMeaning
data-toneThe current tone.

AlertDialog.Title

renders <div role="heading"> and accepts its attributes

Names the dialog. 24px, semibold.

PropTypeDefault
level 1 | 2 | 3 | 4 | 5 | 6

Heading level.

2

AlertDialog.Description

renders <div> and accepts its attributes

Says what happens. 15px in ink-2.

AlertDialog.Footer

renders <div> and accepts its attributes

Right-aligned buttons. Stacked on phones with the action on top.

AlertDialog.Cancel

renders <button> and accepts its attributes

Closes the dialog. Styled as a Button.

PropTypeDefault
variant ButtonVariant

Any Button variant. shadcn uses outline; Statix uses ghost.

'ghost'
size ButtonSize

Any Button size.

'md'

AlertDialog.Action

renders <button> and accepts its attributes

The confirming button. It does not close the dialog; set open to false in onclick.

PropTypeDefault
variant ButtonVariant

Use destructive when data is deleted.

'primary'
size ButtonSize

Any Button size.

'md'

alertDialogMediaVariants

The tailwind-variants function behind AlertDialog.Media.