ConfirmDialog
Since 0.1.0-alpha.24One dialog that asks before any of several things act: a menu item, a row's action, an icon button, a link. It names what it's for (For), and they stay as they are: a click on one opens the dialog, and confirming does what the click would have done, whether that posts a form, follows a link or runs C#.
Asking before each row's action
One ConfirmDialog asks for every row's action: set For to an id, or to a selector such as .revoke-key. To name the row in the question, write {0} in Title and put data-confirm on each button. For a single button that asks first, use a ConfirmButton.
Static
Preview
Code
<form method="post" @onsubmit="() => Revoke(Posted ?? _revoked)" @formname="api-keys" data-enhance>
<AntiforgeryToken />
<Card Title="API keys" Size="ControlSize.Sm">
<List Plain>
foreach (var key in Keys)
{
<ListItem Icon="Lucide.KeyRound" Description="key.Used">
key.Name
<ListItemActions>
if (_revoked == key.Id)
{
<Badge Status="Status.Neutral">Revoked</Badge>
}
else
{
<IconButton Class="revoke-key" data-confirm="key.Name" Label="($"Revoke {key.Name}")" Icon="Lucide.Trash"
Submit Name="revoke" Value="key.Id" OnClick="() => Revoke(key.Id)"
Variant="ButtonVariant.Ghost" Accent="Accent.Neutral" Size="ControlSize.Sm" />
}
</ListItemActions>
</ListItem>
}
</List>
</Card>
</form>
@* One dialog for every row's button: {0} is the clicked button's data-confirm. *@
<ConfirmDialog For=".revoke-key" Title="Revoke {0}?" Message="Apps that use this key stop working at once."
ConfirmText="Revoke key" Accent="Accent.Danger" />
{
// Static pages: the confirmed button's Name=Value posts with the form.
[SupplyParameterFromForm(FormName = "api-keys", Name = "revoke")]
private string? Posted { get; set; }
private string? _revoked;
// Interactive pages post nothing (Posted stays empty), so OnClick says which key it was.
private void Revoke(string? id) => _revoked = id;
}Static: Confirming posts the form with the pressed button's revoke value.
Add this example to a page with the izzy CLI:
izzy add component ConfirmDialog --at <file>:<line>Details
For is an id, or a CSS selector when it starts with "#", "." or "[", so one dialog serves every row of a list. {0} in Title and Message is the data-confirm value of the one clicked, such as the row's name.
Put it outside any form. For a button of its own that asks first, with JavaScript off too, use ConfirmButton.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| A click on what it's for opens the dialog instead of acting | Yes | Yes |
| Confirming posts the form, follows the link or runs the C# of the one clicked | Yes | Yes |
| {0} in Title and Message names the one clicked | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, the buttons and links it's for act without asking.
Accessibility
- A modal
dialogwithrole="alertdialog", named byTitleand described byMessageandBody. - Cancel ("Cancel",
CancelText) takes focus when it opens, so Enter doesn't confirm by accident. - While it's open, focus stays in the dialog; Escape closes it and focus returns to what was clicked, or to its menu's button when that was a menu item. A backdrop click doesn't close it, and there's no close button.
- With JavaScript off, what it's for acts without asking.
Parameters
| Prop | Type | Default |
|---|---|---|
ForWhat it asks for: the id of a button or link, or a CSS selector when it starts with #, . or [, such as .delete-note for every row's Delete. | string | Required |
TitleThe question, such as "Delete this note?". | string | Required |
MessageA line under the question, such as "This can't be undone." {0} works as in Title. | string? | |
BodyMore content under the question, the same for whichever was clicked. | RenderFragment? | |
ConfirmTextThe confirm button's text, such as "Delete". | string? | "Confirm" |
CancelTextThe cancel button's text. | string? | "Cancel" |
AccentThe confirm button's color: Danger for deleting. | Accent | Accent.Primary |
IdThe dialog's id. | string? |
See also
- ConfirmButton: asks the user to confirm in an alert dialog before acting.
- Dialog: opens a native modal dialog from a trigger, a link or C#.
- Menu: opens a popover menu of links, form posts and actions.