Angular 2 Material 2: Nifty Collapsible Sidebar

I am currently working on building a sidebar navigation using Angular 2 and Material 2,

When in the Open State, it appears as shown below,

https://i.sstatic.net/FFGmz.png

However, in the Closed state, the entire sidebar hides. I would like only the menu text to be hidden while keeping the icon visible.

Take a look at my code snippet below,

<md-toolbar color="primary">
  <button md-icon-button (click)="start.toggle()">
        <md-icon>menu</md-icon>
    </button>Test Project
  <button md-icon-button (click)="logout()">
      <md-icon>exit_to_app</md-icon>
    </button>
</md-toolbar>

<md-sidenav-layout class="left-navigation">
  <md-sidenav #start mode="side" opened="true">
    <md-nav-list>
      <a [routerLink]="view.link" md-list-item *ngFor="let view of views" (click)="sidenav.close()">
        <md-icon md-list-icon>{{view.icon}}</md-icon>
        <span md-line>{{view.name}}</span>
      </a>
    </md-nav-list>
  </md-sidenav>
  <div class="demo-sidenav-content">
    <router-outlet></router-outlet>
  </div>
</md-sidenav-layout>

Answer №1

I encountered a similar task and here is how I successfully solved it:

md-sidenav-container {
  min-width: 60px;
}

md-sidenav {
  transform: translate3d(0, 0, 0);
}

md-sidenav.mat-sidenav-closed {
  visibility: visible;
}

UPDATE: The initial solution presented above had an issue where the navbar never reached the states "opened" and "closed" completely if triggered at least once. Instead, it had intermediate stages of "opening" and "closing".

To ensure functionality and complete animations, I implemented a more unconventional approach:

md-sidenav-container {
  background-color: transparent;
  min-width: 60px;
}

md-sidenav {
  width: 230px;
  background-color: transparent;
}

.mat-sidenav-closed md-nav-list,
.mat-sidenav-closing md-nav-list {
  margin-left: 230px;
  width: 230px;
  visibility: visible;
 }

:host >>> .mat-sidenav-focus-trap>.cdk-focus-trap-content {
  overflow-y: visible;
}

The main distinction lies in the animation behavior, which, in my opinion, enhances the overall user experience :)

Answer №2

Check out the plunker demo I've shared in this GitHub issue: here

While it's similar to your requirements, the icons are positioned differently on the sidenav. You may need additional animations to maintain that layout.

Nevertheless, I believe this example will offer insight on how to collapse the sidenav while keeping a portion of it visible.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Using MatCheckbox's preventDefault() method to access the checked value of the

Is there a way to customize a Material checkbox to prevent it from being checked or unchecked by default (e.g., by calling preventDefault() on the event) and still retrieve the checked value from the event? It appears that achieving both conditions simult ...

Can someone explain how to showcase a collection attribute in Angular?

I am working with a collection of objects called "ELEMENT_DATA". Each object in the collection has an attribute named "Activite", which is also a collection. My goal is to display this attribute in a specific way. Below is my app.component.ts: export cla ...

Implement a click event for the X-Axis label in Angular 2 Highcharts

I'm currently facing a challenge with hand-rolling a solution that involves adding a click listener to an X-Axis label in a column chart using the HighCharts API within an Angular 2+ application. Here is what I have gathered so far: I am utilizing ...

Receiving the error notification from a 400 Bad Request

I'm having trouble showing the errors returned from an API on my Sign up form when a user submits invalid data. You can check out the error response in the console here: console ss This is my approach in RegisterComponent.ts: onSubmit() { this.u ...

Hostlistener is unresponsive to inputs from arrow keys

Within my Angular 2 component, I have implemented a key event listener to capture keystrokes: @HostListener('document:keypress', ['$event']) handleKeyboardEvent(event: KeyboardEvent) { console.log(event); } Oddly enough, I am not ...

Invoke index functions within a component

I have a widget/component written in Angular 4 within the index.html file. Before and after this angular app, there are various HTML elements due to the nature of it being an additional component for the website. The head section of the index file include ...

Obtaining the component instance ('this') from a template

Imagine we are in a situation where we need to connect a child component property to the component instance itself within a template: <child-component parent="???"></child-component1> Is there a solution for this without having to create a sp ...

"Error: Unfinished string literal encountered" occurring in a TypeScript app.component.ts file in NativeScript

I've been trying to learn NativeScript through a tutorial, but I keep encountering errors. Here is an excerpt from my app.component.ts file: import { Component } from '@angular/core'; @Component ({ selector: 'my-app', temp ...

Troubleshooting Angular2: Issues with ngModel functionality within an ngIf directive

Whenever I attempt to reference an NgModel directive using a template reference variable within an *ngIf statement, I encounter a "Cannot read property 'valid' of undefined" error. Consider the following form as an example: <form (ngSubmit)= ...

Encountering an issue: "Failed HTTP response while trying to access https://localhost:44328/api/AllItProjectsLists/Index: 0 Error Message Unknown"

I am completely new to working with Angular. I have been working on a small application that is supposed to display a list of projects retrieved from a database on the Angular side. To achieve this, I am making a call from Angular to .NET Core 2.2. The dat ...

Angular reactive form validation ensures user inputs are accurate and complete before submitting

Within my reactive Angular form, I have a specific field that requires validation. In my service class, I am currently utilizing the default Validators.required function provided by Angular in the following code snippet: Countrycode: new FormControl(&apos ...

Input value not being displayed in one-way binding

I have a challenge in binding the input value (within a foreach loop) in the HTML section of my component to a function: <input [ngModel]="getStepParameterValue(parameter, testCaseStep)" required /> ... // Retrieving the previously saved v ...

Issue connecting to Oracle database: Unable to access properties of undefined (attempting to read '_getConnection')

Encountering an issue with node oracle connection. It was successfully connected before in the same application, but now it is not working after updating the node version. The connection string seems fine as the same connection is working in another appli ...

Angular and Express are not communicating properly when attempting to make a GET request

My Angular application is supposed to make an HTTP-Get Request, and the Express server (which also hosts the Angular app) should send a JSON object to the Angular app. However, for some reason, it's not working and I'm unsure why. The first conso ...

Rendering Backend Data in Angular Application on Page Refresh with Apache Server Integration

Attempting to deploy an Angular Flask Application demo with Apache. The project directory is located at /var/www/backend/basicapp/. Within this directory, you will find: app.py from flask import Flask, jsonify, render_template from flask_cors import CORS, ...

After unsubscribing from RxJS timer, it starts again

Trying out a simple reflex-testing game here. The player has to click on the red rectangle when it turns green, and their score is displayed. However, the issue is that the timer restarts after clicking on the rectangle even though I tried using 'unsu ...

The quality of Three.js models is subpar

Lately, I've been facing an issue with the quality of 3D models in my Angular project that uses three.js. Despite my efforts, the models appear to be of poor quality when loaded. Surprisingly, when I load the same models on a platform like , they look ...

Error message in Spring Boot: Unable to display index page, error page not found

Currently, I am working on a web application that utilizes spring boot for the backend and angular2 for the frontend. However, when accessing a non-root URL, spring boot returns a whitelabel page since the servlet container is unaware of the angular routin ...

Obtaining the value of a select option in Angular 2 from a button placed outside of a

Take a look at this snippet of code: (click)="deleteDescriptor(descriptor.DescriptorId, #fc+i) I am dynamically creating all of the selects. Therefore, my goal is to retrieve the value of the select when users click on the delete button located right next ...

What is the best way to link assets within an Angular custom element (Web Components)?

After successfully creating a web component and referencing an image from my asset folder, everything was running smoothly on my local environment. However, when I published my custom element to Firebase hosting, I encountered some issues. When trying to ...