DevExtreme Angular - Getting Started with Navigation Drawer
The Drawer is a dismissible or permanently visible panel used for navigation in responsive web application layouts.
DevExtreme provides an application template for Angular. It implements a responsive layout that uses the Drawer. You can use this template instead of following the tutorial. Refer to the documentation on GitHub for more information.
If you do not use Angular or the template is unsuitable, follow the instructions in this tutorial. We create a Drawer that allows a user to switch between pages. The Drawer is opened and closed via a button on a toolbar.
Refer to the subtopics for details on every configuration step. You can also see the full code below:
- <dx-toolbar id="toolbar">
- <dxi-item
- widget="dxButton"
- [options]="buttonOptions"
- location="before">
- </dxi-item>
- </dx-toolbar>
- <dx-drawer
- template="template"
- [height]="250"
- [(opened)]="isOpened"
- [minSize]="37"
- revealMode="expand"
- openedStateMode="overlap">
- <div *dxTemplate="let data of 'template'">
- <dx-list
- [items]="navigation"
- [width]="200"
- selectionMode="single"
- (onSelectionChanged)="loadView($event)">
- </dx-list>
- </div>
- <div id="view"><router-outlet></router-outlet></div>
- </dx-drawer>
- import { Component } from "@angular/core";
- import { Router } from "@angular/router";
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css'],
- providers: []
- })
- export class AppComponent {
- navigation: any;
- isOpened: Boolean = false;
- buttonOptions: any;
- constructor(private router: Router) {
- this.buttonOptions = {
- icon: "menu",
- onClick: () => {
- this.isOpened = !this.isOpened;
- }
- };
- this.navigation = [
- { id: 1, text: "Inbox", icon: "message", filePath: "inbox" },
- { id: 2, text: "Sent Mail", icon: "check", filePath: "sent-mail" },
- { id: 3, text: "Trash", icon: "trash", filePath: "trash" },
- { id: 4, text: "Spam", icon: "mention", filePath: "spam" }
- ];
- }
- loadView(e) {
- this.router.navigate([e.addedItems[0].filePath])
- }
- }
- ::ng-deep .dx-drawer-panel-content, ::ng-deep .dx-overlay-content {
- background-color: lightgray;
- }
- ::ng-deep #toolbar {
- background-color: rgba(191, 191, 191, .15);
- padding: 5px 10px;
- }
- ::ng-deep .dx-toolbar-button .dx-button {
- background-color: rgba(191, 191, 191, -0.15);
- border: none;
- }
- ::ng-deep .dx-toolbar-button > .dx-toolbar-item-content {
- margin-left: -7px;
- }
- ::ng-deep .dx-list-item-icon {
- margin-right: 10px;
- }
- ::ng-deep #view {
- margin-left: 10px;
- margin-top: 10px;
- }
- import { BrowserModule } from "@angular/platform-browser";
- import { NgModule } from "@angular/core";
- import { AppComponent } from "./app.component";
- import { AppRoutingModule } from "./app-routing.module";
- import { DxDrawerModule, DxToolbarModule, DxListModule } from "devextreme-angular";
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- AppRoutingModule,
- DxDrawerModule,
- DxToolbarModule,
- DxListModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- import { NgModule } from "@angular/core";
- import { Routes, RouterModule } from "@angular/router";
- import { InboxComponent } from "./views/inbox.component";
- import { SentMailComponent } from "./views/sent-mail.component";
- import { TrashComponent } from "./views/trash.component";
- import { SpamComponent } from "./views/spam.component";
- const routes: Routes = [
- { path: '', redirectTo: '/inbox', pathMatch: 'full' },
- { path: 'inbox', component: InboxComponent },
- { path: 'sent-mail', component: SentMailComponent },
- { path: 'trash', component: TrashComponent },
- { path: 'spam', component: SpamComponent },
- ];
- @NgModule({
- imports: [RouterModule.forRoot(routes)],
- exports: [RouterModule],
- declarations: [
- InboxComponent,
- SentMailComponent,
- TrashComponent,
- SpamComponent
- ]
- })
- export class AppRoutingModule { }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-inbox',
- template: '<div>Inbox</div>'
- })
- export class InboxComponent { constructor() { } }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-sent-mail',
- template: '<div>Sent Mail</div>'
- })
- export class SentMailComponent { constructor() { } }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-spam',
- template: '<div>Spam</div>'
- })
- export class SpamComponent { constructor() { } }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-trash',
- template: '<div>Trash</div>'
- })
- export class TrashComponent { constructor() { } }
Create the Drawer
Wrap the view in the Drawer and specify a template for the Drawer's content. Inside the template, set the Drawer's width. You can use the nested widget's width option for this (see Implement Navigation), but in this tutorial, we use the width
CSS property. The Drawer's height adjusts to the view's height (specified via the height option).
In addition, you can specify the minSize option to make the Drawer partially visible in the closed state.
- <dx-drawer
- template="template"
- [height]="250"
- [minSize]="37">
- <div *dxTemplate="let data of 'template'">
- <div style="width: 150px">Drawer content</div>
- </div>
- <div>View content</div>
- </dx-drawer>
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- }
- import { BrowserModule } from "@angular/platform-browser";
- import { NgModule } from "@angular/core";
- import { AppComponent } from "./app.component";
- import { DxDrawerModule } from "devextreme-angular";
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- DxDrawerModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- ::ng-deep .dx-drawer-panel-content, ::ng-deep .dx-overlay-content {
- background-color: lightgray;
- }
- ::ng-deep #view {
- margin-left: 10px;
- margin-top: 10px;
- }
If you run the code, you should see a partially visible Drawer and a view that displays View content.
Open and Close the Drawer
Depending on the library or framework you use, call the toggle() method or bind a variable to the opened option.
In the following code, a toolbar button outside the Drawer opens and closes it:
- <dx-toolbar id="toolbar">
- <dxi-item
- widget="dxButton"
- [options]="buttonOptions"
- location="before">
- </dxi-item>
- </dx-toolbar>
- <dx-drawer ...
- [(opened)]="isOpened">
- <div>View content</div>
- </dx-drawer>
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- isOpened: Boolean = false;
- buttonOptions: any;
- constructor() {
- this.buttonOptions = {
- icon: "menu",
- onClick: () => {
- this.isOpened = !this.isOpened;
- }
- };
- }
- }
- // ...
- import { DxDrawerModule, DxToolbarModule } from "devextreme-angular";
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- // ...
- DxDrawerModule,
- DxToolbarModule,
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- /* ... */
- ::ng-deep #toolbar {
- background-color: rgba(191, 191, 191, .15);
- padding: 5px 10px;
- }
- ::ng-deep .dx-toolbar-button .dx-button {
- background-color: rgba(191, 191, 191, -0.15);
- border: none;
- }
- ::ng-deep .dx-toolbar-button > .dx-toolbar-item-content {
- margin-left: -7px;
- }
Implement Navigation
The Drawer is designed to contain navigation items. If they should nest other items, use the TreeView widget to implement navigation. Otherwise, use the List.
These widgets provide the onSelectionChanged function in which we can implement the navigation logic. However, this function is executed only if you enable selection. In the TreeView, set the selectionMode to "single" and assign true to selectByClick; in the List, set the selectionMode to "single".
In this tutorial, we use the List:
- <dx-drawer ... >
- <div *dxTemplate="let data of 'template'">
- <dx-list
- [items]="navigation"
- [width]="200"
- selectionMode="single"
- (onSelectionChanged)="loadView($event)">
- </dx-list>
- </div>
- <div id="view"><router-outlet></router-outlet></div>
- </dx-drawer>
- import { Component } from "@angular/core";
- import { Router } from "@angular/router";
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css'],
- providers: []
- })
- export class AppComponent {
- navigation: any;
- isOpened: Boolean = false;
- constructor(private router: Router) {
- this.navigation = [
- { id: 1, text: "Inbox", icon: "message", filePath: "inbox" },
- { id: 2, text: "Sent Mail", icon: "check", filePath: "sent-mail" },
- { id: 3, text: "Trash", icon: "trash", filePath: "trash" },
- { id: 4, text: "Spam", icon: "mention", filePath: "spam" }
- ];
- }
- loadView(e) {
- this.router.navigate([e.addedItems[0].filePath])
- }
- }
- /* ... */
- ::ng-deep .dx-list-item-icon {
- margin-right: 10px;
- }
- import { NgModule } from "@angular/core";
- import { Routes, RouterModule } from "@angular/router";
- import { InboxComponent } from "./views/inbox.component";
- import { SentMailComponent } from "./views/sent-mail.component";
- import { TrashComponent } from "./views/trash.component";
- import { SpamComponent } from "./views/spam.component";
- const routes: Routes = [
- { path: '', redirectTo: '/inbox', pathMatch: 'full' },
- { path: 'inbox', component: InboxComponent },
- { path: 'sent-mail', component: SentMailComponent },
- { path: 'trash', component: TrashComponent },
- { path: 'spam', component: SpamComponent },
- ];
- @NgModule({
- imports: [RouterModule.forRoot(routes)],
- exports: [RouterModule],
- declarations: [
- InboxComponent,
- SentMailComponent,
- TrashComponent,
- SpamComponent
- ]
- })
- export class AppRoutingModule { }
- import { AppRoutingModule } from "./app-routing.module";
- import { DxDrawerModule, DxToolbarModule, DxListModule } from "devextreme-angular";
- // ...
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- // ...
- AppRoutingModule,
- DxDrawerModule,
- DxToolbarModule,
- DxListModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-inbox',
- template: '<div>Inbox</div>'
- })
- export class InboxComponent { constructor() { } }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-sent-mail',
- template: '<div>Sent Mail</div>'
- })
- export class SentMailComponent { constructor() { } }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-spam',
- template: '<div>Spam</div>'
- })
- export class SpamComponent { constructor() { } }
- import { Component } from "@angular/core";
- @Component({
- selector: 'app-trash',
- template: '<div>Trash</div>'
- })
- export class TrashComponent { constructor() { } }
Run the code, open the Drawer, and click its items to change the views.
Configure the Reveal Behavior
When you open the Drawer, it can slide in or expand from the closed position. Use the revealMode option to specify this behavior.
- <dx-drawer ...
- revealMode="expand">
- </dx-drawer>
Run the code and open the Drawer. You should see that the widget gets wider, but its content stays in place, creating an impression that the Drawer expands.
Configure Interaction with the View
When the Drawer opens, it can overlap, shrink, or partially displace the view, depending on the openedStateMode option:
- <dx-drawer ...
- openedStateMode="overlap">
- </dx-drawer>
Run the code, open the Drawer and you should see that it overlaps the view's text.
Change the Position
You can use the position option to anchor the Drawer to any side of the view. In this tutorial, the Drawer is in its default position (left).
You have configured basic Drawer features. For more information about this widget, explore the following resources:
If you have technical questions, please create a support ticket in the DevExpress Support Center.