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.
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.
Preview
Section titled “Preview”<nana-alert-dialog id="alert-dialog-preview">
<nana-dialog-header>Delete project?</nana-dialog-header>
<nana-dialog-body>
The project and its history are removed for everyone. This cannot be undone.
</nana-dialog-body>
<nana-dialog-footer>
<nana-button
variant="secondary"
autofocus
onclick="this.closest('nana-alert-dialog').close()"
>
Cancel
</nana-button>
<nana-button
variant="danger"
onclick="this.closest('nana-alert-dialog').close('delete')"
>
Delete
</nana-button>
</nana-dialog-footer>
</nana-alert-dialog>
<nana-button
variant="danger"
onclick="document.getElementById('alert-dialog-preview').show()"
>
Delete project
</nana-button> <script lang="ts">
import { NanaButton } from '$lib/components/ui/button';
import {
NanaAlertDialog,
NanaAlertDialogAction,
NanaAlertDialogCancel,
NanaAlertDialogDescription,
NanaAlertDialogFooter,
NanaAlertDialogHeader,
NanaAlertDialogTitle,
} from '$lib/components/ui/alert-dialog';
let open = $state(false);
</script>
<NanaButton variant="danger" onclick={() => (open = true)}>Delete project</NanaButton>
<NanaAlertDialog bind:open>
<NanaAlertDialogHeader>
<NanaAlertDialogTitle>Delete project?</NanaAlertDialogTitle>
<NanaAlertDialogDescription>
The project and its history are removed for everyone. This cannot be undone.
</NanaAlertDialogDescription>
</NanaAlertDialogHeader>
<NanaAlertDialogFooter>
<NanaAlertDialogCancel>Cancel</NanaAlertDialogCancel>
<NanaAlertDialogAction variant="danger">Delete</NanaAlertDialogAction>
</NanaAlertDialogFooter>
</NanaAlertDialog> Installation
Section titled “Installation”Install the package for a versioned dependency:
npm install @nana-tec/ui-componentsOr copy the source in and own it:
npx @nana-tec/ui add alert-dialog --framework web-componentsnpx @nana-tec/ui add alert-dialog --framework svelteLands in src/lib/components/ui/alert-dialog/ with the Dialog and
Button it is built on. Requires Tailwind v4.
Import
Section titled “Import”
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>Anatomy
Section titled “Anatomy”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>Accessibility
Section titled “Accessibility”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
| Property | Attribute | Type | Default | Description |
|---|---|---|---|---|
ariaLabel | aria-label | string | null | null | Accessible label (use when there is no visible heading) |
closeButton | close-button | boolean | false | Show a close button in the panel's top corner. |
closeLabel | close-label | string | 'Close' | Names the close button for assistive tech. |
noBackdropClose | no-backdrop-close | boolean | false | Don't close when the backdrop is clicked |
noEscapeClose | no-escape-close | boolean | false | Don't close when Escape is pressed |
open | open | boolean | false | Whether the dialog is open |
placement | placement | DialogPlacement | 'end' | Which edge a drawer slides from |
size | size | DialogSize | 'md' | Panel size |
variant | variant | DialogVariant | 'alert' | Behaviour / placement style |
Methods
| Method | Description |
|---|---|
close(returnValue?: string): void | Close the dialog |
show(): void | Open the dialog |
Events
| Event | Description |
|---|---|
nana-open | Fired after the dialog opens |
nana-close | Fired after the dialog closes (detail.returnValue) |
Slots
| Slot | Description |
|---|---|
(default) | Dialog content (header / body / footer) |
CSS parts
| Part | Description |
|---|---|
::part(dialog) | The native <dialog> element |
::part(panel) | The content surface |
::part(close-button) | The built-in close button |
CSS custom properties
| Property | Description |
|---|---|
--nana-dialog-width | Panel width (per size by default) |
--nana-dialog-backdrop | Backdrop colour |
--nana-dialog-radius | Panel corner radius |
--nana-dialog-bg | Panel background |
--nana-dialog-duration | How long the dialog takes to open; closing takes three quarters of it |
--nana-dialog-sheet-duration | How long the dialog takes to rise as a bottom sheet on phones |
NanaAlertDialog
| Prop | Type | Default |
|---|---|---|
openbind: | boolean | false |
onOpenChange | (open: boolean) => void | |
size | AlertDialogSize | 'md' |
label | string | |
dismissible | boolean | |
children | Snippet |