Skip to content

Commit 2ef479e

Browse files
committed
docs(material/sidenav): improve responsive example
improvements: - use signal instead of manually detect changes - add access modifier to the class properties - add missing import of routerLink - replace deprecated add/removeListener with add/removeEventListener
1 parent 5a7da12 commit 2ef479e

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/components-examples/material/sidenav/sidenav-responsive/sidenav-responsive-example.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<div class="example-container" [class.example-is-mobile]="mobileQuery.matches" *ngIf="shouldRun">
1+
<div class="example-container" [class.example-is-mobile]="isMobile()" *ngIf="shouldRun">
22
<mat-toolbar color="primary" class="example-toolbar">
33
<button mat-icon-button (click)="snav.toggle()"><mat-icon>menu</mat-icon></button>
44
<h1 class="example-app-name">Responsive App</h1>
55
</mat-toolbar>
66

77
<mat-sidenav-container class="example-sidenav-container"
8-
[style.marginTop.px]="mobileQuery.matches ? 56 : 0">
9-
<mat-sidenav #snav [mode]="mobileQuery.matches ? 'over' : 'side'"
10-
[fixedInViewport]="mobileQuery.matches" fixedTopGap="56">
8+
[style.marginTop.px]="isMobile() ? 56 : 0">
9+
<mat-sidenav #snav [mode]="isMobile() ? 'over' : 'side'"
10+
[fixedInViewport]="isMobile()" fixedTopGap="56">
1111
<mat-nav-list>
1212
<a mat-list-item routerLink="." *ngFor="let nav of fillerNav">{{nav}}</a>
1313
</mat-nav-list>
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {MediaMatcher} from '@angular/cdk/layout';
2-
import {ChangeDetectorRef, Component, OnDestroy} from '@angular/core';
2+
import {Component, OnDestroy, signal} from '@angular/core';
33
import {MatListModule} from '@angular/material/list';
44
import {MatSidenavModule} from '@angular/material/sidenav';
55
import {MatIconModule} from '@angular/material/icon';
66
import {MatButtonModule} from '@angular/material/button';
77
import {MatToolbarModule} from '@angular/material/toolbar';
88
import {NgIf, NgFor} from '@angular/common';
9+
import {RouterLink} from '@angular/router';
910

1011
/** @title Responsive sidenav */
1112
@Component({
@@ -21,14 +22,13 @@ import {NgIf, NgFor} from '@angular/common';
2122
MatSidenavModule,
2223
MatListModule,
2324
NgFor,
25+
RouterLink,
2426
],
2527
})
2628
export class SidenavResponsiveExample implements OnDestroy {
27-
mobileQuery: MediaQueryList;
29+
protected readonly fillerNav = Array.from({length: 50}, (_, i) => `Nav Item ${i + 1}`);
2830

29-
fillerNav = Array.from({length: 50}, (_, i) => `Nav Item ${i + 1}`);
30-
31-
fillerContent = Array.from(
31+
protected readonly fillerContent = Array.from(
3232
{length: 50},
3333
() =>
3434
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
@@ -38,17 +38,23 @@ export class SidenavResponsiveExample implements OnDestroy {
3838
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`,
3939
);
4040

41-
private _mobileQueryListener: () => void;
41+
protected readonly isMobile = signal(true);
42+
43+
private readonly _mobileQuery: MediaQueryList;
44+
private readonly _mobileQueryListener: () => void;
4245

43-
constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher) {
44-
this.mobileQuery = media.matchMedia('(max-width: 600px)');
45-
this._mobileQueryListener = () => changeDetectorRef.detectChanges();
46-
this.mobileQuery.addListener(this._mobileQueryListener);
46+
constructor(media: MediaMatcher) {
47+
this._mobileQuery = media.matchMedia('(max-width: 600px)');
48+
this.isMobile.set(this._mobileQuery.matches);
49+
this._mobileQueryListener = () => this.isMobile.set(this._mobileQuery.matches);
50+
this._mobileQuery.addEventListener('change', this._mobileQueryListener);
4751
}
4852

4953
ngOnDestroy(): void {
50-
this.mobileQuery.removeListener(this._mobileQueryListener);
54+
this._mobileQuery.removeEventListener('change', this._mobileQueryListener);
5155
}
5256

53-
shouldRun = /(^|.)(stackblitz|webcontainer).(io|com)$/.test(window.location.host);
57+
protected readonly shouldRun = /(^|.)(stackblitz|webcontainer).(io|com)$/.test(
58+
window.location.host,
59+
);
5460
}

0 commit comments

Comments
 (0)