# Alert Dialog
> Confirmation dialog requiring explicit user action
Source: https://shellui.dev/docs/v0.4/components/advanced/alert-dialog
The AlertDialog component presents a modal dialog that interrupts the user to confirm a destructive or important action. Unlike a standard Dialog, it requires an explicit confirm or cancel — clicking outside does not dismiss it.
## Installation
```bash
shellui add alert-dialog
```
## Basic Usage
```razor
@code {
private bool isOpen = false;
private void HandleConfirm()
{
// Perform destructive action
isOpen = false;
}
private void HandleCancel()
{
isOpen = false;
}
}
```
## Custom Button Text
Customize the confirm and cancel button labels:
```razor
@code {
private bool isOpen = false;
private void HandleDiscard() => isOpen = false;
}
```
## Destructive Confirm Variant
Style the confirm button as destructive:
```razor
@code {
private bool isOpen = false;
private void HandleDelete() => isOpen = false;
}
```
## Non-Destructive Confirmation
Use for important but non-destructive actions:
```razor
@code {
private bool isOpen = false;
private void HandlePublish() => isOpen = false;
}
```
## Async Confirm
Handle async operations on confirm:
```razor
@code {
private bool isOpen = false;
private string memberName = "John Doe";
private async Task HandleRemoveAsync()
{
await Task.Delay(500); // e.g. await TeamService.RemoveMemberAsync(memberName);
isOpen = false;
}
}
```
## Composed from parts
New in v0.4: leave out `Title` and `Description` and build the dialog from parts. `AlertDialogTrigger` opens it, so no open-state field is needed. `AlertDialogAction` runs `OnConfirm` and closes; `AlertDialogCancel` and the Escape key run `OnCancel` and close.
```razor
Delete your account?
This removes your account and all of its data. It can't be undone.
Keep accountDelete
Result: @result
@code {
private string result = "none";
}
```
Without a trigger, control it with `@bind-IsOpen`. `AlertDialogAction` and `AlertDialogCancel` also have their own `OnClick`, which runs before the dialog's callback.
```razor
Discard this draft?Your unsaved changes will be lost.Keep editingDiscard
Drafts discarded: @discarded
@code {
private bool open;
private int discarded;
}
```
## API Reference
| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `IsOpen` | `bool` | `false` | Controls dialog visibility |
| `IsOpenChanged` | `EventCallback` | — | Callback when open state changes |
| `Title` | `string` | — | Dialog title (required) |
| `Description` | `string` | — | Dialog description (required) |
| `ConfirmText` | `string` | `"Continue"` | Confirm button label |
| `CancelText` | `string?` | `"Cancel"` | Cancel button label |
| `ConfirmVariant` | `ButtonVariant` | `ButtonVariant.Default` | Confirm button style variant |
| `OnConfirm` | `EventCallback` | — | Callback when user confirms |
| `OnCancel` | `EventCallback` | — | Callback when user cancels |
### Parts
| Component | Parameters | Purpose |
|-----------|------------|---------|
| `AlertDialogTrigger` | - | Opens the dialog when clicked |
| `AlertDialogContent` | `Class` | The modal panel and backdrop |
| `AlertDialogHeader` | `Class` | Wraps the title and description |
| `AlertDialogTitle` | `Class` | The heading |
| `AlertDialogDescription` | `Class` | The supporting text |
| `AlertDialogFooter` | `Class` | Wraps the buttons |
| `AlertDialogAction` | `Variant`, `OnClick`, `Class` | Confirms and closes |
| `AlertDialogCancel` | `OnClick`, `Class` | Cancels and closes |
Setting `Title` or `Description` on `AlertDialog` selects the built-in layout; leaving both empty selects the composed one.
## Accessibility
The AlertDialog component includes:
- `role="alertdialog"` with `aria-modal="true"`
- Focus trap — Tab cycles within the dialog
- Escape key triggers cancel (not dismiss)
- Focus moves to cancel button on open (safer default)
- Focus returns to trigger on close
- Screen reader announces title and description