Alert Dialog
Confirmation dialog requiring explicit user action
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
shellui add alert-dialog
Basic Usage
<Button Variant="ButtonVariant.Destructive" OnClick="() => isOpen = true">Delete Account</Button>
<AlertDialog IsOpen="@isOpen"
IsOpenChanged="@((v) => isOpen = v)"
Title="Are you absolutely sure?"
Description="This action cannot be undone. This will permanently delete your account and remove all data."
OnConfirm="HandleConfirm"
OnCancel="HandleCancel" />
@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:
<Button Variant="ButtonVariant.Outline" OnClick="() => isOpen = true">Discard changes</Button>
<AlertDialog IsOpen="@isOpen"
IsOpenChanged="@((v) => isOpen = v)"
Title="Discard changes?"
Description="You have unsaved changes that will be lost."
ConfirmText="Discard"
CancelText="Keep Editing"
OnConfirm="HandleDiscard"
OnCancel="() => isOpen = false" />
@code {
private bool isOpen = false;
private void HandleDiscard() => isOpen = false;
}
Destructive Confirm Variant
Style the confirm button as destructive:
<Button Variant="ButtonVariant.Destructive" OnClick="() => isOpen = true">Delete project</Button>
<AlertDialog IsOpen="@isOpen"
IsOpenChanged="@((v) => isOpen = v)"
Title="Delete project?"
Description="This will permanently delete the project and all associated files."
ConfirmText="Delete Project"
ConfirmVariant="ButtonVariant.Destructive"
OnConfirm="HandleDelete"
OnCancel="() => isOpen = false" />
@code {
private bool isOpen = false;
private void HandleDelete() => isOpen = false;
}
Non-Destructive Confirmation
Use for important but non-destructive actions:
<Button Variant="ButtonVariant.Default" OnClick="() => isOpen = true">Publish article</Button>
<AlertDialog IsOpen="@isOpen"
IsOpenChanged="@((v) => isOpen = v)"
Title="Publish article?"
Description="This will make the article visible to all users."
ConfirmText="Publish"
ConfirmVariant="ButtonVariant.Default"
CancelText="Not Yet"
OnConfirm="HandlePublish"
OnCancel="() => isOpen = false" />
@code {
private bool isOpen = false;
private void HandlePublish() => isOpen = false;
}
Async Confirm
Handle async operations on confirm:
<Button Variant="ButtonVariant.Destructive" OnClick="() => isOpen = true">Remove member</Button>
<AlertDialog IsOpen="@isOpen"
IsOpenChanged="@((v) => isOpen = v)"
Title="Remove member?"
Description="@($"Remove {memberName} from the team?")"
ConfirmText="Remove"
ConfirmVariant="ButtonVariant.Destructive"
OnConfirm="HandleRemoveAsync"
OnCancel="() => isOpen = false" />
@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.
Result: none
<AlertDialog OnConfirm="@(() => result = "Deleted")" OnCancel="@(() => result = "Cancelled")">
<AlertDialogTrigger>
<Button Variant="ButtonVariant.Destructive">Delete account</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete your account?</AlertDialogTitle>
<AlertDialogDescription>
This removes your account and all of its data. It can't be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep account</AlertDialogCancel>
<AlertDialogAction Variant="ButtonVariant.Destructive">Delete</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<p class="mt-3 text-sm text-muted-foreground">Result: @result</p>
@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.
Drafts discarded: 0
<Button Variant="ButtonVariant.Outline" OnClick="() => open = true">Discard draft</Button>
<AlertDialog @bind-IsOpen="open">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Discard this draft?</AlertDialogTitle>
<AlertDialogDescription>Your unsaved changes will be lost.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep editing</AlertDialogCancel>
<AlertDialogAction OnClick="@(() => discarded++)">Discard</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<p class="mt-3 text-sm text-muted-foreground">Drafts discarded: @discarded</p>
@code {
private bool open;
private int discarded;
}API Reference
| Property | Type | Default | Description |
|---|---|---|---|
IsOpen |
bool |
false |
Controls dialog visibility |
IsOpenChanged |
EventCallback<bool> |
— | 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"witharia-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