Skip to content
Nana UI

Layout & Containers

Alert Dialog

A modal dialog that interrupts the user to confirm or cancel an action, such as deleting something that cannot be restored.

View .md Download

An alert dialog asks for a decision before something happens. Unlike a Dialog, it is announced as an alert, and a press outside it does nothing, so it is only closed by one of its buttons or by Escape.

Delete project? The project and its history are removed for everyone. This cannot be undone. Cancel Delete Delete project

Install the package for a versioned dependency:

npm install @nana-tec/ui-components

Or copy the source in and own it:

npx @nana-tec/ui add alert-dialog --framework web-components
npx @nana-tec/ui add alert-dialog --framework svelte

Lands in src/lib/components/ui/alert-dialog/ with the Dialog and Button it is built on. Requires Tailwind v4.

Import the autoloader once, in your app's entry point. Every <nana-*> tag then works anywhere, and each component downloads only the first time a page uses it:

import "@nana-tec/ui-components/autoloader";
import "@nana-tec/ui-components/theme";

The second line gives every component the Nana look and the design tokens behind it. Leave it out to style the components entirely yourself.

Import a component directly when it must be ready at once: on screen at first paint, or when you call its methods straight away. The two mix freely:

import "@nana-tec/ui-components/alert-dialog";

Copied in with npx @nana-tec/ui add alert-dialog? Import your project's own @/lib/nana-ui/autoloader.js once instead; the CLI keeps it up to date.

<script lang="ts">
import {
NanaAlertDialog,
NanaAlertDialogHeader,
NanaAlertDialogTitle,
NanaAlertDialogDescription,
NanaAlertDialogFooter,
NanaAlertDialogCancel,
NanaAlertDialogAction,
} from '$lib/components/ui/alert-dialog';
</script>

nana-alert-dialog takes the same nana-dialog-header, nana-dialog-body and nana-dialog-footer as a dialog. The header names it and the body describes it. Put your own buttons in the footer and close the dialog from them with close(); the value passed to close() arrives as detail.returnValue on nana-close, so one listener can tell which button was pressed.

dialog.addEventListener('nana-close', (event) => {
if (event.detail.returnValue === 'delete') deleteProject();
});

The title names the dialog and the description describes it. Cancel and Action are Buttons that close the dialog after their onclick runs. Cancel is secondary and Action primary unless you pass a variant.

To keep the dialog open, for example while a request runs, call event.preventDefault() in the action’s handler and close it yourself by setting open to false.

<script lang="ts">
let open = $state(false);
let deleting = $state(false);
async function remove(event: MouseEvent): Promise<void> {
event.preventDefault();
deleting = true;
await deleteProject();
deleting = false;
open = false;
}
</script>
<NanaAlertDialog bind:open>
<NanaAlertDialogHeader>
<NanaAlertDialogTitle>Delete project?</NanaAlertDialogTitle>
<NanaAlertDialogDescription>This cannot be undone.</NanaAlertDialogDescription>
</NanaAlertDialogHeader>
<NanaAlertDialogFooter>
<NanaAlertDialogCancel>Cancel</NanaAlertDialogCancel>
<NanaAlertDialogAction variant="danger" loading={deleting} onclick={remove}>
Delete
</NanaAlertDialogAction>
</NanaAlertDialogFooter>
</NanaAlertDialog>

The dialog has the alertdialog role, takes its name from the title and its description from the description. Focus moves into it on open, stays inside while it is open, and returns to whatever had it before.

The first focus goes to the least destructive choice, so pressing Enter straight away cannot delete anything.

Cancel receives it.

Give that button autofocus, as on Cancel above.

Properties

PropertyAttributeTypeDefaultDescription
ariaLabelaria-labelstring | nullnullAccessible label (use when there is no visible heading)
closeButtonclose-buttonbooleanfalseShow a close button in the panel's top corner.
closeLabelclose-labelstring'Close'Names the close button for assistive tech.
noBackdropCloseno-backdrop-closebooleanfalseDon't close when the backdrop is clicked
noEscapeCloseno-escape-closebooleanfalseDon't close when Escape is pressed
openopenbooleanfalseWhether the dialog is open
placementplacementDialogPlacement'end'Which edge a drawer slides from
sizesizeDialogSize'md'Panel size
variantvariantDialogVariant'alert'Behaviour / placement style

Methods

MethodDescription
close(returnValue?: string): voidClose the dialog
show(): voidOpen the dialog

Events

EventDescription
nana-openFired after the dialog opens
nana-closeFired after the dialog closes (detail.returnValue)

Slots

SlotDescription
(default)Dialog content (header / body / footer)

CSS parts

PartDescription
::part(dialog)The native <dialog> element
::part(panel)The content surface
::part(close-button)The built-in close button

CSS custom properties

PropertyDescription
--nana-dialog-widthPanel width (per size by default)
--nana-dialog-backdropBackdrop colour
--nana-dialog-radiusPanel corner radius
--nana-dialog-bgPanel background
--nana-dialog-durationHow long the dialog takes to open; closing takes three quarters of it
--nana-dialog-sheet-durationHow long the dialog takes to rise as a bottom sheet on phones

NanaAlertDialog

PropTypeDefault
openbind:booleanfalse
onOpenChange(open: boolean) => void
sizeAlertDialogSize'md'
labelstring
dismissibleboolean
childrenSnippet