Overlay <hw:alert-dialog>
View examples

Alert Dialog

Accessible alert dialog that intercepts clicks and requires user confirmation before proceeding. Works with links, buttons, form submissions, and Turbo actions.

Basic Usage

The default slot is the trigger — anything inside the component is wrapped in a click-intercept zone. The action button uses the default variant by default:

BLADE
<hw:alert-dialog title="Continue?" description="This will proceed.">
    <button type="button">Continue</button>
</hw:alert-dialog>

Destructive Action

Use confirm-variant="destructive" when the confirmed action is destructive:

BLADE
<hw:alert-dialog
    title="Delete item?"
    description="This action cannot be undone."
    confirm-label="Delete"
    confirm-variant="destructive"
>
    <button type="button">Delete</button>
</hw:alert-dialog>

With Turbo Method

BLADE
<hw:alert-dialog
    title="Delete item?"
    description="This action cannot be undone."
    confirm-label="Delete"
    confirm-variant="destructive"
>
    <a href="/items/1" data-turbo-method="delete">Delete</a>
</hw:alert-dialog>

Rich Body Content

When description isn't enough — lists of consequences, multiple paragraphs, embedded links — use the content slot:

BLADE
<hw:alert-dialog title="Archive project?" description="This will hide the project from the dashboard.">
    <button type="button">Archive</button>

    <x-slot:content>
        <ul class="mt-2 list-disc pl-5 text-sm text-muted-foreground">
            <li>Existing links keep working.</li>
            <li>Members lose write access.</li>
            <li>Restoring takes one click from the archive view.</li>
        </ul>
    </x-slot:content>
</hw:alert-dialog>

The content slot renders below description and above the action buttons.

Shared Host

Rendering one inline dialog per row is unnecessary when every action uses the same visual shell. Put one <hw:alert-dialog.host> around the collection and mark each action with <hw:alert-dialog.trigger>:

BLADE
<hw:alert-dialog.host
    title="Delete item?"
    description="This action cannot be undone."
    confirm-label="Delete"
    confirm-variant="destructive"
>
    @foreach ($items as $item)
        <article>
            <span>{{ $item->name }}</span>

            <hw:alert-dialog.trigger
                as-child
                :title="'Delete '.$item->name.'?'"
                :description="'This permanently deletes '.$item->name.'.'"
            >
                <button id="delete-item-{{ $item->id }}" type="button">Delete</button>
            </hw:alert-dialog.trigger>
        </article>
    @endforeach
</hw:alert-dialog.host>

The host renders one overlay and delegates clicks from all of its marked triggers. Trigger overrides are plain text and are reset to the host defaults after each decision. Use as-child when the slot is already one <button> or <a>; without it, the trigger renders its own button. A nested host owns its own triggers, so the nearest host always wins. Targets without a per-trigger text override retain their authored child markup, including icons and emphasized text.

The trigger component must render in the same Blade component tree as its host. A row partial rendered independently for a Turbo Stream append, prepend, or replace cannot contain <hw:alert-dialog.trigger>, even when its eventual stream target is inside a host, because Blade validates the host before the response reaches the browser. Render the host and collection together, including inside a Turbo Frame, when using this scoped mode. Referencing a separately rendered host by id is not part of this API.

While a decision is pending, later trigger clicks are swallowed without replacing the first action. If a Turbo morph may replace a trigger while the dialog is open, give the action element a stable, unique id, just as for the inline component. Moving a pending trigger into another host makes confirmation fail closed and dispatches alert-dialog:dropped.

Automatic Behavior

The default slot is wrapped in a click-intercept zone. When the user clicks any element inside, the click is swallowed and the alert dialog opens. If the user clicks Confirm, the captured action resumes after the actual exit motion settles, by clicking the trigger again. Listeners on the resolved replay node and bubbling ancestors reached by it observe the confirmed click once. Replacing a trigger with a stable id while the dialog is open does not discard the action. If the user clicks Cancel or presses Escape, the dialog closes and nothing is dispatched downstream. Rapid reopen cancels stale close completion.

Every trigger action — link, submit or generic type="button" — is deferred in capture phase and runs once after confirmation. Give the trigger a stable id when a Turbo morph may replace it while the dialog is open. Without one, only the exact original node can be resumed; an id-less replacement fails closed instead of risking another action. Changes to the resolved destination, native command/popover target, submitted non-file values, file selection metadata, or effective Turbo/Frame context also fail closed. When that happens, the component dispatches a bubbling alert-dialog:dropped event with { kind, triggerId } in event.detail, so the application can report or log the discarded confirmation.

The trigger element needs no special attributes — place it as the default slot.

Tweaking Behavior

Motion, scroll lock, and click-outside behavior are exposed as Blade props — no need to write data-*-value attributes:

BLADE
<hw:alert-dialog
    title="Are you sure?"
    motion="none"
    :lock-scroll="false"
    :close-on-click-outside="false"
>
    <button type="button">Proceed</button>
</hw:alert-dialog>

initial-focus="auto" honors an eligible [autofocus] element and otherwise focuses Cancel, keeping the safer action as the default. Use dialog to focus the alertdialog surface, first-focusable to select the first eligible control, or none to leave focus where it is. With none, the first Tab or Shift+Tab still enters the trap. Initial focus runs once per opening and is not repeated by morph reconnects or nested-overlay resume.

Turbo Integration

The dialog closes synchronously on turbo:before-cache, preventing ghost dialogs when navigating with Turbo Drive.

Accessibility

  • role="alertdialog" and aria-modal="true" on the overlay
  • title and description receive stable ids and automatically name and describe the alert dialog
  • Focus follows initial-focus; Tab/Shift+Tab cycle through focusable elements inside the dialog
  • Focus returns to the trigger element on close
  • Closes on Escape key
  • Closes on backdrop click (configurable via close-on-click-outside)
  • Body scroll is locked while open (configurable via lock-scroll)

Requirements

  • No external dependencies.
  • Ships with _action_replay.js, _composition.js, _focus_trap.js, _overlay.js, _overlay_stack.js, _presence.js, and _top_layer.js; publishing the alert-dialog controller publishes these helpers too.

Props

Prop Type Default Description
id string|object generated Root id. Pass a model for a stable cross-request id.
title string '' Dialog heading
description string '' Body text below the title
confirm-label string 'Confirm' Action button label
cancel-label string 'Cancel' Cancel button label
confirm-variant string 'default' Action button variant
cancel-variant string 'outline' Cancel button variant
confirm-class string '' Extra CSS classes for the action button
cancel-class string '' Extra CSS classes for the cancel button
motion string 'default' default follows CSS motion; none disables it
initial-focus string 'auto' auto, dialog, first-focusable, or none; invalid values use auto
lock-scroll bool true Locks body scroll when the dialog is open
close-on-click-outside bool true Closes when clicking the backdrop
stimulus Htmlable|null null Optional extra Stimulus binding merged into the root element

Regular data-controller / data-action attributes and the stimulus prop are merged and deduplicated with the internal alert-dialog controller. Component-owned data-alert-dialog-* attributes are protected; configure supported dialog behavior with props instead of overriding those attributes directly.

<hw:alert-dialog.host> accepts the same props, but defaults title to Confirm action so every trigger has an accessible fallback. Its default slot contains the collection and its shared triggers rather than one implicit trigger.

Trigger Props

Prop Type Default Description
as-child bool false Merge trigger metadata into one button or anchor root element
title string|null null Plain-text title override for this action
description string|null null Plain-text description override; '' removes the description
confirm-label string|null null Confirm button label override
cancel-label string|null null Cancel button label override
confirm-variant string|null null Confirm button variant override
cancel-variant string|null null Cancel button variant override

<hw:alert-dialog.trigger> must be rendered inside a host in the same Blade component tree. In as-child mode, its slot must contain exactly one button or anchor root element.

Slots

Slot Description
slot (default) Trigger element whose click is intercepted to open the dialog
content Optional rich content rendered below description and above the buttons

For the inline component, the content slot may use alert-dialog.title and alert-dialog.description when rich markup is required instead of the string props. These subcomponents automatically name and describe the alert dialog. Shared hosts require title and description text props on the host or trigger so dynamic content remains safe and resettable.

Need more control?

For fully custom markup — different DOM structure, no Tailwind, or wiring custom buttons inside the dialog — drop down to the alert-dialog controller.

Styling hooks

The component exposes stable data-slot hooks for preset and application CSS:

  • data-slot="alert-dialog-overlay"
  • data-slot="alert-dialog-backdrop"
  • data-slot="alert-dialog-panel"
  • data-slot="alert-dialog-header"
  • data-slot="alert-dialog-title"
  • data-slot="alert-dialog-description"
  • data-slot="alert-dialog-body"
  • data-slot="alert-dialog-footer"
  • data-slot="alert-dialog-cancel"
  • data-slot="alert-dialog-action"
  • data-slot="alert-dialog"
  • data-slot="alert-dialog-trigger"

alert-dialog-trigger is the interception wrapper around the inline trigger or shared collection. Individual shared markers use data-alert-dialog-trigger instead, so styling the wrapper does not alter every action button.