JavaScript/jQuery Drawer - Getting Started
The Drawer is a dismissible or permanently visible panel used for navigation in responsive web application layouts.
This tutorial shows how to 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 GitHub repository.
Create the Drawer
To create the Drawer component, you need to specify its content template. The image below shows the difference between Drawer and view content:
Wrap the view in the Drawer in HTML markup. Then, specify a template for the Drawer's content.
It is important to specify the Drawer's height and width. Alternatively, if you implement a UI component within the Drawer component's content, you can specify the component's width property and set the view's height. The Drawer's height automatically adjusts to the view's height in this case. Refer to the following topic for details: Implement Navigation.
This tutorial uses the width
CSS property of the Drawer's nested HTML element and specifies the component's height property.
You can also specify the minSize property to make the Drawer partially visible when it is closed.
- <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/24.2.5/css/dx.light.css">
- <link rel="stylesheet" href="index.css">
- <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/24.2.5/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 can see a partially visible Drawer and a view that displays View content.
Open and Close the Drawer
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:
- $(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 UI component to implement navigation. Otherwise, use the List, as this tutorial does.
Each list item should navigate to a different view. To implement this, follow the steps below:
Enable item selection
Set the selectionMode to "single". If you use the TreeView, also set the selectByClick property to true.
Navigate to a view when selection is changed
In the onSelectionChanged event handler, load the new view and hide the Drawer.
- $(function() {
- // Loads the initial page
- $("#view" ).load( "./pages/inbox.html" );
- const drawer = $("#drawer").dxDrawer({
- // ...
- template: function(e) {
- const $list = $("<div/>").dxList({
- items: [
- { 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" }
- ],
- width: 200,
- selectionMode: "single",
- onSelectionChanged: function(e) {
- $("#view").load( "./pages/" + e.addedItems[0].path + ".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 property 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 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:
- $(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 property to anchor the Drawer to any side of the view. In this tutorial, the Drawer is in its default position (left).
For more information about the Drawer, explore the following resources:
If you have technical questions, please create a support ticket in the DevExpress Support Center.