# Confirm dialog

Asks the user to confirm an action before it happens, with one line of code: `await confirm.ask(…)` resolves to `true` or `false`. Use it for consequential or irreversible actions, not for routine ones.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-confirm-dialog`
- Source: [libs/ui/src/lib/organisms/confirm-dialog/confirm-dialog.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/organisms/confirm-dialog/confirm-dialog.ts)

```typescript title="Import"
import { ConfirmDialog, Confirm } from '@vigdx/ui/confirm-dialog';
```

## Basic

Place `<vig-confirm-dialog />` once in the app shell (see the `app.ts` tab; this site already does). Then inject the `Confirm` service anywhere and `await` its `ask()` with a `message` and an optional `heading`.

```typescript title="confirm-dialog-basic.ts"
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Confirm } from '@vigdx/ui/confirm-dialog';

@Component({
  selector: 'app-confirm-dialog-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Publish post" (click)="publish()" />
      <span class="text-sm text-muted-foreground" aria-live="polite">{{ status() }}</span>
    </div>
  `,
})
export class ConfirmDialogBasicDemo {
  private readonly confirm = inject(Confirm);
  protected readonly status = signal('Draft');

  protected async publish(): Promise<void> {
    const accepted = await this.confirm.ask({
      heading: 'Publish post?',
      message: 'Subscribers will be notified by email right away.',
    });
    this.status.set(accepted ? 'Published' : 'Still a draft');
  }
}
```

```typescript title="app.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { ConfirmDialog } from '@vigdx/ui/confirm-dialog';

/** The app shell: the host for every `Confirm.ask()` call, placed once. */
@Component({
  selector: 'app-root',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RouterOutlet, ConfirmDialog],
  template: `
    <router-outlet />
    <vig-confirm-dialog />
  `,
})
export class App {}
```

## Destructive action

`variant: 'error'` styles the accept button as destructive. `acceptLabel` and `rejectLabel` name the outcome of each button, which is clearer than a generic “Confirm”.

```typescript title="confirm-dialog-destructive.ts"
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Confirm } from '@vigdx/ui/confirm-dialog';

@Component({
  selector: 'app-confirm-dialog-destructive',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <ul class="w-full max-w-md divide-y divide-border rounded-card border border-border">
      @for (file of files(); track file) {
        <li class="flex items-center justify-between gap-3 py-1.5 ps-4 pe-2 text-sm">
          {{ file }}
          <vig-button
            label="Delete"
            variant="error"
            appearance="text"
            size="sm"
            (click)="remove(file)"
          />
        </li>
      } @empty {
        <li class="px-4 py-3 text-sm text-muted-foreground">All files deleted.</li>
      }
    </ul>
  `,
})
export class ConfirmDialogDestructiveDemo {
  private readonly confirm = inject(Confirm);
  protected readonly files = signal(['budget-2026.xlsx', 'logo-final.svg', 'contract.pdf']);

  protected async remove(file: string): Promise<void> {
    const accepted = await this.confirm.ask({
      heading: 'Delete file?',
      message: `“${file}” will be deleted permanently. This cannot be undone.`,
      variant: 'error',
      acceptLabel: 'Delete',
      rejectLabel: 'Keep file',
    });
    if (accepted) this.files.update((files) => files.filter((f) => f !== file));
  }
}
```

## Default labels

Without `heading` or labels, the host's localized defaults apply (“Are you sure?”, “Confirm”, “Cancel”); set them app-wide through the `vig-confirm-dialog` inputs. Escape, the X button and a backdrop click resolve to `false`.

```typescript title="confirm-dialog-defaults.ts"
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Confirm } from '@vigdx/ui/confirm-dialog';
import { Toast } from '@vigdx/ui/toast';

@Component({
  selector: 'app-confirm-dialog-defaults',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `<vig-button label="Archive project" variant="secondary" (click)="archive()" />`,
})
export class ConfirmDialogDefaultsDemo {
  private readonly confirm = inject(Confirm);
  private readonly toast = inject(Toast);

  // No header or labels: the localized defaults of the vig-confirm-dialog host apply.
  protected async archive(): Promise<void> {
    if (await this.confirm.ask({ message: 'Archive “Website relaunch”?' })) {
      this.toast.success('The project moved to the archive.', 'Archived');
    }
  }
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="confirm-dialog"][data-part="message"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="confirm-dialog"]`

| Part | Element | data-state |
| --- | --- | --- |
| `message` | The message paragraph (inside the dialog body). |   |
| `reject` | The reject `vig-button`. |   |
| `accept` | The accept `vig-button`. |   |

## Accessibility

- Renders a `vig-dialog`, so it is a native modal `<dialog>`: the page is inert, focus is trapped and Escape cancels.
- It opens with focus on the reject button, the safe answer, so a reflexive Enter never confirms a destructive action.
- The dialog is named by its header; the message is plain text in the body, so write it as a complete sentence that makes sense on its own.
- Focus returns to the button that asked once the dialog closes, whatever the answer.
- All built-in strings (`heading`, `acceptLabel`, `rejectLabel`, `closeAriaLabel`) are inputs on the host and options per call, for i18n.

## API

### ConfirmDialog

Selector: `vig-confirm-dialog` · Kind: component

Host for the Confirm service — place once in the app shell. Renders the pending request in a vig-dialog; label defaults are inputs for i18n and can be overridden per call via ConfirmOptions. The dialog opens with focus on the reject (safe) action.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `acceptLabel` | `string \| undefined` | `undefined` | — |
| `closeAriaLabel` | `string \| undefined` | `undefined` | — |
| `header` | `string \| undefined` | `undefined` | **Deprecated**: Use `heading`; still honored when `heading` is unset. |
| `heading` | `string \| undefined` | `undefined` | Default title; `ConfirmOptions.heading` overrides it per call. |
| `rejectLabel` | `string \| undefined` | `undefined` | — |
| `width` | `string` | `'var(--container-dialog-sm)'` | CSS width; defaults to the `--container-dialog-sm` token. |

### Confirm

Kind: injectable

Imperative confirmation flow (PrimeNG ConfirmationService territory): place `<vig-confirm-dialog />` once in the app shell, then

**Example :**

```ts
if (await this.confirm.ask({ message: 'Delete this record?', variant: 'error' })) { … }
```

Resolves false on reject and on any dismissal (Escape, X, backdrop).

#### Methods

| Signature | Description |
| --- | --- |
| `ask(options: ConfirmOptions): Promise<boolean>` | — |
| `settle(accepted: boolean): void` | Settles and closes the pending request; no-op when nothing is open. |
