alert dialog

A modal dialog that interrupts the user with important content and expects a response.

PreviousNext
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardAlertDialogService } from '@/shared/components/alert-dialog/alert-dialog.service';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

@Component({
  selector: 'z-demo-alert-dialog-preview',
  imports: [ZardAlertDialogImports, ZardButtonComponent, ZardTabsImports],
  template: `
    <z-tab-group>
      <z-tab label="Template">
        <button type="button" z-button zType="outline" (click)="visible.set(true)">Show Dialog</button>

        <z-alert-dialog [(zVisible)]="visible">
          <z-alert-dialog-header>
            <z-alert-dialog-title>Are you absolutely sure?</z-alert-dialog-title>
            <z-alert-dialog-description>
              This action cannot be undone. This will permanently delete your account from our servers.
            </z-alert-dialog-description>
          </z-alert-dialog-header>
          <z-alert-dialog-footer>
            <button type="button" z-button zType="outline" z-alert-dialog-close>Cancel</button>
            <button type="button" z-button (click)="visible.set(false)">Continue</button>
          </z-alert-dialog-footer>
        </z-alert-dialog>
      </z-tab>

      <z-tab label="Service">
        <button type="button" z-button zType="outline" (click)="open()">Show Dialog</button>
      </z-tab>
    </z-tab-group>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoAlertDialogPreviewComponent {
  private readonly alertDialogService = inject(ZardAlertDialogService);

  readonly visible = signal(false);

  open() {
    this.alertDialogService.create({
      zTitle: 'Are you absolutely sure?',
      zDescription: 'This action cannot be undone. This will permanently delete your account from our servers.',
      zOkText: 'Continue',
      zCancelText: 'Cancel',
    });
  }
}

Installation

Copy
npx zard-cli@latest add alert-dialog

Usage

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
Copy
<button type="button" z-button zType="outline" (click)="visible.set(true)">Show Dialog</button>

<z-alert-dialog [(zVisible)]="visible">
  <z-alert-dialog-header>
    <z-alert-dialog-title>Are you absolutely sure?</z-alert-dialog-title>
    <z-alert-dialog-description>
      This action cannot be undone. This will permanently delete your account and remove your data from our servers.
    </z-alert-dialog-description>
  </z-alert-dialog-header>
  <z-alert-dialog-footer>
    <button type="button" z-button zType="outline" z-alert-dialog-close>Cancel</button>
    <button type="button" z-button (click)="visible.set(false)">Continue</button>
  </z-alert-dialog-footer>
</z-alert-dialog>
Copy

Composition

Use the following composition to build a alert-dialog:

z-alert-dialog
├── z-alert-dialog-header
│   ├── z-alert-dialog-media
│   ├── z-alert-dialog-title
│   └── z-alert-dialog-description
└── z-alert-dialog-footer
    └── [z-alert-dialog-close]
Copy

Examples

Basic

The minimal alert dialog: a title, a description and the cancel/continue buttons. In the template the buttons are yours; [z-alert-dialog-close] closes without a handler.
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardAlertDialogService } from '@/shared/components/alert-dialog/alert-dialog.service';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

@Component({
  selector: 'z-demo-alert-dialog-basic',
  imports: [ZardAlertDialogImports, ZardButtonComponent, ZardTabsImports],
  template: `
    <z-tab-group>
      <z-tab label="Template">
        <button type="button" z-button zType="outline" (click)="visible.set(true)">Show Dialog</button>

        <z-alert-dialog [(zVisible)]="visible">
          <z-alert-dialog-header>
            <z-alert-dialog-title>Are you absolutely sure?</z-alert-dialog-title>
            <z-alert-dialog-description>
              This action cannot be undone. This will permanently delete your account and remove your data from our
              servers.
            </z-alert-dialog-description>
          </z-alert-dialog-header>
          <z-alert-dialog-footer>
            <button type="button" z-button zType="outline" z-alert-dialog-close>Cancel</button>
            <button type="button" z-button (click)="visible.set(false)">Continue</button>
          </z-alert-dialog-footer>
        </z-alert-dialog>
      </z-tab>

      <z-tab label="Service">
        <button type="button" z-button zType="outline" (click)="open()">Show Dialog</button>
      </z-tab>
    </z-tab-group>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoAlertDialogBasicComponent {
  private readonly alertDialogService = inject(ZardAlertDialogService);

  readonly visible = signal(false);

  open() {
    this.alertDialogService.create({
      zTitle: 'Are you absolutely sure?',
      zDescription:
        'This action cannot be undone. This will permanently delete your account and remove your data from our servers.',
      zOkText: 'Continue',
      zCancelText: 'Cancel',
    });
  }
}

Small

Use zSize="sm", as an input or an option, to make the alert dialog smaller.
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardAlertDialogService } from '@/shared/components/alert-dialog/alert-dialog.service';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

@Component({
  selector: 'z-demo-alert-dialog-small',
  imports: [ZardAlertDialogImports, ZardButtonComponent, ZardTabsImports],
  template: `
    <z-tab-group>
      <z-tab label="Template">
        <button type="button" z-button zType="outline" (click)="visible.set(true)">Show Dialog</button>

        <z-alert-dialog [(zVisible)]="visible" zSize="sm">
          <z-alert-dialog-header>
            <z-alert-dialog-title>Allow accessory to connect?</z-alert-dialog-title>
            <z-alert-dialog-description>
              Do you want to allow the USB accessory to connect to this device?
            </z-alert-dialog-description>
          </z-alert-dialog-header>
          <z-alert-dialog-footer>
            <button type="button" z-button zType="outline" z-alert-dialog-close>Don't allow</button>
            <button type="button" z-button (click)="visible.set(false)">Allow</button>
          </z-alert-dialog-footer>
        </z-alert-dialog>
      </z-tab>

      <z-tab label="Service">
        <button type="button" z-button zType="outline" (click)="open()">Show Dialog</button>
      </z-tab>
    </z-tab-group>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoAlertDialogSmallComponent {
  private readonly alertDialogService = inject(ZardAlertDialogService);

  readonly visible = signal(false);

  open() {
    this.alertDialogService.create({
      zSize: 'sm',
      zTitle: 'Allow accessory to connect?',
      zDescription: 'Do you want to allow the USB accessory to connect to this device?',
      zOkText: 'Allow',
      zCancelText: "Don't allow",
    });
  }
}

Media

Add a media element such as an icon above the title: z-alert-dialog-media in the template, or a <ng-template> passed via zMedia to the service.
import { ChangeDetectionStrategy, Component, inject, signal, type TemplateRef } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideCircleFadingPlus } from '@ng-icons/lucide';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardAlertDialogService } from '@/shared/components/alert-dialog/alert-dialog.service';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

@Component({
  selector: 'z-demo-alert-dialog-media',
  imports: [ZardAlertDialogImports, ZardButtonComponent, ZardTabsImports, NgIcon],
  template: `
    <z-tab-group>
      <z-tab label="Template">
        <button type="button" z-button zType="outline" (click)="visible.set(true)">Share Project</button>

        <z-alert-dialog [(zVisible)]="visible">
          <z-alert-dialog-header>
            <z-alert-dialog-media>
              <ng-icon name="lucideCircleFadingPlus" />
            </z-alert-dialog-media>
            <z-alert-dialog-title>Share this project?</z-alert-dialog-title>
            <z-alert-dialog-description>
              Anyone with the link will be able to view and edit this project.
            </z-alert-dialog-description>
          </z-alert-dialog-header>
          <z-alert-dialog-footer>
            <button type="button" z-button zType="outline" z-alert-dialog-close>Cancel</button>
            <button type="button" z-button (click)="visible.set(false)">Share</button>
          </z-alert-dialog-footer>
        </z-alert-dialog>
      </z-tab>

      <z-tab label="Service">
        <ng-template #mediaIcon>
          <ng-icon name="lucideCircleFadingPlus" />
        </ng-template>
        <button type="button" z-button zType="outline" (click)="open(mediaIcon)">Share Project</button>
      </z-tab>
    </z-tab-group>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideCircleFadingPlus })],
})
export class ZardDemoAlertDialogMediaComponent {
  private readonly alertDialogService = inject(ZardAlertDialogService);

  readonly visible = signal(false);

  open(media: TemplateRef<void>) {
    this.alertDialogService.create({
      zMedia: media,
      zTitle: 'Share this project?',
      zDescription: 'Anyone with the link will be able to view and edit this project.',
      zOkText: 'Share',
      zCancelText: 'Cancel',
    });
  }
}

Small With Media

Combine zSize="sm" with the media slot to add a media element to the smaller alert dialog.
import { ChangeDetectionStrategy, Component, inject, signal, type TemplateRef } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideBluetooth } from '@ng-icons/lucide';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardAlertDialogService } from '@/shared/components/alert-dialog/alert-dialog.service';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

@Component({
  selector: 'z-demo-alert-dialog-small-with-media',
  imports: [ZardAlertDialogImports, ZardButtonComponent, ZardTabsImports, NgIcon],
  template: `
    <z-tab-group>
      <z-tab label="Template">
        <button type="button" z-button zType="outline" (click)="visible.set(true)">Show Dialog</button>

        <z-alert-dialog [(zVisible)]="visible" zSize="sm">
          <z-alert-dialog-header>
            <z-alert-dialog-media>
              <ng-icon name="lucideBluetooth" />
            </z-alert-dialog-media>
            <z-alert-dialog-title>Allow accessory to connect?</z-alert-dialog-title>
            <z-alert-dialog-description>
              Do you want to allow the USB accessory to connect to this device?
            </z-alert-dialog-description>
          </z-alert-dialog-header>
          <z-alert-dialog-footer>
            <button type="button" z-button zType="outline" z-alert-dialog-close>Don't allow</button>
            <button type="button" z-button (click)="visible.set(false)">Allow</button>
          </z-alert-dialog-footer>
        </z-alert-dialog>
      </z-tab>

      <z-tab label="Service">
        <ng-template #mediaIcon>
          <ng-icon name="lucideBluetooth" />
        </ng-template>
        <button type="button" z-button zType="outline" (click)="open(mediaIcon)">Show Dialog</button>
      </z-tab>
    </z-tab-group>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideBluetooth })],
})
export class ZardDemoAlertDialogSmallWithMediaComponent {
  private readonly alertDialogService = inject(ZardAlertDialogService);

  readonly visible = signal(false);

  open(media: TemplateRef<void>) {
    this.alertDialogService.create({
      zSize: 'sm',
      zMedia: media,
      zTitle: 'Allow accessory to connect?',
      zDescription: 'Do you want to allow the USB accessory to connect to this device?',
      zOkText: 'Allow',
      zCancelText: "Don't allow",
    });
  }
}

Destructive

A destructive action: tint the media slot (class on z-alert-dialog-media, or zMediaClass) and make the confirm button destructive (zType="destructive", or zOkDestructive: true).
import { ChangeDetectionStrategy, Component, inject, signal, type TemplateRef } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideTrash2 } from '@ng-icons/lucide';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardAlertDialogService } from '@/shared/components/alert-dialog/alert-dialog.service';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

@Component({
  selector: 'z-demo-alert-dialog-destructive',
  imports: [ZardAlertDialogImports, ZardButtonComponent, ZardTabsImports, NgIcon],
  template: `
    <z-tab-group>
      <z-tab label="Template">
        <button type="button" z-button zType="destructive" (click)="visible.set(true)">Delete Chat</button>

        <z-alert-dialog [(zVisible)]="visible" zSize="sm">
          <z-alert-dialog-header>
            <z-alert-dialog-media class="bg-destructive/10 text-destructive dark:bg-destructive/20">
              <ng-icon name="lucideTrash2" />
            </z-alert-dialog-media>
            <z-alert-dialog-title>Delete chat?</z-alert-dialog-title>
            <z-alert-dialog-description>
              This will permanently delete this chat conversation. View
              <a href="#">Settings</a>
              delete any memories saved during this chat.
            </z-alert-dialog-description>
          </z-alert-dialog-header>
          <z-alert-dialog-footer>
            <button type="button" z-button zType="outline" z-alert-dialog-close>Cancel</button>
            <button type="button" z-button zType="destructive" (click)="visible.set(false)">Delete</button>
          </z-alert-dialog-footer>
        </z-alert-dialog>
      </z-tab>

      <z-tab label="Service">
        <ng-template #mediaIcon>
          <ng-icon name="lucideTrash2" />
        </ng-template>
        <button type="button" z-button zType="destructive" (click)="open(mediaIcon)">Delete Chat</button>
      </z-tab>
    </z-tab-group>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideTrash2 })],
})
export class ZardDemoAlertDialogDestructiveComponent {
  private readonly alertDialogService = inject(ZardAlertDialogService);

  readonly visible = signal(false);

  open(media: TemplateRef<void>) {
    this.alertDialogService.create({
      zSize: 'sm',
      zMedia: media,
      zMediaClass: 'bg-destructive/10 text-destructive dark:bg-destructive/20',
      zTitle: 'Delete chat?',
      zDescription:
        'This will permanently delete this chat conversation. View <a href="#">Settings</a> delete any memories saved during this chat.',
      zOkText: 'Delete',
      zCancelText: 'Cancel',
      zOkDestructive: true,
    });
  }
}

RTL

To enable RTL support in zard/ui, see the RTL configuration guide.
// You will notice this example is rendered inside a wrapper that sets the dir and data-lang attributes, and
// reads its text from a translations helper. This is because this site is not RTL by default.
// In your application you won't need either: set dir="rtl" on <html> (or use the CDK Dir directive on a subtree)
// and write the text of your own language. See the RTL guide: https://zardui.com/docs/rtl

import { ChangeDetectionStrategy, Component, signal } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideBluetooth } from '@ng-icons/lucide';

import { ZardAlertDialogImports } from '@/shared/components/alert-dialog/alert-dialog.imports';
import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { injectRtlTranslation, type ZardRtlTranslations } from '@/shared/demo-utils/rtl-translation';

interface AlertDialogRtlStrings {
  showDialog: string;
  showDialogSm: string;
  title: string;
  description: string;
  cancel: string;
  continue: string;
  smallTitle: string;
  smallDescription: string;
  dontAllow: string;
  allow: string;
}

const translations: ZardRtlTranslations<AlertDialogRtlStrings> = {
  en: {
    dir: 'ltr',
    values: {
      showDialog: 'Show Dialog',
      showDialogSm: 'Show Dialog (sm)',
      title: 'Are you absolutely sure?',
      description: 'This action cannot be undone. This will permanently delete your account from our servers.',
      cancel: 'Cancel',
      continue: 'Continue',
      smallTitle: 'Allow accessory to connect?',
      smallDescription: 'Do you want to allow the USB accessory to connect to this device?',
      dontAllow: "Don't allow",
      allow: 'Allow',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      showDialog: 'إظهار الحوار',
      showDialogSm: 'إظهار الحوار (صغير)',
      title: 'هل أنت متأكد تمامًا؟',
      description: 'لا يمكن التراجع عن هذا الإجراء. سيؤدي هذا إلى حذف حسابك نهائيًا من خوادمنا.',
      cancel: 'إلغاء',
      continue: 'متابعة',
      smallTitle: 'السماح للملحق بالاتصال؟',
      smallDescription: 'هل تريد السماح لملحق USB بالاتصال بهذا الجهاز؟',
      dontAllow: 'عدم السماح',
      allow: 'السماح',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      showDialog: 'הצג דיאלוג',
      showDialogSm: 'הצג דיאלוג (קטן)',
      title: 'האם אתה בטוח לחלוטין?',
      description: 'פעולה זו לא ניתנת לביטול. זה ימחק לצמיתות את החשבון שלך מהשרתים שלנו.',
      cancel: 'ביטול',
      continue: 'המשך',
      smallTitle: 'לאפשר להתקן להתחבר?',
      smallDescription: 'האם אתה רוצה לאפשר להתקן USB להתחבר למכשיר זה?',
      dontAllow: 'אל תאפשר',
      allow: 'אפשר',
    },
  },
};

@Component({
  selector: 'z-demo-alert-dialog-rtl',
  imports: [ZardAlertDialogImports, ZardButtonComponent, NgIcon],
  template: `
    @let t = i18n.t();
    <div class="flex gap-4">
      <button type="button" z-button zType="outline" (click)="visible.set(true)">{{ t.showDialog }}</button>
      <button type="button" z-button zType="outline" (click)="smallVisible.set(true)">{{ t.showDialogSm }}</button>
    </div>

    <z-alert-dialog [(zVisible)]="visible">
      <z-alert-dialog-header>
        <z-alert-dialog-title>{{ t.title }}</z-alert-dialog-title>
        <z-alert-dialog-description>{{ t.description }}</z-alert-dialog-description>
      </z-alert-dialog-header>
      <z-alert-dialog-footer>
        <button type="button" z-button zType="outline" z-alert-dialog-close>{{ t.cancel }}</button>
        <button type="button" z-button (click)="visible.set(false)">{{ t.continue }}</button>
      </z-alert-dialog-footer>
    </z-alert-dialog>

    <z-alert-dialog [(zVisible)]="smallVisible" zSize="sm">
      <z-alert-dialog-header>
        <z-alert-dialog-media>
          <ng-icon name="lucideBluetooth" />
        </z-alert-dialog-media>
        <z-alert-dialog-title>{{ t.smallTitle }}</z-alert-dialog-title>
        <z-alert-dialog-description>{{ t.smallDescription }}</z-alert-dialog-description>
      </z-alert-dialog-header>
      <z-alert-dialog-footer>
        <button type="button" z-button zType="outline" z-alert-dialog-close>{{ t.dontAllow }}</button>
        <button type="button" z-button (click)="smallVisible.set(false)">{{ t.allow }}</button>
      </z-alert-dialog-footer>
    </z-alert-dialog>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideBluetooth })],
})
export class ZardDemoAlertDialogRtlComponent {
  protected readonly i18n = injectRtlTranslation(translations);

  protected readonly visible = signal(false);
  protected readonly smallVisible = signal(false);
}

API Reference

z-alert-dialog

Declarative alert dialog root that holds the open state.

Prop
[zVisible]

Two-way bindable open state

Type
boolean
Default
false
[zSize]

sm keeps a compact, centered layout

Type
'default' | 'sm'
Default
'default'
[zWidth]

Custom width

Type
string
Default
—
[zMaskClosable]

Clicking outside closes it

Type
boolean
Default
false
[zDuration]

Transition duration in ms

Type
number
Default
100
[class]

Custom CSS classes

Type
ClassValue
Default
—
(zAfterOpen)

Emits once the dialog is attached

Type
EventEmitter<void>
Default
—
(zAfterClose)

Emits after the exit transition

Type
EventEmitter<void>
Default
—

z-alert-dialog-header

Top slot for the media, title and description.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
—

z-alert-dialog-media

Media slot above the title, usually an icon.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
—

z-alert-dialog-title

Dialog title, wired to aria-labelledby.

Prop
[zTitle]

Title text or template

Type
string | TemplateRef<void>
Default
—
[class]

Custom CSS classes

Type
ClassValue
Default
—

z-alert-dialog-description

Supporting text, wired to aria-describedby.

Prop
[zDescription]

Description text or template

Type
string | TemplateRef<void>
Default
—
[class]

Custom CSS classes

Type
ClassValue
Default
—

z-alert-dialog-footer

Slot for the action buttons.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
—

[z-alert-dialog-close]

Closes the enclosing alert dialog.

ZardAlertDialogService

Opens alert dialogs from code and returns a ZardAlertDialogRef.

Prop
[zTitle]

Title text or template

Type
string | TemplateRef<void>
Default
—
[zDescription]

Description text

Type
string
Default
—
[zMedia]

Media template above the title

Type
TemplateRef<void>
Default
—
[zMediaClass]

Custom CSS classes for the media slot

Type
ClassValue
Default
—
[zData]

Data passed to zOnOk and zOnCancel

Type
T
Default
—
[zOkText]

OK button text, null hides it

Type
string | null
Default
'Continue'
[zCancelText]

Cancel button text, null hides it

Type
string | null
Default
'Cancel'
[zOkDestructive]

Destructive OK button

Type
boolean
Default
false
[zOkDisabled]

Disables the OK button

Type
boolean
Default
false
[zMaskClosable]

Clicking outside closes it

Type
boolean
Default
false
[zClosable]

Dialog can be closed

Type
boolean
Default
true
[zSize]

sm keeps a compact width

Type
'default' | 'sm'
Default
'default'
[zWidth]

Custom width

Type
string
Default
—
[zCustomClasses]

Custom CSS classes

Type
ClassValue
Default
—
[zDuration]

Close animation duration in ms

Type
number
Default
100
[zOnOk]

OK click handler; return false to stay open

Type
EventEmitter<T> | OnClickCallback<T>
Default
—
[zOnCancel]

Cancel click handler; return false to stay open

Type
EventEmitter<T> | OnClickCallback<T>
Default
—
[zViewContainerRef]

View container for the content

Type
ViewContainerRef
Default
—
github iconwhatsapp icondiscord iconX icon

Made with ❤ in Brazil. Open source and available on GitHub .