Sidenav
Materialize is a modern responsive CSS framework based on Material Design by Google.
This is a slide out menu. You can add a dropdown to your sidebar by using our collapsible component. If you want to see a demo, our sidebar will use this on smaller screens. To use this in conjunction with a fullscreen navigation, you have to use two copies of the same UL.
Please note that the sidenav HTML should not be contained within the navbar HTML.
Toggle Sidenav
menu
Initialization
document.addEventListener('DOMContentLoaded', function() {
const elems = document.querySelectorAll('.sidenav');
const instances = M.Sidenav.init(elems, {
// specify options here
});
});
// Initialize collapsible (uncomment the lines below if you use the dropdown variation)
// const collapsibleElem = document.querySelector('.collapsible');
// const collapsibleInstance = M.Collapsible.init(collapsibleElem, {
// // specify options here
// });
Options
Name | Type | Default | Description |
---|---|---|---|
edge | String | 'left' | Side of screen on which Sidenav appears. |
draggable | Boolean | true | Allow swipe gestures to open/close Sidenav. |
dragTargetWidth | String | '10px' | Width of the area where you can start dragging. |
inDuration | Number | 250 | Length in ms of enter transition. |
outDuration | Number | 200 | Length in ms of exit transition. |
onOpenStart | Function | null | Function called when sidenav starts entering. |
onOpenEnd | Function | null | Function called when sidenav finishes entering. |
onCloseStart | Function | null | Function called when sidenav starts exiting. |
onCloseEnd | Function | null | Function called when sidenav finishes exiting. |
preventScrolling | Boolean | true | Prevent page from scrolling while sidenav is open. |
Methods
All the methods are called on the plugin instance. You can get the plugin instance like this:
const instance = M.Sidenav.getInstance(elem);
.open();
Opens Sidenav.
instance.open();
class="method-header">
.close();
Closes Sidenav.
instance.close();
class="method-header">
.destroy();
Destroy plugin instance and teardown
instance.destroy();
Properties
Name | Type | Description |
---|---|---|
el | Element | The DOM element the plugin was initialized with. |
options | Object | The options the instance was initialized with. |
isOpen | Boolean | Describes open/close state of Sidenav. |
isFixed | Boolean | Describes if sidenav is fixed. |
isDragged | Boolean | Describes if Sidenav is being dragged. |
Close Trigger
Add the class
.sidenav-close
to an element inside the sidenav and any click event on that element will cause the sidenav to close. This is
useful in Single Page Apps where the page does not refresh on link clicks.
menu
Variations
Here are some useful variations and additional elements you can add to your sidebar.
Dropdown HTML Structure
Add collapsible menus to your sidebar.
menu
Fullscreen HTML Structure
If you want the menu to be accessible on all screensizes you just have to add a simple helper class
show-on-large
to the
.sidenav-trigger
.
menu
Fixed HTML Structure
Add the class
sidenav-fixed
to have the sidenav be fixed and open on large screens and hides to the regular functionality on smaller screens.
Our sidenav on the left is an example of this.
menu
If you are planning on using this you will have to offset your content by the width of the side menu. Place the padding on where the offset content will be, which in our case is in header, main and footer.
header, main, footer {
padding-left: 300px;
}
@media only screen and (max-width : 992px) {
header, main, footer {
padding-left: 0;
}
}