Angular Material SideNavbar

Note : By default SideNavbar is hidden

material.module.ts

import { NgModule } from '@angular/core';
import { MatButtonModule } from '@angular/material';
import { MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import { MatBadgeModule } from '@angular/material/badge';
import { MatButtonToggleModule} from '@angular/material/button-toggle';
import { MatIconModule} from '@angular/material/icon';
import { MatToolbarModule} from '@angular/material/toolbar';
import {MatSidenavModule} from '@angular/material/sidenav';


const MaterialComponent = [
  MatButtonModule,
  MatButtonToggleModule,
  MatIconModule,
  MatBadgeModule,
  MatProgressSpinnerModule,
  MatToolbarModule,
  MatSidenavModule
];

@NgModule({
  imports: [
    MaterialComponent,
  ],
  exports: [
    MaterialComponent
  ]
})
export class MaterialModule { }

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from './material/material.module';
import { FormsModule } from '@angular/forms';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MaterialModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  opened = false;
  title = 'GettingStarted';
  log(state){
    console.log(state);
  }
}

app.component.html

<mat-sidenav-container>
  <!-- default is mode over -->
  <!-- mode push pushes over the content with an overlay-->
  <!-- mode side pushes over the content without an overlay and does not auto closes -->
  <mat-sidenav #sidenav (opened)="log('Opened')" (closed)="log('closed')" mode="side" [(opened)]="opened">Sidenav</mat-sidenav>
  <mat-sidenav-content>Main
    <button (click)="opened=!opened">Toggle Opened</button>
    <button (click)="sidenav.open()">Open</button>
    <button (click)="sidenav.close()">Close</button>
    <button (click)="sidenav.toggle()">Toggle</button>
  </mat-sidenav-content>
</mat-sidenav-container>

app.component.css

mat-sidenav-container{
  height: 100%;
}
mat-sidenav, mat-sidenav-content{
  padding: 16px;
}
mat-sidenav{
  background: lightcoral;
  width: 200px;
}

Output:

Leave a Comment