Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Alert Dialog

    Modal confirmation built on the native dialog element. It needs an explicit cancel or action choice and never closes on a backdrop click.

    Publish this post?

    Your post will be visible to everyone who follows your blog.

    Publish this post?

    Your post will be visible to everyone who follows your blog.

    Publish this post?

    Your post will be visible to everyone who follows your blog.

    Publish this post?

    Your post will be visible to everyone who follows your blog.

    Publish this post?

    Your post will be visible to everyone who follows your blog.
    <x-markas.alert-dialog title="Publish this post?">
        <x-slot:trigger>
            <x-markas.button>Publish post</x-markas.button>
        </x-slot:trigger>
    
        Your post will be visible to everyone who follows your blog.
    </x-markas.alert-dialog>
    import { AlertDialog } from "@/components/markas/AlertDialog";
    import { Button } from "@/components/markas/Button";
    
    export default function AlertDialogExample() {
      return (
        <AlertDialog
          title="Publish this post?"
          trigger={<Button>Publish post</Button>}
        >
          Your post will be visible to everyone who follows your blog.
        </AlertDialog>
      );
    }
    <script setup lang="ts">
    import AlertDialog from "@/components/markas/AlertDialog.vue";
    import Button from "@/components/markas/Button.vue";
    </script>
    
    <template>
      <AlertDialog title="Publish this post?">
        <template #trigger>
          <Button>Publish post</Button>
        </template>
    
        Your post will be visible to everyone who follows your blog.
      </AlertDialog>
    </template>
    <script lang="ts">
      import AlertDialog from "$lib/components/markas/AlertDialog.svelte";
      import Button from "$lib/components/markas/Button.svelte";
    </script>
    
    <AlertDialog title="Publish this post?">
      {#snippet trigger()}
        <Button>Publish post</Button>
      {/snippet}
      Your post will be visible to everyone who follows your blog.
    </AlertDialog>
    ---
    import AlertDialog from "@/components/markas/AlertDialog.astro";
    import Button from "@/components/markas/Button.astro";
    ---
    
    <AlertDialog title="Publish this post?">
      <Button slot="trigger">Publish post</Button>
      Your post will be visible to everyone who follows your blog.
    </AlertDialog>
    import { Component } from "@angular/core";
    import { AlertDialog } from "./markas/alert-dialog.component";
    import { Button } from "./markas/button.component";
    
    @Component({
      selector: "markas-example",
      imports: [AlertDialog, Button],
      template: `
        <markas-alert-dialog title="Publish this post?">
          <button markas-button trigger>Publish post</button>
          Your post will be visible to everyone who follows your blog.
        </markas-alert-dialog>
      `,
    })
    export default class AlertDialogExample {}

    Examples

    Destructive action

    Delete your account?

    All of your projects, invoices, and team members will be removed. You cannot undo this action.

    Delete your account?

    All of your projects, invoices, and team members will be removed. You cannot undo this action.

    Delete your account?

    All of your projects, invoices, and team members will be removed. You cannot undo this action.

    Delete your account?

    All of your projects, invoices, and team members will be removed. You cannot undo this action.

    Delete your account?

    All of your projects, invoices, and team members will be removed. You cannot undo this action.

    Discard changes

    Draft: Q3 product update

    3 unsaved changes

    Discard unsaved changes?

    Your edits since the last save will be lost.

    Draft: Q3 product update

    3 unsaved changes

    Discard unsaved changes?

    Your edits since the last save will be lost.

    Draft: Q3 product update

    3 unsaved changes

    Discard unsaved changes?

    Your edits since the last save will be lost.

    Draft: Q3 product update

    3 unsaved changes

    Discard unsaved changes?

    Your edits since the last save will be lost.

    Draft: Q3 product update

    3 unsaved changes

    Discard unsaved changes?

    Your edits since the last save will be lost.

    With icon trigger

    Sign out of all devices?

    You will need to sign in again on your phone, tablet, and other browsers.

    Sign out of all devices?

    You will need to sign in again on your phone, tablet, and other browsers.

    Sign out of all devices?

    You will need to sign in again on your phone, tablet, and other browsers.

    Sign out of all devices?

    You will need to sign in again on your phone, tablet, and other browsers.

    Sign out of all devices?

    You will need to sign in again on your phone, tablet, and other browsers.

    Installation

    npx markasui@latest add alert-dialog

    Usage

    <x-markas.alert-dialog title="Delete your account?" variant="danger" action-label="Delete account" x-on:markas-action="deleteAccount()">
        <x-slot:trigger>
            <x-markas.button variant="danger">Delete account</x-markas.button>
        </x-slot:trigger>
    
        All of your projects and invoices will be removed. You cannot undo this action.
    </x-markas.alert-dialog>

    Needs Alpine.js. Livewire 3 and 4 already ship it.

    Livewire

    <x-markas.alert-dialog title="Delete project?" wire:model="confirming" wire:markas-action="delete">
        This cannot be undone.
    </x-markas.alert-dialog>

    wire:model binds the open state. wire:markas-action runs a Livewire action when the action button is pressed; wire:markas-cancel for any other close.

    import { AlertDialog } from "@/components/markas/AlertDialog";
    import { Button } from "@/components/markas/Button";
    
    <AlertDialog
      title="Delete your account?"
      variant="danger"
      actionLabel="Delete account"
      trigger={<Button variant="danger">Delete account</Button>}
      onAction={() => deleteAccount()}
      onCancel={() => console.log("kept")}
    >
      All of your projects and invoices will be removed. You cannot undo this
      action.
    </AlertDialog>;
    <template>
      <AlertDialog
        title="Delete your account?"
        variant="danger"
        action-label="Delete account"
        @action="deleteAccount"
      >
        <template #trigger>
          <Button variant="danger">Delete account</Button>
        </template>
    
        All of your projects and invoices will be removed. You cannot undo this
        action.
      </AlertDialog>
    </template>
    <script lang="ts">
      import AlertDialog from "$lib/components/markas/AlertDialog.svelte";
      import Button from "$lib/components/markas/Button.svelte";
    </script>
    
    <AlertDialog
      title="Delete your account?"
      variant="danger"
      actionLabel="Delete account"
      onaction={deleteAccount}
    >
      {#snippet trigger()}
        <Button variant="danger">Delete account</Button>
      {/snippet}
    
      All of your projects and invoices will be removed. You cannot undo this
      action.
    </AlertDialog>
    ---
    import AlertDialog from "@/components/markas/AlertDialog.astro";
    import Button from "@/components/markas/Button.astro";
    ---
    
    <AlertDialog
      title="Delete your account?"
      variant="danger"
      actionLabel="Delete account"
    >
      <Button slot="trigger" variant="danger">Delete account</Button>
    
      All of your projects and invoices will be removed. You cannot undo this
      action.
    </AlertDialog>

    A small bundled script opens every alert dialog on the page and dispatches a bubbling markas-action or markas-cancel event from the dialog when it closes.

    import { Component } from "@angular/core";
    import { AlertDialog } from "./markas/alert-dialog.component";
    import { Button } from "./markas/button.component";
    
    @Component({
      selector: "app-delete-account",
      imports: [AlertDialog, Button],
      template: `
        <markas-alert-dialog
          title="Delete your account?"
          variant="danger"
          actionLabel="Delete account"
          (action)="deleteAccount()"
        >
          <button markas-button trigger variant="danger">Delete account</button>
    
          All of your projects and invoices will be removed. You cannot undo this
          action.
        </markas-alert-dialog>
      `,
    })
    export class DeleteAccount {
      deleteAccount() {}
    }
    • Unlike Modal, clicking the backdrop does not close the dialog. The user must choose cancel or the action; Escape counts as cancel.
    • Focus starts on the cancel button.
    • dialog.returnValue is action or cancel in the close event.
    • The buttons read “Cancel” and “Continue” by default. Translate them with cancelLabel and actionLabel (Blade: cancel-label, action-label).

    Props

    NameTypeDefaultDescription
    titlestring-Dialog title, used for aria-labelledby.
    children / slotslot / ReactNode / snippet-Short explanation of the consequence, used for aria-describedby.
    triggerslot / ReactNode / snippet-Element that opens the dialog when clicked. Angular: an element with the trigger attribute.
    variantdefault | dangerdefaultColor of the action button. Use danger for destructive or irreversible actions.
    cancelLabelstringCancelText of the cancel button.
    actionLabelstringContinueText of the action button.
    onAction / @action / markas-action() => void-Called when the action button is chosen. Svelte: onaction. Blade and Astro dispatch a markas-action event.
    onCancel / @cancel / markas-cancel() => void-Called when the dialog closes through the cancel button or Escape. Svelte: oncancel. Blade and Astro dispatch a markas-cancel event.
    idstringautoDialog id. Use it to open the dialog from elsewhere with showModal().
    actionoutput<void>-Angular only. Emits when the action button is chosen.
    cancelledoutput<void>-Angular only. Emits when the dialog closes through the cancel button or Escape.

    Accessibility

    • Uses dialog with role=alertdialog and showModal(), so focus stays inside and the page behind is inert.
    • The title is linked with aria-labelledby and the explanation with aria-describedby.
    • Focus starts on the cancel button, so pressing Enter by accident never runs the action.
    • Escape closes the dialog and counts as cancel. Clicking the backdrop does nothing.

    Related components

    More overlay components with the same classes in every framework.