Jumbo 5.0
Search…
Change collapse behavior of drawer
Auto hide the drawer on click of a nav item
The current behavior of the drawer is that when you click outside the drawer then only it collapses. But sometimes you might need to collapse the drawer soon you click the nav item inside of that drawer.
Following the below steps you can easily achieve this:
In the NavMenuItem component, use the setSidebarOpen function from LayoutContext with following line of code:
1
const { setSidebarOpen } = useContext(LayoutContext);
Copied!
Now, in the return statement of NavMenuItem component, On the component, you can put the onClick prop and bind that with a function which sets the sidebarOpen context value to false as shown in the following line of code:
1
return (
2
<List component="div" className={clsx(classes.navMenuItem, 'Cmt-nav-menu-item')}
3
onClick={() => setSidebarOpen(false)}>
4
<NavLink className={clsx(classes.navMenuLink, 'Cmt-nav-menu-link')} to={link}>
5
{/* Display an icon if any */}
6
{renderIcon()}
7
<span className={clsx(classes.navText, 'Cmt-nav-text')}>{name}</span>
8
</NavLink>
9
</List>
10
);
Copied!
Thats all you need to do to make drawer collapse on click of a nav item.
Last modified 1mo ago
Copy link