DevExtreme jQuery/JS - Getting Started with Navigation Drawer
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 subtopics for details on every configuration step. You can also see the full code below:
- $(function() {
- $("#view").load( "./inbox.html" );
- $("#toolbar").dxToolbar({
- items: [{
- widget: "dxButton",
- location: "before",
- options: {
- icon: "menu",
- onClick: function() {
- drawer.toggle();
- }
- }
- }]
- });
- const drawer = $("#drawer").dxDrawer({
- minSize: 37,
- height: 250,
- revealMode: "expand",
- openedStateMode: "overlap",
- template: function() {
- const $list = $("<div/>").dxList({
- items: [
- { 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" }
- ],
- width: 200,
- height: 200,
- selectionMode: "single",
- onSelectionChanged: function(e) {
- $("#view").load( e.addedItems[0].filePath + ".html" );
- drawer.hide();
- }
- });
- return $list;
- }
- }).dxDrawer("instance");
- })
- <html>
- <head>
- <!-- ... -->
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
- <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/19.2.15/css/dx.common.css">
- <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/19.2.15/css/dx.light.css">
- <link rel="stylesheet" href="index.css">
- <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/19.2.15/js/dx.all.js"></script>
- <script type="text/javascript" src="index.js"></script>
- </head>
- <body>
- <div id="toolbar"></div>
- <div id="drawer">
- <div id="view"></div>
- </div>
- </body>
- </html>
- .dx-overlay-content {
- background-color: lightgray;
- }
- #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;
- }
- .dx-list-item-icon {
- margin-right: 10px;
- }
- #view {
- margin-left: 10px;
- margin-top: 10px;
- }
- <div>Inbox</div>
- <div>Sent Mail</div>
- <div>Trash</div>
- <div>Spam</div>
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.
- <html>
- <head>
- <!-- ... -->
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
- <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/19.2.15/css/dx.common.css">
- <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/19.2.15/css/dx.light.css">
- <link rel="stylesheet" href="index.css">
- <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/19.2.15/js/dx.all.js"></script>
- <script type="text/javascript" src="index.js"></script>
- </head>
- <body>
- <div id="drawer">
- <div id="view">View content</div>
- </div>
- </body>
- </html>
- $(function() {
- $("#drawer").dxDrawer({
- template: function(e) {
- return $("<div style='width: 150px'>Drawer content</div>");
- },
- height: 250,
- minSize: 37
- });
- });
- .dx-overlay-content {
- background-color: lightgray;
- }
- #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 the opened option to a component property.
In the following code, a toolbar button outside the Drawer opens and closes it:
- $(function() {
- const drawer = $("#drawer").dxDrawer({
- // ...
- }).dxDrawer("instance");
- $("#toolbar").dxToolbar({
- items: [{
- widget: "dxButton",
- location: "before",
- options: {
- icon: "menu",
- onClick: function() {
- drawer.toggle();
- }
- }
- }]
- });
- })
- <div id="toolbar"></div>
- <div id="drawer">
- <div id="view">View content</div>
- </div>
- /* ... */
- #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 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:
- $(function() {
- // Loads the initial page
- $("#view" ).load( "./inbox.html" );
- const drawer = $("#drawer").dxDrawer({
- // ...
- template: function(e) {
- const $list = $("<div/>").dxList({
- items: [
- { 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" }
- ],
- width: 200,
- selectionMode: "single",
- onSelectionChanged: function(e) {
- $("#view").load( e.addedItems[0].filePath + ".html" );
- drawer.hide();
- }
- });
- return $list;
- }
- }).dxDrawer("instance");
- })
- <div id="toolbar"></div>
- <div id="drawer">
- <div id="view"></div>
- </div>
- /* ... */
- .dx-list-item-icon {
- margin-right: 10px;
- }
- <div>Inbox</div>
- <div>Sent Mail</div>
- <div>Trash</div>
- <div>Spam</div>
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.
- $(function() {
- const drawer = $("#drawer").dxDrawer({
- // ...
- revealMode: "expand"
- }).dxDrawer("instance");
- })
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:
- $(function() {
- const drawer = $("#drawer").dxDrawer({
- // ...
- openedStateMode: "overlap"
- }).dxDrawer("instance");
- })
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.