Searching Puzzle Pieces

Type to search Puzzle Pieces.

Alert Dialog

A confirm and cancel modal for consequential actions, on the native alertdialog element. The backdrop is inert, there is no close X, and initial focus starts on Cancel — so the choice is made deliberately, never dismissed by a stray click.

This piece is a wrapper, not a port, and a thin variant of Dialog: the same <dialog-panel> custom element from @magic-spells/dialog-panel, the same edge-triggered open flag, the same close-and-notify contract. What differs is the dismiss policy and the two buttons.

Both buttons close the prompt themselves and report through @hide, whose detail carries result: 'confirm', 'cancel', or null when Escape closed it. Your job is to flip open back to false there — see Usage.

last result: —

Delete project?

This permanently removes the project and all of its data. This action cannot be undone.

Installation

Copy the piece into your app — it compiles with your build and the code is yours:

puzzle add piece alert-dialog

Like Dialog, Sheet and Bottom Sheet, this piece has a runtime dependency: the web component it wraps. All four share it, so it is installed once.

npm install @magic-spells/dialog-panel

Then merge the component's stylesheet into your app's style entry (app/styles/styles.css). It owns the dialog and overlay transport — the state-driven fade, the backdrop element, and the page scroll lock:

/* app/styles/styles.css */
@import "tailwindcss";
@import "@magic-spells/dialog-panel/css" layer(components);

layer(components) is not optional cosmetics: the stylesheet uses plain element selectors, and an unlayered import would outrank every Tailwind utility on the panel.

Requires the pieces.css tokens merged into your app styles (see Introduction).

Usage

import AlertDialog from '@/components/ui/AlertDialog.pzl';
<AlertDialog
  open={ open }
  title="Delete project?"
  description="This permanently removes the project and all of its data."
  confirmLabel="Delete"
  destructive
  @hide={ finish } />

// events = {
//   finish: ({ result }) => {
//     this.setData('open', false);
//     if (result === 'confirm') this.deleteProject();
//   },
// };

// open is EDGE-TRIGGERED: only a change to it acts. Both buttons close the
// prompt through the component and report through @hide — result is
// 'confirm', 'cancel', or null when Escape closed it.

open is edge-triggered: only a change to it acts. An unrelated re-render never re-opens a prompt the user already answered while your state still says it is open.

Coming from the ported piece: @confirm and @cancel are gone, replaced by one @hide whose result is 'confirm', 'cancel' or null. One callback, one place to flip open back to false, and the same contract every other overlay in the library uses. title, description, confirmLabel, cancelLabel and destructive all mean what they did.

Non-destructive confirm

Publish this post?

It will be visible to everyone right away. You can unpublish later.

The dismiss policy

last result: —

Discard your draft?

Click the backdrop: nothing happens. Press Escape: it closes, and reports null.

Reference

Props

openControlled, and EDGE-TRIGGERED: only a change acts. true opens, false closes. An unrelated re-render never re-opens a prompt the user already answered.
titleHeading text, with a generated id wired to aria-labelledby. role="alertdialog" REQUIRES an accessible name, so always pass this or labelledby.
descriptionSupporting paragraph, with a generated id wired to aria-describedby.
labelledbyId of the labelling element, to wire by hand instead of using title.
describedbyId of the describing element, to wire by hand instead of using description.
confirmLabelText on the affirmative button. Default "Continue". The result it reports is always "confirm".
cancelLabelText on the safe button, which also takes initial focus. Default "Cancel". The result it reports is always "cancel".
destructiveSwaps the confirm button from the primary recipe to the danger one. Presentation only — it changes no behaviour.
classMerged onto the dialog element, after the piece's own panel classes. The panel is capped at max-w-md; a prompt is narrow.
backdropClassMerged onto the dialog-backdrop element, after the piece's overlay classes. The backdrop is still inert.

Callbacks

@hide(detail)The prompt closed. detail is { result, triggerElement }: result is "confirm", "cancel", or null when Escape closed it. Flip open back to false here, and treat anything that is not "confirm" as a cancel.
@show()The entrance settled and the native dialog is in the top layer, with focus on Cancel.

Slots

None. A prompt is config-first on purpose: title, description and two labelled buttons are the whole surface. Reach for Dialog the moment you need markup inside it.

How the dismiss policy works

dialog-panel reports neither Escape nor a backdrop click: both arrive at its cancelable beforeHide event with nothing attached and no reason. So the piece tells them apart itself. Escape reaches the dialog as a native cancel event first, and the piece listens for that in the capture phase — which runs ahead of the component's own handler regardless of which listener was registered first. A backdrop click raises no such flag and is the one path refused.

Everything else goes through: either button, and the parent flipping open back to false. A reason in the component's own hidden detail would retire the mechanism.

Known gaps

role="alertdialog" requires an accessible name, so always pass title (or wire labelledby yourself). There is no runtime warning if you forget.