Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set color of active item in mat-nav-list in angular 6

I am new to angular 6 ,here I have a mat-toolbar with mat-sidenav .Everything is working well but I want to set a color for the active item in side nav menu.

currently I have BLACK background I want to set a color when I select the item in the mat-nav-list and also I tried to set mat-divider between each item that also not working .

app.component.html

<mat-sidenav-container class="sidenav-container">

  <mat-sidenav #drawer class="sidenav" fixedInViewport="true" [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
    [mode]="(isHandset$ | async) ? 'over' : 'side'" [opened]="!(isHandset$ | async)" style="background:black"> //current background is black
    <mat-toolbar class="menuBar">Menus</mat-toolbar>
    <mat-nav-list>
      <a class="menuTextColor" mat-list-item href="#">Link 1</a> // want to change the color of the selected item.
      <a class="menuTextColor" mat-list-item href="#">Link 2</a> // want to set divider between each item
      <a class="menuTextColor" mat-list-item href="#">Link 3</a>
    </mat-nav-list>
  </mat-sidenav>

  <mat-sidenav-content>
    <mat-toolbar class="toolbar">
      <button class="menuTextColor" type="button" aria-label="Toggle sidenav" mat-icon-button (click)="drawer.toggle()" *ngIf="isHandset$ | async">
        <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
      </button>
      <span class="toolbarHeading">Demo App</span>
    </mat-toolbar>
    <!-- Add Content Here -->
  </mat-sidenav-content>

</mat-sidenav-container>

Can anyone help me to fix this .

like image 937
Zhu Avatar asked Oct 01 '18 14:10

Zhu


1 Answers

This can be done if you're using Angular Router with the routerLinkActive attribute.

From the documentation for the RouterLinkActive directive:

Lets you add a CSS class to an element when the link's route becomes active.

Description

This directive lets you add a CSS class to an element when the link's route becomes active.

Consider the following example:

<a routerLink="/user/bob" routerLinkActive="active-link">Bob</a>

When the URL is either /user or /user/bob, the active-link class will be added to the <a> tag. If the URL changes, the class will be removed.

The code below showcases a typical use case:

<mat-nav-list>
  <a mat-list-item routerLink="/home" routerLinkActive="active-list-item">Home</a>
  <a mat-list-item routerLink="/settings" routerLinkActive="active-list-item">Settings</a>
  <a mat-list-item routerLink="/about" routerLinkActive="active-list-item">About</a>
</mat-nav-list>
.active-list-item {
  color: #3F51B5 !important; /* Note: You could also use a custom theme */
}

A couple of notes:

  • You can change active-list-item to whatever class name you would like to be applied.
  • The !important declaration in the second code snippet is used as the list item styles take precedence over your custom styles.

Here's a Stackblitz.

like image 170
Edric Avatar answered Sep 28 '22 04:09

Edric