Blazor Alerts
Provide contextual feedback messages for typical user actions with the handful of available and flexible Blazor Bootstrap alert messages.
Parameters
Name | Type | Default | Required | Description | Added Version |
---|---|---|---|---|---|
ChildContent | RenderFragment? | ✔️ | Gets or sets the content to be rendered within the component. | 1.0.0 | |
Color | AlertColor | AlertColor.None | Gets or sets the alert color. | 1.0.0 | |
Dismissable | bool | false | If true, shows an inline close button. | 1.0.0 |
Methods
Name | Description | Added Version |
---|---|---|
CloseAsync | Closes an alert by removing it from the DOM. | 1.0.0 |
Callback Events
Name | Description | Added Version |
---|---|---|
OnClose | Fires immediately when the close instance method is called. | 1.0.0 |
OnClosed | Fired when the alert has been closed and CSS transitions have completed. | 1.0.0 |
Examples
Alerts
Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight colors.
<div>
<Alert Color="AlertColor.Primary"> A simple primary alert - check it out! </Alert>
<Alert Color="AlertColor.Secondary"> A simple secondary alert - check it out! </Alert>
<Alert Color="AlertColor.Success"> A simple success alert - check it out! </Alert>
<Alert Color="AlertColor.Danger"> A simple danger alert - check it out! </Alert>
<Alert Color="AlertColor.Warning"> A simple warning alert - check it out! </Alert>
<Alert Color="AlertColor.Info"> A simple info alert - check it out! </Alert>
<Alert Color="AlertColor.Light"> A simple light alert - check it out! </Alert>
<Alert Color="AlertColor.Dark"> A simple dark alert - check it out! </Alert>
</div>
Additional Content
Alerts can also contain additional HTML elements like headings, paragraphs and dividers.
<div>
<Alert Color="AlertColor.Success">
<h4 class="alert-heading">Well done!</h4>
<p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
<hr>
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</Alert>
</div>
See alerts with additional content demo.
Icons
Similarly, you can use Bootstrap Icons to create alerts with icons.
<div>
<Alert Color="AlertColor.Primary"> <Icon Name="IconName.InfoCircleFill" class="me-2"></Icon>An example alert with an icon </Alert>
<Alert Color="AlertColor.Success"> <Icon Name="IconName.CheckCircleFill" class="me-2"></Icon>A simple success alert with an icon </Alert>
<Alert Color="AlertColor.Danger"> <Icon Name="IconName.ExclamationTriangleFill" class="me-2"></Icon>A simple danger alert with an icon </Alert>
<Alert Color="AlertColor.Warning"> <Icon Name="IconName.ExclamationTriangleFill" class="me-2"></Icon>A simple warning alert with an icon </Alert>
</div>
Dismissing
- Using the
Dismissable="true"
, it's possible to dismiss any alert inline.
It's possible to dismiss any alert inline. Here's how:
<div>
<Alert Color="AlertColor.Warning" Dismissable="true"> <strong>Holy guacamole!</strong> You should check in on some of those fields below. </Alert>
</div>
- Manually we can close an alert with button click.
<div>
<Alert @ref="warningAlert" Color="AlertColor.Warning">
<strong>Holy guacamole!</strong> You should check in on some of those fields below. <Button Color="ButtonColor.Primary" @onclick="CloseAlert">Close</Button>
</Alert>
</div>
@code {
Alert warningAlert;
private async Task CloseAlert() => await warningAlert?.CloseAsync();
}
When an alert is dismissed, the element is completely removed from the page structure. If a keyboard user dismisses the alert using the close button, their focus will suddenly be lost and, depending on the browser, reset to the start of the page/document. For this reason, we recommend subscribing to the OnClosed
callback event and programmatically sets focus to the most appropriate location on the page.