Skip to content
IzzyBlazor 0.1.0-alpha.17

Enter all results ↑ ↓ move Esc close

Alert

A status message about part of a page.

JavaScript off: what the HTML and CSS do on their own.

Usage

razor
<Alert Status="Status.Success" Title="Saved">Your changes are live.</Alert>

@* A close button that works with JavaScript off too. *@
<Alert Status="Status.Warning" Dismissible>Your trial ends in 3 days.</Alert>

@* Buttons or links under the message, written last. *@
<Alert Status="Status.Danger" Title="Couldn't save">
    The server didn't respond.
    <AlertActions><Button Size="ControlSize.Sm">Try again</Button></AlertActions>
</Alert>

An alert is part of the page, next to what it's about: a form's result, a warning above a table. For a message about the whole app, use a Banner; for news that needs no place on the page, a toast from the ToastRegion.

Status

Use Status for what the message means: it sets the color, the icon and the word screen readers hear before the message. Icon puts another icon in its place, and HideIcon leaves it out.

Preview

Info:
Update available
Version 2.4 is ready to install.
Success:
Saved
Your changes are live.
Maintenance is scheduled for Sunday.

Code

Statuses.razor
<Stack Gap="3" Grow MaxWidth="36.Rem">
    <Alert Title="Update available">Version 2.4 is ready to install.</Alert>
    <Alert Status="Status.Success" Title="Saved">Your changes are live.</Alert>
    <Alert Status="Status.Warning">Your trial ends in 3 days.</Alert>
    <Alert Status="Status.Danger" Title="Couldn't save">The server didn't respond. Try again in a moment.</Alert>
    <Alert Status="Status.Neutral" HideIcon>Maintenance is scheduled for Sunday.</Alert>
</Stack>

Variants

Use Variant for the weight: Soft (the default), Outline or Solid.

Preview

Success:
Payment received
Invoice INV-1042 is paid.
Success:
Payment received
Invoice INV-1042 is paid.
Success:
Payment received
Invoice INV-1042 is paid.

Code

Variants.razor
<Stack Gap="3" Grow MaxWidth="36.Rem">
    <Alert Status="Status.Success" Title="Payment received">Invoice INV-1042 is paid.</Alert>
    <Alert Status="Status.Success" Variant="AlertVariant.Outline" Title="Payment received">Invoice INV-1042 is paid.</Alert>
    <Alert Status="Status.Success" Variant="AlertVariant.Solid" Title="Payment received">Invoice INV-1042 is paid.</Alert>
</Stack>

Actions and dismissing

Use Dismissible for a close button, which works with JavaScript off too, and an AlertActions written last in the message for buttons or links under it.

Preview

Code

Dismiss.razor
<Stack Gap="3" Grow MaxWidth="36.Rem">
    <Alert Status="Status.Warning" Title="Unverified email" Dismissible @bind-Dismissed="_dismissed">
        Check your inbox for the link we sent to ada@example.com.
        <AlertActions>
            <Button Size="ControlSize.Sm" Variant="ButtonVariant.Outline" Accent="Accent.Neutral">Resend email</Button>
        </AlertActions>
    </Alert>
    @* Interactive pages only (and their prerender, so nothing moves): C# shows it again. *@
    @if (RendererInfo.IsInteractive || AssignedRenderMode is not null)
    {
        <Switch @bind-Value="ShowReminder" Label="Show the reminder" />
    }
</Stack>

@code {
    private bool _dismissed;

    private bool ShowReminder
    {
        get => !_dismissed;
        set => _dismissed = !value;
    }
}

Off: The close button is a checkbox and CSS: the alert hides, but focus isn't moved on, and a reload brings it back.

Closing lasts only while the reader stays on the page: a reload, another page or, with JavaScript off, any post renders the alert again. With script, a static page keeps it closed through a post, unless the alert is in the form that posted or its words changed, as an interactive page keeps it closed until Dismissed changes. So put an alert that answers a post inside that form, or say what the post did in its words ("Deleted Invoice 12"). For a message that should stay closed on every page, use a Banner, which keeps the choice in a cookie. On an interactive page, @bind-Dismissed tells C# it closed, and setting Dismissed back to false shows it again.

After a post

Render an alert from the handler to say what a post did: it comes with the response, so it shows in every mode, with or without a reload.

Preview

Code

Saved.razor
@using System.ComponentModel.DataAnnotations

<Form Model="Model" FormName="alert-saved" OnValidSubmit="Save" Grow MaxWidth="28.Rem">
    @if (_saved is not null)
    {
        <Alert Status="Status.Success" Title="Saved" Dismissible @bind-Dismissed="_dismissed">
            The project is now called @_saved.
        </Alert>
    }
    <TextField @bind-Value="Model!.Name" />
    <FormActions>
        <Button Submit>Save</Button>
    </FormActions>
</Form>

@code {
    [SupplyParameterFromForm(FormName = "alert-saved")]
    private ProjectName? Model { get; set; }

    private string? _saved;
    private bool _dismissed;

    protected override void OnInitialized() => Model ??= new();

    // What was saved, not the field: on an interactive page it changes as people type.
    // A new save shows the alert again, even if the last one was closed.
    private void Save()
    {
        _saved = Model!.Name;
        _dismissed = false;
    }
}

Off: Save reloads the page with the alert. Closing hides it until the next save renders it again; focus isn't moved.

Show what was saved, not the field: on an interactive page the field changes as people type. There, an alert closed once stays closed until Dismissed changes, so the example binds it and clears it on each save. On a static page each response draws the alert afresh, since it's in the form that posted.

Its own words

Use StatusLabel for the word read before the message ("Info", "Success", "Warning" or "Error"; nothing for Neutral; "" leaves it out), and DismissLabel for the close button's name ("Dismiss"). Text and other languages sets them for the whole app.

Parts

Written inside Alert. The API page lists their parameters.

  • AlertActions: buttons or links below an Alert's message, such as "Retry" or "Details".

Mode support

What works in each mode, Alert
FeatureOffStaticLive
Status, title, icon and actionsYesYesYes
Closes with its close buttonYesYesYes
The close button is a button, and focus moves on when it closesNoYesYes
Dismissed bound to C#NoNoYes

Off: JavaScript disabled. Static: a static SSR page with IzzyBlazor's script. Live: an interactive page (Server, WebAssembly or Auto).

Accessibility

  • Danger and Warning alerts are role="alert", so screen readers announce them at once when they appear; the rest are role="status", read politely. Announce turns this off.
  • Screen readers hear the status in words before the message ("Error: "), from StatusLabel.
  • With script, the close control is a button named "Dismiss" (DismissLabel); when it closes the alert, focus moves to the next control after it, or else the one before.
  • With JavaScript off, it's a visually hidden checkbox named "Dismiss": Tab reaches it (the ring shows on the close icon), screen readers call it a checkbox, and Space checks it. Focus isn't moved when the alert hides, so the browser drops it to the page.

Parameters

Alert parameters
PropTypeDefault
StatusWhat the message means; sets the color and icon.StatusStatus.Info
VariantHow it's drawn.AlertVariantAlertVariant.Soft
TitleA short bold line above the message.string?
ChildContentThe message, and any AlertActions written last.RenderFragment?
IconAn icon in place of the status's own.IconData?
HideIconLeaves out the icon.boolfalse
DismissibleAdds a close button.boolfalse
@bind-DismissedWhether it has been dismissed.boolfalse
AnnounceWhether screen readers announce it when it appears (role="alert" for danger and warning, role="status" otherwise).booltrue
StatusLabelWhat kind of message it is, read to screen readers before it (the icon and color carry it visually).string?computed
DismissLabelThe close button's accessible name.string?"Dismiss"
IdThe element id.string?

See also

  • Banner: a full-width message about the whole app.
  • ToastRegion: toasts from any render mode, even across a redirect.
  • EmptyState: what to show when there's nothing yet.