Blazor Banner Overview
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
Usage
Before using the IgbBanner
, you need to register it as follows:
// in Program.cs file
builder.Services.AddIgniteUIBlazor(typeof(IgbBannerModule));
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:
<link href="_content/IgniteUI.Blazor/themes/light/bootstrap.css" rel="stylesheet" />
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;
private void ShowBanner()
{
this.bannerRef.ShowAsync();
}
}
[!NOTE] The
IgbBanner
includes a default action buttonOK
, 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>
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.
[!NOTE] 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 oneIgbIcon
directly to the banner.
To pass an IgbIcon
to your banner, use the prefix
slot:
<IgbBanner @ref="bannerRef">
<IgbIcon slot="prefix" IconName="signal_wifi_off" Collection="material"></IgbIcon>
You have lost connection to the internet. This app is offline.
</IgbBanner>
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.
<IgbIcon IconName="signal_wifi_off" Collection="material"></IgbIcon>
</IgbBanner>
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">
<IgbIcon slot="prefix" IconName="signal_wifi_off" Collection="material"></IgbIcon>
You have lost connection to the internet. This app is offline.
<div slot="actions">
<IgbButton Variant="ButtonVariant.Flat" @onclick="OnButtonClick">
Toggle Banner
<IgbRipple />
</IgbButton>
</div>
</IgbBanner>
@code {
private IgbBanner bannerRef;
private void OnButtonClick()
{
this.bannerRef.ToggleAsync();
}
}
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.
<IgbBanner id="banner">
...
</IgbBanner>
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JS.InvokeVoidAsync("handleClosing");
}
}
}
//In JavaScript:
function handleClosing() {
const banner = document.getElementById('banner');
banner.addEventListener('igcClosing', (event) => {
event.preventDefault();
});
}
[!NOTE] 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">
<IgbIcon IconName="signal_wifi_off" Collection="material" slot="prefix"></IgbIcon>
You have lost connection to the internet. This app is offline.
<div slot="actions">
<IgbButton Variant="ButtonVariant.Flat" @onclick="HideBanner">
Continue Offline
<IgbRipple />
</IgbButton>
<IgbButton Variant="ButtonVariant.Flat" @onclick="RefreshBanner">
Turn On Wifi
<IgbRipple />
</IgbButton>
</div>
</IgbBanner>
@code {
private IgbBanner bannerRef;
private void HideBanner()
{
this.bannerRef.HideAsync();
}
}
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 theactions
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:
<IgbNavbar>
<h1>Gallery</h1>
<IgbIcon @ref="iconRef" IconName="@iconName" Collection="material" slot="end" @onclick="RefreshBanner"></IgbIcon>
</IgbNavbar>
<IgbBanner @ref="bannerRef">
...
<div slot="actions">
...
<IgbButton Variant="ButtonVariant.Flat" @onclick="RefreshBanner">
Turn On Wifi
<IgbRipple />
</IgbButton>
</div>
</IgbBanner>
@code {
private IgbBanner bannerRef;
private string iconName = "signal_wifi_off";
private bool wifiState = false;
private void RefreshBanner()
{
if (!this.wifiState)
{
this.iconName = "signal_wifi_4_bar";
this.bannerRef.HideAsync();
}
else
{
this.iconName = "signal_wifi_off";
this.bannerRef.ShowAsync();
}
this.wifiState = !this.wifiState;
}
}
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:
Styling
The IgbBanner
component exposes several CSS parts which give you full control over its style:
Name | Description |
---|---|
base |
The base wrapper of the banner component. |
spacer |
The inner wrapper that sets the space around the banner. |
message |
The part that holds the text and the illustration. |
illustration |
The part that holds the banner icon/illustration. |
content |
The part that holds the banner text content. |
actions |
The part that holds the banner action buttons. |
igc-banner::part(spacer) {
background: var(--ig-surface-600);
}
igc-banner::part(illustration) {
color: var(--ig-surface-600-contrast);
}
igc-banner::part(content) {
color: var(--ig-gray-900);
}