
Angular Material Sidebar and Submenu with Responsive Design is a powerful combination that can elevate your application's user experience.
To create a responsive design, you can use the mat-sidenav-container component, which allows you to create a sidebar that adapts to different screen sizes.
By using the mat-sidenav component with the mode attribute set to 'push', you can create a sidebar that pushes the main content area to the right when the sidebar is opened.
The mat-menu component can be used to create a submenu that appears when a menu item is clicked. This is especially useful for creating a responsive design that adapts to different screen sizes.
Suggestion: Material Design
Sidebar Setup
To set up the sidebar UI in Angular Material, you can start by importing the necessary modules, including MatToolbarModule and MatSidenavModule.
The application's root component (app.component.ts) was updated to include a mat-toolbar for the top navigation and a mat-sidenav-container that houses the sidebar and the main content area.
You can make the sidebar collapsible by utilizing Angular Material's opened and mode properties of mat-sidenav. This involves adding a MatIconButton with a hamburger icon to toggle the sidebar's state and using Angular signals to handle the state reactivity.
The collapsed state of the signal is also passed in as an input to the custom sidenav component. This allows for a dynamic and responsive sidebar that can be easily toggled.
To conditionally adjust the width of the sidenav and the content, you can create a computed called sidenavWidth. This will help you achieve a responsive layout that adapts to different screen sizes.
Here are the key components involved in setting up the sidebar UI:
- mat-toolbar for the top navigation
- mat-sidenav-container for the sidebar and main content area
- mat-sidenav for the sidebar menu
- mat-sidenav-content for the main content
- MatIconButton for toggling the sidebar's state
Navigation and Menu
Implementing a sidebar navigation is a core functionality of a sidebar, and Angular's Router makes it seamless to set up routing for the application. Each menu item corresponds to a different component or page within the application, and Angular's routerLink is used to navigate between components.
A fresh viewpoint: Building a Realtime Chat Application with Angular and Firebase
To visually indicate the current active route, the RouterLinkActive directive is used, changing the appearance of the active menu item to match YouTube Studio's sidebar. The menu items' left border color is conditionally changed using a style, and the icon style is changed using the fontSet property of the material icon component.
Responsive Multi-Level Angular Material Menu is achieved by using the BreakpointObserver to detect the screen size. If the screen width is above 768px, the menu is shown, and if it's below 768px, the menu is hidden.
The menu is shown or hidden by setting the [opened] property of the mat-sidenav to true or false, respectively. The mode is also set to 'side' when the screen width is above 768px and 'over' when it's below 768px.
Here's a table summarizing the properties used to show or hide the menu:
To give visual indication on which menu item was clicked, a variable selectedMenuItem is used to track and change the CSS class of the menu item. This is achieved by using the ngClass directive and passing in an object that adds the 'selected-menu-item' class when the selectedMenuItem is equal to the menu item's name.
A different take: How to Add/remove Css Class Dynamically in Angular 8
Styling and Responsiveness
The sidebar in Angular Material can be styled to give it a professional look. The styles associated with the elements have colors and spacing needed for each of them.
To make the sidebar responsive, we use the material sidenav component's mode property. For large screen sizes, we want the mode to be side.
The BreakpointObserver service from the Angular CDK layout package is used to detect screen size changes. We can observe breakpoints and switch between modes when the screen size changes.
Adding a template variable for the sidenav component using the #sidenav="matSidenav" syntax makes the reference available in the template. This is similar to using the ViewChild decorator.
We can use the observe method of the BreakpointObserver to take in any number of breakpoints. If any of them matches or changes state, all subscriptions to the method are notified.
The breakpoint 800px is used to switch to the over mode for the sidebar and close it. If it doesn't match, the mode remains side.
Discover more: Switch Case in Angular Html
Components and Directives
The sidebar-nav component is a simple combination of nav-list and nav-list-item, but it can be improved by adding auto-expand capabilities.
We want to forward the emitted value from the routerLink directive to another Output property on the nav-list component class, which will be used to expand the panel.
The expand-on-active-link directive injects an instance of MatExpansionPanel in its constructor and uses its open method to expand the panel whenever one of the projected nav-list-item components emits true from its isActive Output.
ContentChildren queries are set right before the ngAfterContentInit lifecycle hook, which is why navListItemComponents are accessed in this hook.
The from function takes an array of nav-list-item components and sends each component to the mergeMap operator, which picks up the isActive Output property of each component.
The filter operator is used to make sure that only true events will continue down the stream.
A setTimeout is used because there's a bug in Material that prevents mat-expansion-panel from expanding if it is placed inside a mat-drawer.
Additional reading: Expand Collapse Panel in Angular without Material Ui
Adding Finishing Touches
Now that you have your sidebar and submenu set up, it's time to add the finishing touches.
The sidebar can be customized to fit your needs by changing the color palette. In the example, the sidebar is set to a dark theme with a primary color of '#2196f3'.
You can also add icons to the sidebar items to make them more visually appealing. In the code snippet, an icon is added to the 'Dashboard' item using the 'mat-icon' directive.
To make the submenu work seamlessly with the sidebar, you need to import the necessary modules and configure the menu. This is done by importing the 'MatMenuModule' and 'MatIconModule' in the app module.
The submenu can be configured to open on hover or click. In the example, the submenu is set to open on hover.
See what others are reading: Webflow Mobile Nav with Submenu
Featured Images: pexels.com


