Web Components Navigation Drawer Overview

    The Ignite UI for Web Components 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.

    Web Components Navigation Drawer Example

    This sample demonstrates how to create IgcNavDrawerComponent component.

    EXAMPLE
    TS
    HTML
    add-drawer-items.css
    index.css

    Like this sample? Get access to our complete Ignite UI for Web Components toolkit and start building your own apps in minutes. Download it for free.

    Usage

    First, you need to install the Ignite UI for Web Components by running the following command:

    npm install igniteui-webcomponents
    cmd
    import { defineComponents, IgcNavDrawerComponent } from 'igniteui-webcomponents';
    
    defineComponents(IgcNavDrawerComponent);
    ts

    For a complete introduction to the Ignite UI for Web Components, read the Getting Started topic.

    Ignite UI for Web Components | CTA Banner

    Adding Navigation Drawer Items

    The simplest way to start using the IgcNavDrawerComponent is as follows:

    <igc-nav-drawer open="true">
        <igc-nav-drawer-header-item>
            Sample Drawer
        </igc-nav-drawer-header-item>
        <igc-nav-drawer-item>
            <igc-icon slot="icon" name="home"></igc-icon>
            <span slot="content">Home</span>
        </igc-nav-drawer-item>
        <igc-nav-drawer-item>
            <igc-icon slot="icon" name="search"></igc-icon>
            <span slot="content">Search</span>
        </igc-nav-drawer-item>
    </igc-nav-drawer>
    html

    If all went well, you should see the following in your browser:

    EXAMPLE
    TS
    HTML
    add-drawer-items.css
    index.css

    While any content can be provided in the drawer, the IgcNavDrawerItemComponent 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 IgcNavbarComponent. 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:

    <igc-navbar>
      <igc-icon slot="start" name="menu" id="menu"></igc-icon>
      <h2>Home</h2>
    </igc-navbar>
    
    <igc-nav-drawer id="navDrawer">
        <igc-nav-drawer-header-item>
            Sample Drawer
        </igc-nav-drawer-header-item>
        <igc-nav-drawer-item>
            <igc-icon slot="icon" name="home"></igc-icon>
            <span slot="content">Home</span>
        </igc-nav-drawer-item>
        <igc-nav-drawer-item>
            <igc-icon slot="icon" name="search"></igc-icon>
            <span slot="content">Search</span>
        </igc-nav-drawer-item>
    </igc-nav-drawer>
    html

    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.

    // ...
    import { defineComponents, IgcNavDrawerComponent, IgcRadioComponent, IgcRadioGroupComponent } from 'igniteui-webcomponents';
    
    defineComponents(IgcNavDrawerComponent, IgcRadioComponent, IgcRadioGroupComponent);
    this.navDrawer = document.getElementById('navDrawer') as IgcNavDrawerComponent;
    this.radioGroup = document.getElementById('radio-group') as IgcRadioGroupComponent;
    this.radioGroup.addEventListener('click', (radio: any) => {
        this.navDrawer.position = radio.target.value;
    });
    ts
    <igc-radio-group id="radio-group" alignment="horizontal">
        <igc-radio name="position" value="start" label-position="after" checked>Start</igc-radio>
        <igc-radio name="position" value="end" label-position="after">End</igc-radio>
        <igc-radio name="position" value="top" label-position="after">Top</igc-radio>
        <igc-radio name="position" value="bottom" label-position="after">Bottom</igc-radio>
    </igc-radio-group>
    html

    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:

    // ...
    const menu = document.getElementById('menu');
    const navDrawer = document.querySelector('igc-nav-drawer') as IgcNavDrawerComponent;
    
    menu!.addEventListener('click', () => {
        navDrawer.show();
    })
    
    document.getElementById('root')!.onclick = (e) => {
        if (e.target != document.getElementById('navDrawer')) {
            navDrawer.hide();
        }
    }
    ts

    If all goes well, your component should now look like this:

    EXAMPLE
    TS
    HTML
    CSS

    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.

    <igc-nav-drawer position="start">
        <igc-nav-drawer-header-item>Sample Drawer</igc-nav-drawer-header-item>
        <igc-nav-drawer-item>
            <igc-icon slot="icon" name="home"></igc-icon>
            <span slot="content">Home</span>
        </igc-nav-drawer-item>
        <igc-nav-drawer-item>
            <igc-icon slot="icon" name="search"></igc-icon>
            <span slot="content">Search</span>
        </igc-nav-drawer-item>
        <div slot="mini">
            <igc-nav-drawer-item>
                <igc-icon slot="icon" name="home"></igc-icon>
            </igc-nav-drawer-item>
            <igc-nav-drawer-item>
                <igc-icon slot="icon" name="search"></igc-icon>
            </igc-nav-drawer-item>
        </div>
    </igc-nav-drawer>
    html

    And here's the result:

    EXAMPLE
    TS
    HTML
    CSS

    Styling

    The IgcNavDrawerComponent 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);
    }
    scss

    EXAMPLE
    TS
    HTML
    drawer-styling.css
    index.css

    API References

    Additional Resources