React Drawer - Getting Started
The Drawer is a dismissible or permanently visible panel used for navigation in responsive web application layouts.
DevExtreme supplies application templates for Angular, Vue, and React. They implement a responsive layout that uses the Drawer. You can use these templates instead of following the tutorial. Refer to the documentation on GitHub for more information:
If the templates are unsuitable or you use jQuery, 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 sections below for details on each configuration step. You can also find the full code in the following GitHub repository: getting-started-with-drawer.
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 UI component's width property 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 property).
In addition, you can specify the minSize property to make the Drawer partially visible in the closed state.
- import React from "react";
- import "devextreme/dist/css/dx.common.css";
- import "devextreme/dist/css/dx.light.css";
- import "./NavigationDrawer.css";
- import { Drawer } from "devextreme-react/drawer";
- class NavigationDrawer extends React.Component {
- constructor(props) {
- super(props);
- }
- render() {
- return (
- <React.Fragment>
- <Drawer
- minSize={37}
- height={250}
- render={ () => <div style="width: 150px">Drawer content</div> } >
- <div>View content</div>
- </Drawer>
- </React.Fragment>
- );
- }
- }
- export default NavigationDrawer;
- .dx-overlay-content {
- background-color: lightgray;
- }
- #view {
- margin-left: 10px;
- margin-top: 10px;
- }
- import React, { Component } from "react";
- import NavigationDrawer from "./components/NavigationDrawer";
- class App extends Component {
- render() {
- return (
- <div className="App">
- <NavigationDrawer />
- </div>
- );
- }
- }
- export default App;
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 the opened property to a component property.
In the following code, a toolbar button outside the Drawer opens and closes it:
- // ...
- import { Drawer } from "devextreme-react/drawer";
- import { Toolbar, Item } from "devextreme-react/toolbar";
- class NavigationDrawer extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- isDrawerOpen: false
- };
- this.buttonOptions = {
- icon: "menu",
- onClick: () => {
- this.setState({ isDrawerOpen: !this.state.isDrawerOpen })
- }
- };
- }
- render() {
- return (
- <React.Fragment>
- <Toolbar id="toolbar">
- <Item
- widget="dxButton"
- options={this.buttonOptions}
- location="before" />
- </Toolbar>
- <Drawer ...
- opened={this.state.isDrawerOpen} >
- <div>View content</div>
- </Drawer>
- </React.Fragment>
- );
- }
- }
- export default NavigationDrawer;
- /* ... */
- #toolbar {
- background-color: rgba(191, 191, 191, .15);
- padding: 5px 10px;
- }
- .dx-toolbar-button .dx-button {
- background-color: rgba(191, 191, 191, -0.15);
- border: none;
- }
- .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 UI component to implement navigation. Otherwise, use the List, as done in this tutorial.
Each list item should navigate to a different view. To implement this, follow the steps below:
Install React Router
- npm install react-router-dom --save
Configure routing
Wrap the entire
App
component in aBrowserRouter
and add aRoute
that renders theNavigationDrawer
component. Define otherRoutes
within theDrawer
markup in theNavigationDrawer
component.Define the itemRender function
Specify the elements that the function should render and wrap them in a
Link
. In the code below, therenderItem
function renders an icon and text.Enable item selection
Set the selectionMode to "single". If you use the TreeView, you should also set the selectByClick property to true. In the onSelectionChanged event handler, close the Drawer.
- // ...
- import { BrowserRouter, Route } from 'react-router-dom'
- class App extends Component {
- render() {
- return (
- <BrowserRouter>
- <div className="App">
- <Route component={NavigationDrawer} />
- </div>
- </BrowserRouter>
- );
- }
- }
- export default App;
- // ...
- import NavigationList from "./NavigationList";
- import { Switch, Route } from "react-router-dom";
- import Inbox from "./views/Inbox";
- import Trash from "./views/Trash";
- import SentMail from "./views/SentMail";
- import Spam from "./views/Spam";
- class NavigationDrawer extends React.Component {
- // ...
- renderList = () => {
- const stateHandler = (newState) => this.setState(newState);
- return (
- <NavigationList stateHandler={stateHandler} />
- );
- }
- render() {
- return (
- <React.Fragment>
- { /* ... */ }
- <Drawer ...
- render={this.renderList}>
- <div id="view">
- <Switch>
- <Route exact path="/" component={Inbox} />
- <Route exact path="/inbox" component={Inbox} />
- <Route exact path="/sent-mail" component={SentMail} />
- <Route exact path="/spam" component={Spam} />
- <Route exact path="/trash" component={Trash} />
- </Switch>
- </div>
- </Drawer>
- </React.Fragment>
- );
- }
- }
- export default NavigationDrawer;
- import React from "react";
- import List from "devextreme-react/list";
- import { Link } from "react-router-dom";
- const navigation = [
- { id: 1, text: "Inbox", icon: "message", path: "inbox" },
- { id: 2, text: "Sent Mail", icon: "check", path: "sent-mail" },
- { id: 3, text: "Trash", icon: "trash", path: "trash" },
- { id: 4, text: "Spam", icon: "mention", path: "spam" }
- ];
- class NavigationList extends React.PureComponent {
- closeDrawer = () => {
- this.props.stateHandler({ isDrawerOpen: false });
- }
- renderItem = (data) => {
- return (
- <div>
- <Link to={'/' + data.path}>
- <div>
- <div className="dx-list-item-icon-container">
- <i className={`dx-icon dx-list-item-icon dx-icon-${data.icon}`}></i>
- </div>
- <span>{data.text}</span>
- </div>
- </Link>
- </div>
- );
- }
- render() {
- return (
- <React.Fragment>
- <List
- items={navigation}
- width={200}
- selectionMode="single"
- onSelectionChanged={this.closeDrawer}
- itemRender={this.renderItem}
- />
- </React.Fragment>
- );
- }
- }
- export default NavigationList;
- /* ... */
- .dx-list-item-icon {
- margin-right: 10px;
- }
- import React from "react";
- class Inbox extends React.Component {
- render() {
- return (
- <div>Inbox</div>
- );
- }
- }
- export default Inbox;
- import React from "react";
- class SentMail extends React.Component {
- render() {
- return (
- <div>Sent Mail</div>
- );
- }
- }
- export default SentMail;
- import React from "react";
- class Spam extends React.Component {
- render() {
- return (
- <div>Spam</div>
- );
- }
- }
- export default Spam;
- import React from "react";
- class Trash extends React.Component {
- render() {
- return (
- <div>Trash</div>
- );
- }
- }
- export default Trash;
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 property to specify this behavior.
- // ...
- class NavigationDrawer extends React.Component {
- // ...
- render() {
- return (
- <React.Fragment>
- <Drawer ...
- revealMode="expand" >
- </Drawer>
- </React.Fragment>
- );
- }
- }
- export default NavigationDrawer;
Run the code and open the Drawer. You should see that the UI component 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 property:
- // ...
- class NavigationDrawer extends React.Component {
- // ...
- render() {
- return (
- <React.Fragment>
- <Drawer ...
- openedStateMode="overlap" >
- </Drawer>
- </React.Fragment>
- );
- }
- }
- export default NavigationDrawer;
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 property 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 UI component, explore the following resources:
If you have technical questions, please create a support ticket in the DevExpress Support Center.