React Navigation Drawer Overview
The Ignite UI for React Navigation Drawer provides side navigation that can be expanded or collapsed within the content. A mini version provides quick access to navigation even when closed. Its content is completely customizable while also providing default menu item styling.
React Navigation Drawer Example
This sample demonstrates how to create IgrNavDrawer
component.
Usage
First, you need to the install the corresponding Ignite UI for React npm package by running the following command:
npm install igniteui-react
You will then need to import the IgrNavDrawer
, its necessary CSS, and register its module, like so:
import { IgrNavDrawerModule, IgrNavDrawer, IgrNavDrawerHeaderItem, IgrNavDrawerItem } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrNavDrawerModule.register();
For a complete introduction to the Ignite UI for React, read the Getting Started topic.
Adding Navigation Drawer Items
The simplest way to start using the IgrNavDrawer
is as follows:
<IgrNavDrawer open={true}>
<IgrNavDrawerHeaderItem>
<span>Sample Drawer</span>
</IgrNavDrawerHeaderItem>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="home" collection="material" />
</div>
<span slot="content">Home</span>
</IgrNavDrawerItem>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="search" collection="material" />
</div>
<span slot="content">Search</span>
</IgrNavDrawerItem>
</IgrNavDrawer>
If all went well, you should see the following in your browser:
Navbar Integration
While any content can be provided in the drawer, the IgrNavDrawerItem
is available to apply out-of-the-box styling to the items.
To enhance our component a bit, we can use it in conjunction with the IgrNavbar
. This way we can achieve a more completed look and use the drawer's methods. Let's look at how we can use this in the next example:
<IgrNavbar>
<div slot="start">
<IgrIcon name="menu" collection="material"/>
</div>
<h2>Home</h2>
</IgrNavbar>
<IgrNavDrawer>
<IgrNavDrawerHeaderItem>
<span>Sample Drawer</span>
</IgrNavDrawerHeaderItem>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="home" collection="material" />
</div>
<span slot="content">Home</span>
</IgrNavDrawerItem>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="search" collection="material" />
</div>
<span slot="content">Search</span>
</IgrNavDrawerItem>
</IgrNavDrawer>
Let's also add some radio buttons to display all position
values. This way whenever one gets selected, we will change the position of the drawer.
<IgrRadioGroup alignment="horizontal">
<IgrRadio value="start" labelPosition="after" checked={true} change={this.onRadioChange}>
<span>Start</span>
</IgrRadio>
<IgrRadio value="end" labelPosition="after" change={this.onRadioChange}>
<span>End</span>
</IgrRadio>
<IgrRadio value="top" labelPosition="after" change={this.onRadioChange}>
<span>Top</span>
</IgrRadio>
<IgrRadio value="bottom" labelPosition="after" change={this.onRadioChange}>
<span>Bottom</span>
</IgrRadio>
</IgrRadioGroup>
<IgrNavDrawer position={this.state.drawerPosition} />
public onRadioChange(e: any) {
if (e.checked == true) {
this.setState({ drawerPosition: e.value });
}
}
And finally, we need a way to open and close our navigation drawer. There are a couple of ways to achieve this, but for the sake of this example we will do the following:
public onMenuIconClick() {
if (this.navDrawerRef) {
this.navDrawerRef.show();
}
}
If all goes well, your component should now look like this:
Mini Variant
With the mini variant, the Navigation Drawer changes its width instead of closing. It is used to maintain quick navigation, available at all times, leaving just the icons. To achieve that, you only need to set up the mini
slot of the drawer.
<IgrNavDrawer>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="home" collection="material" />
</div>
<span slot="content">Home</span>
</IgrNavDrawerItem>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="search" collection="material"/>
</div>
<span slot="content">Search</span>
</IgrNavDrawerItem>
<div slot="mini">
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="home" collection="material"/>
</div>
</IgrNavDrawerItem>
<IgrNavDrawerItem>
<div slot="icon">
<IgrIcon name="search" collection="material" />
</div>
</IgrNavDrawerItem>
</div>
</IgrNavDrawer>
<IgrButton clicked={this.onButtonClick}>
<span>Toggle</span>
</IgrButton>
And here's the result:
Styling
The IgrNavDrawer
exposes several CSS parts - base
, main
, and mini
, giving you full control over their styling.
igc-nav-drawer::part(base) {
background: var(--ig-secondary-500);
}
igc-nav-drawer-item::part(base) {
color: var(--ig-secondary-500-contrast);
}
igc-nav-drawer-item::part(base):hover {
background-color: var(--ig-gray-800);
}
igc-nav-drawer-item[active]::part(base) {
background: var(--ig-warn-500);
color: var(--ig-warn-500-contrast);
}
igc-nav-drawer-header-item {
color: var(--ig-warn-500);
}
API References
IgrButton
IgrIcon
IgrNavDrawerHeaderItem
IgrNavDrawerItem
IgrNavDrawer
IgrNavbar
IgrRadioGroup
IgrRadio
Styling & Themes