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.
Please add (click)="sidenav. hide()" method to the links that should close the 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.
MatDrawer. This component corresponds to a drawer that can be opened on the drawer container.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With