Skip to content

Commit

Permalink
Implemented feature to highlight active route nav item.
Browse files Browse the repository at this point in the history
  • Loading branch information
tony21921 committed Dec 30, 2024
1 parent 3337ae5 commit ec5c281
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
{
"glob": "**/*",
"input": "projects/dev-app/public"
},
{
"glob": "logo.svg",
"input": "projects/nested-nav-list",
"output": "."
}
],
"stylePreprocessorOptions": {
Expand Down
14 changes: 10 additions & 4 deletions projects/dev-app/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<h1>Angular Material Nested Navigation List</h1>
<mat-slide-toggle [(ngModel)]="keepSvgIconColor">Keep Svg Icon Color: {{keepSvgIconColor}}</mat-slide-toggle>
<ngx-nested-nav-list [navData]="navData" [keepSvgIconColor]="keepSvgIconColor"></ngx-nested-nav-list>
<mat-slide-toggle [(ngModel)]="keepSvgIconColor"
>Keep Svg Icon Color: {{ keepSvgIconColor }}</mat-slide-toggle
>
<ngx-nested-nav-list
[navData]="navData"
[keepSvgIconColor]="keepSvgIconColor"
[highlightActiveItem]="true"
></ngx-nested-nav-list>
<div>
<router-outlet></router-outlet>
</div>
<router-outlet></router-outlet>
</div>
2 changes: 1 addition & 1 deletion projects/dev-app/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>DevApp</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link rel="icon" type="image/svg+xml" href="logo.svg" />
<link
href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<mat-list-item
(click)="onClickNavItem(navItem, $event)"
[ngStyle]="{ 'padding-left.px': level * nestedIndent }"
[class.active]="highlightActiveItem && isActiveItem(navItem)"
>
@switch (navItem.iconType) {
@case ('svg') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
[matlistitemicon] {
display: flex;
}

mat-list-item.active {
background-color: var(--mat-selected-item-background, rgba(0, 0, 0, 0.08));
color: var(--mat-selected-item-color, rgba(0, 0, 0, 0.87));
}
}
7 changes: 7 additions & 0 deletions projects/nested-nav-list/src/lib/nested-nav-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class NestedNavListComponent implements OnChanges {
@Input() navData: NavData[] = [];
@Input() nestedIndent: number = 16;
@Input() keepSvgIconColor: boolean = false;
@Input() highlightActiveItem: boolean = false;
@Input() isActiveItem: (item: NavData) => boolean = this.defaultIsActiveItem;

transformedNavData: NavData[] = [];

Expand Down Expand Up @@ -88,4 +90,9 @@ export class NestedNavListComponent implements OnChanges {
item.expanded = !item.expanded;
}
}

defaultIsActiveItem(item: NavData): boolean {
const currentUrl = this.router.url.split('?')[0]; // Exclude query parameters
return item.route !== undefined && currentUrl.endsWith(item.route || '');
}
}

0 comments on commit ec5c281

Please sign in to comment.