Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close Angular Material Sidenav with escape?

Tags:

My goal is to close the Angular Material Sidenav by pressing escape.

I have tried using the [autoFocus]="true" property to achieve this, but to no help.

The problem is that pressing escape has no effect - only if I first click inside the sidenav does pressing escape work.

I have forked one of the official examples to demonstrate the issue here, so you can play around with the code.

I am unsure if this is a bug with the sidenav, or just me not being able to figure out how to trap focus.

like image 314
DauleDK Avatar asked Dec 13 '19 18:12

DauleDK


People also ask

How do I turn off Sidenav on click?

Please add (click)="sidenav. hide()" method to the links that should close the sidenav.

How do I use Sidenav?

Both the main and side content should be placed inside of the <mat-sidenav-container> , content that you don't want to be affected by the sidenav, such as a header or footer, can be placed outside of the container. The side content should be wrapped in a <mat-sidenav> element.

What is mat drawer in angular?

MatDrawer. This component corresponds to a drawer that can be opened on the drawer container.


1 Answers

You can have a work around with a Hostlistener listens esc key. Like this :

@HostListener("document:keyup.esc")
  onkeyup() {
    console.log("close it");
    this.opened = false;
  }

Here is the stackblitz example

like image 86
Eldar Avatar answered Sep 30 '22 19:09

Eldar