The Ignite UI for Blazor Banner component provides a way to easily display a prominent message to your application's users in a way that is less transient than a snackbar and less obtrusive than a dialog. It can also indicate actions to take based on the context of the message.
Ignite UI for Blazor Banner Example
EXAMPLE
MODULES
RAZOR
CSS
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modulesnamespaceInfragistics.Samples
{
publicclassProgram
{
publicstaticasync Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbBannerModule),
typeof(IgbNavbarModule),
typeof(IgbCardModule),
typeof(IgbIconModule)
);
await builder.Build().RunAsync();
}
}
}cs
Before using the IgbBanner, you need to register it as follows:
// in Program.cs file
builder.Services.AddIgniteUIBlazor(typeof(IgbBannerModule));
razor
You will also need to link an additional CSS file to apply the styling to the IgbBanner component. The following needs to be placed in the wwwroot/index.html file in a Blazor Web Assembly project or the Pages/_Host.cshtml file in a Blazor Server project:
For a complete introduction to the Ignite UI for Blazor, read the Getting Started topic.
Show Banner
In order to display the banner component, use its Show method and call it on a button click. The banner appears relative to where the element was inserted in the page template, moving all other content. It typically shows some non-intrusive content that requires minimal user interaction to be dismissed.
<IgbButton @onclick="ShowBanner">Show Banner</IgbButton><IgbBanner @ref="bannerRef">
You are currently offline.
</IgbBanner>@code {private IgbBanner bannerRef;
privatevoidShowBanner()
{
this.bannerRef.ShowAsync();
}
}razor
The IgbBanner includes a default action button OK, which closes the banner.
Examples
The IgbBanner component allows templating of its content while still sticking as closely as possible to the material design banner guidelines.
Changing the banner message
Configuring the message displayed in the banner is easy - just change the content you are passing to the IgbBanner tag. The text will show up in the specified banner area and the banner will use its default template when displaying it. Below, we will change the content of our sample banner to be a bit more descriptive:
<IgbBanner @ref="bannerRef">
You have lost connection to the internet. This app is offline.
</IgbBanner>razor
Adding an icon
An IgbIcon can be displayed in the banner by using the banner's prefix slot. The icon will always be positioned at the beginning of the banner message.
If several IgbIcon elements are inserted, the banner will try to position all of them at the beginning. It is strongly advised to pass only one IgbIcon directly to the banner.
To pass an IgbIcon to your banner, use the prefix slot:
<IgbBanner @ref="bannerRef"><IgbIconslot="prefix"IconName="signal_wifi_off"Collection="material"></IgbIcon>
You have lost connection to the internet. This app is offline.
</IgbBanner>razor
If you want to use an IgbIcon in your banner message, simply insert it in the banner's content:
<IgbBanner @ref="bannerRef">
You have lost connection to the internet. This app is offline.
<IgbIconIconName="signal_wifi_off"Collection="material"></IgbIcon></IgbBanner>razor
Changing the banner button
The IgbBanner exposes the actions slot for templating the banner buttons. This allows you to override the default banner button (OK) and add user-defined custom actions.
<IgbBanner @ref="bannerRef"><IgbIconslot="prefix"IconName="signal_wifi_off"Collection="material"></IgbIcon>
You have lost connection to the internet. This app is offline.
<divslot="actions"><IgbButtonVariant="ButtonVariant.Flat" @onclick="OnButtonClick">
Toggle Banner
<IgbRipple /></IgbButton></div></IgbBanner>@code {private IgbBanner bannerRef;
privatevoidOnButtonClick()
{
this.bannerRef.ToggleAsync();
}
}razor
EXAMPLE
MODULES
RAZOR
CSS
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modulesnamespaceInfragistics.Samples
{
publicclassProgram
{
publicstaticasync Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbBannerModule),
typeof(IgbNavbarModule),
typeof(IgbCardModule),
typeof(IgbIconModule),
typeof(IgbButtonModule),
typeof(IgbRippleModule)
);
await builder.Build().RunAsync();
}
}
}cs
/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/css
Binding to events
The banner component emits the igcClosing and igcClosed events when being closed. The igcClosing event is cancelable - it uses the CustomEvent interface and the emitted object has its cancelable property set to true. If we cancel the igcClosing event, the corresponding end action and event will not be triggered - the banner will not be closed and the igcClosed event will not be emitted.
To cancel the closing event, call the preventDefault method.
If the changes above are applied, the banner will never close, as the closing event is always cancelled.
Advanced Example
Let's create a banner with two custom buttons - one for dismissing the notification and one for turning on the connection. We can pass custom action handlers using the actions slot:
<IgbBanner @ref="bannerRef"><IgbIconIconName="signal_wifi_off"Collection="material"slot="prefix"></IgbIcon>
You have lost connection to the internet. This app is offline.
<divslot="actions"><IgbButtonVariant="ButtonVariant.Flat" @onclick="HideBanner">
Continue Offline
<IgbRipple /></IgbButton><IgbButtonVariant="ButtonVariant.Flat" @onclick="RefreshBanner">
Turn On Wifi
<IgbRipple /></IgbButton></div></IgbBanner>@code {private IgbBanner bannerRef;
privatevoidHideBanner()
{
this.bannerRef.HideAsync();
}
}razor
According to Google's Material Design guidelines, a banner should have a maximum of 2 buttons present. The IgbBanner does not explicitly limit the number of elements under the actions slot, but it is strongly recommended to use up to 2 if you want to adhere to the material design guidelines.
The dismiss option (Continue Offline) doesn't need any further logic, so it can just call the Hide method. The confirm action (Turn On Wifi), however, requires some additional logic, so we have to define it in the component. Then, we will add an event listener for the click event. The last step is to call the refreshBanner() method on each change, which will toggle the banner depending on the wifiState.
The navbar will have a Wifi icon and we will add an event listener for its click event as well. As the refreshBanner() method is called on each change, the icon will not only toggle the banner, but change according to the state of the connection:
Finally, we will add a IgbToast, displaying a message about the WiFi state. The results of the templated banner can be seen in the demo below:
EXAMPLE
MODULES
RAZOR
CSS
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modulesnamespaceInfragistics.Samples
{
publicclassProgram
{
publicstaticasync Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbBannerModule),
typeof(IgbNavbarModule),
typeof(IgbCardModule),
typeof(IgbIconModule),
typeof(IgbButtonModule),
typeof(IgbRippleModule),
typeof(IgbToastModule)
);
await builder.Build().RunAsync();
}
}
}cs
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modulesnamespaceInfragistics.Samples
{
publicclassProgram
{
publicstaticasync Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbBannerModule),
typeof(IgbNavbarModule),
typeof(IgbCardModule),
typeof(IgbIconModule),
typeof(IgbButtonModule),
typeof(IgbRippleModule),
typeof(IgbToastModule)
);
await builder.Build().RunAsync();
}
}
}cs