Blazor Toast Overview
The Blazor Toast is a super lightweight and small pop-up component that is used for displaying a message content, notifying end-users about the status of a changed record. You can easily position and show Blazor toast notifications at the bottom or at any other specified area of the screen. Or you can also dismiss them in a simple and easy way.
The Blazor Toast component is primarily used for system messaging, push notifications, warning messages, and information. It cannot be dismissed by the user. This control has different features like animation effects, display time property to configure how long the toast component is visible, styling, and others.
Blazor Toast Example
Take a look at the simple Ignite UI for Blazor Toast example below. The animated notification message pops up after clicking on the button.
How To Use Ignite UI for Blazor Toast Notification
Register the Ignite UI for Blazor services:
// in Program.cs file
builder.Services.AddIgniteUIBlazor();
Registering IgbToastModule, the client resource module of the IgbToast, is optional: the component loads its resources when it first renders. Listing it in builder.Services.AddIgniteUIBlazor(typeof(IgbToastModule)) can be used to preload resources or to support elements created only on the client (see Component Modules).
For a complete introduction to the Ignite UI for Blazor, read the Getting Started topic.
You will also need to link an additional CSS file to apply the styling to the IgbToast component. The following needs to be placed in the app’s host page (wwwroot/index.html in a standalone Blazor WebAssembly or Blazor Hybrid app, Pages/_Host.cshtml in a Blazor Server app, Components/App.razor in a Blazor Web App):
<link href="_content/IgniteUI.Blazor/themes/light/bootstrap.css" rel="stylesheet" />
The simplest way to display the toast component is to use its Show method and call it on a button click.
<IgbButton @onclick=@OnToastButtonClick Variant=@ButtonVariant.Contained>Show Toast</IgbButton>
<IgbToast @ref="ToastRef">Toast Message</IgbToast>
@code {
public IgbToast ToastRef { get; set; }
protected override void OnInitialized()
{
}
public void OnToastButtonClick(MouseEventArgs args)
{
if (this.ToastRef != null)
{
this.ToastRef.Show();
}
}
}
Examples
Properties
Use the DisplayTime property to configure how long the toast component is visible. By default, it’s set to 4000 milliseconds.
By default, the toast component is hidden automatically after a period specified by the DisplayTime. You can use KeepOpen property to change this behavior. In this way, the toast will remain visible.
<IgbButton @onclick=@OnToggleToastButtonClick Variant="ButtonVariant.Contained">Toggle Toast</IgbButton>
<IgbButton @onclick=@OnToggleKeepOpenButtonClick Variant="ButtonVariant.Contained">Toggle KeepOpen Property</IgbButton>
<IgbButton @onclick=@OnDisplayTimeButtonClick Variant="ButtonVariant.Contained">Set DisplayTime to 8000</IgbButton>
<IgbToast @ref=ToastRef>Toast Message</IgbToast>
@code {
public IgbToast ToastRef{ get; set; }
protected override void OnInitialized()
{
}
public void OnToggleToastButtonClick(MouseEventArgs args)
{
if (this.ToastRef != null)
{
this.ToastRef.Toggle();
}
}
public void OnToggleKeepOpenButtonClick(MouseEventArgs args)
{
if (this.ToastRef != null)
{
this.ToastRef.KeepOpen = !this.ToastRef.KeepOpen;
}
}
public void OnDisplayTimeButtonClick(MouseEventArgs args)
{
if (this.ToastRef != null)
{
this.ToastRef.DisplayTime = 8000;
}
}
}
Styling
You can style the Blazor IgbToast notifications directly using its tag selector:
igc-toast {
background-color: var(--ig-primary-500);
color: var(--ig-primary-500-contrast);
}