Web Components Banner Overview
The Ignite UI for Web Components 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 Web Components Banner Example
Usage
First, you need to install the Ignite UI for Web Components by running the following command:
npm install igniteui-webcomponents
You will then need to import the IgcBannerComponent
, its necessary CSS, and register its module, like so:
import { defineComponents, IgcBannerComponent } from "igniteui-webcomponents";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcBannerComponent);
For a complete introduction to the Ignite UI for Web Components, 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.
<igc-button onclick="banner.show()">Show Banner</igc-button>
<igc-banner id="banner">
You are currently offline.
</igc-banner>
[!NOTE] The
IgcBannerComponent
includes a default action buttonOK
, which closes the banner.
Examples
The IgcBannerComponent
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 igc-banner
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:
<igc-banner id="banner">
You have lost connection to the internet. This app is offline.
</igc-banner>
Adding an icon
An IgcIconComponent
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
IgcIconComponent
elements are inserted, the banner will try to position all of them at the beginning. It is strongly advised to pass only oneIgcIconComponent
directly to the banner.
To pass an IgcIconComponent
to your banner, use the prefix
slot:
<igc-banner id="banner">
<igc-icon slot="prefix" name="signal_wifi_off"></igc-icon>
You have lost connection to the internet. This app is offline.
</igc-banner>
If you want to use an IgcIconComponent
in your banner message, simply insert it in the banner's content:
<igc-banner id="banner">
You have lost connection to the internet. This app is offline.
<igc-icon name="signal_wifi_off"></igc-icon>
</igc-banner>
Changing the banner button
The IgcBannerComponent
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.
<igc-banner id="banner">
<igc-icon slot="prefix" name="signal_wifi_off"></igc-icon>
You have lost connection to the internet. This app is offline.
<div slot="actions">
<igc-button onclick="banner.toggle()">
<igc-ripple></igc-ripple>
Toggle Banner
</igc-button>
</div>
</igc-banner>
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.
<igc-banner id="banner">
...
</igc-banner>
const banner = document.getElementById('banner') as IgcBannerComponent;
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:
<igc-banner id="banner">
<igc-icon slot="prefix" name="signal_wifi_off"></igc-icon>
You have lost connection to the internet. This app is offline.
<div slot="actions">
<igc-button onclick="banner.hide()">
<igc-ripple></igc-ripple>
Continue Offline
</igc-button>
<igc-button id="button">
<igc-ripple></igc-ripple>
Turn On Wifi
</igc-button>
</div>
</igc-banner>
According to Google's Material Design guidelines, a banner should have a maximum of 2 buttons present. The
IgcBannerComponent
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:
<igc-navbar>
<h1>Gallery</h1>
<igc-icon id="icon" slot="end" name="signal_wifi_off"></igc-icon>
</igc-navbar>
<igc-banner id="banner">
...
<div slot="actions">
...
<igc-button id="button">
<igc-ripple></igc-ripple>
Turn On Wifi
</igc-button>
</div>
</igc-banner>
private banner: IgcBannerComponent;
private icon: IgcIconComponent;
private button: IgcButtonComponent;
private wifiState: boolean = false;
constructor() {
this.banner = document.getElementById('banner') as IgcBannerComponent;
this.icon = document.getElementById('icon') as IgcIconComponent;
this.button = document.getElementById('button') as IgcButtonComponent;
this.icon.addEventListener('click', () => this.refreshBanner());
this.button.addEventListener('click', () => this.refreshBanner());
}
public refreshBanner() {
if (!this.wifiState) {
this.icon.name = 'signal_wifi_4_bar';
this.banner.hide();
} else {
this.icon.name = 'signal_wifi_off';
this.banner.show();
}
this.wifiState = !this.wifiState;
}
Finally, we will add a IgcToastComponent
, displaying a message about the WiFi state. The results of the templated banner can be seen in the demo below:
Styling
The banner component exposes several CSS parts (base
, spacer
, message
, illustration
, content
and actions
) to give you full control over its style.
igc-banner::part(spacer) {
background: #dedede;
}
igc-banner::part(illustration) {
color: #666666;
}
igc-banner::part(content) {
color: #151515;
}
API References
IgcBannerComponent
IgcCardComponent
IgcIconComponent
IgcNavbarComponent
IgcToastComponent
IgcRippleComponent