Skip to content

docs(material/sidenav): Deprecated methods used in the responsive sidenav example #29317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions src/components-examples/material/sidenav/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ng_module(
"@npm//@angular/forms",
"@npm//@angular/platform-browser",
"@npm//@types/jasmine",
"@npm//rxjs",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
@if (shouldRun) {
<div class="example-container" [class.example-is-mobile]="mobileQuery.matches">
<mat-toolbar class="example-toolbar">
<button mat-icon-button (click)="snav.toggle()"><mat-icon>menu</mat-icon></button>
<div class="example-container" [class.example-is-mobile]="(isHandset | async)?.matches">
<mat-toolbar color="primary" class="example-toolbar">
<button mat-icon-button (click)="sidenav.toggle()">
<mat-icon>menu</mat-icon>
</button>
<h1 class="example-app-name">Responsive App</h1>
</mat-toolbar>

<mat-sidenav-container class="example-sidenav-container"
[style.marginTop.px]="mobileQuery.matches ? 56 : 0">
<mat-sidenav #snav [mode]="mobileQuery.matches ? 'over' : 'side'"
[fixedInViewport]="mobileQuery.matches" fixedTopGap="56">
<mat-sidenav-container class="example-sidenav-container" [style.marginTop.px]="(isHandset | async) ? 56 : 0">
<mat-sidenav #sidenav [mode]="(isHandset | async) ? 'over' : 'side'" [fixedInViewport]="(isHandset | async)?.matches" fixedTopGap="56">
<mat-nav-list>
@for (nav of fillerNav; track nav) {
<a mat-list-item routerLink=".">{{nav}}</a>
<a mat-list-item routerLink=".">{{ nav }}</a>
}
</mat-nav-list>
</mat-sidenav>

<mat-sidenav-content>
@for (content of fillerContent; track content) {
<p>{{content}}</p>
}
<p>{{ content }}</p>
}
</mat-sidenav-content>
</mat-sidenav-container>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import {MediaMatcher} from '@angular/cdk/layout';
import {BreakpointObserver, BreakpointState, Breakpoints} from '@angular/cdk/layout';
import {ChangeDetectorRef, Component, OnDestroy} from '@angular/core';
import {MatListModule} from '@angular/material/list';
import {MatSidenavModule} from '@angular/material/sidenav';
import {MatIconModule} from '@angular/material/icon';
import {MatButtonModule} from '@angular/material/button';
import {MatToolbarModule} from '@angular/material/toolbar';
import {Observable, Subscription} from 'rxjs';
import {AsyncPipe} from '@angular/common';

/** @title Responsive sidenav */
@Component({
selector: 'sidenav-responsive-example',
templateUrl: 'sidenav-responsive-example.html',
styleUrl: 'sidenav-responsive-example.css',
standalone: true,
imports: [MatToolbarModule, MatButtonModule, MatIconModule, MatSidenavModule, MatListModule],
imports: [
AsyncPipe,
MatToolbarModule,
MatButtonModule,
MatIconModule,
MatSidenavModule,
MatListModule,
],
})
export class SidenavResponsiveExample implements OnDestroy {
mobileQuery: MediaQueryList;

isHandset: Observable<BreakpointState>;
fillerNav = Array.from({length: 50}, (_, i) => `Nav Item ${i + 1}`);

fillerContent = Array.from(
{length: 50},
() =>
Expand All @@ -29,16 +36,27 @@ export class SidenavResponsiveExample implements OnDestroy {
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`,
);

private _mobileQueryListener: () => void;
mobileQuery: boolean;
private _breakpointSubscription: Subscription;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this and _breakpointSubscription aren't used anymore and can be removed.


constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher) {
this.mobileQuery = media.matchMedia('(max-width: 600px)');
this._mobileQueryListener = () => changeDetectorRef.detectChanges();
this.mobileQuery.addListener(this._mobileQueryListener);
constructor(
private _breakpointObserver: BreakpointObserver,
private _changeDetectorRef: ChangeDetectorRef,
) {
this.isHandset = _breakpointObserver.observe([
Breakpoints.HandsetLandscape,
Breakpoints.HandsetPortrait,
]);
this._breakpointSubscription = this._breakpointObserver
.observe([Breakpoints.Handset])
.subscribe(result => {
this.mobileQuery = result.matches;
this._changeDetectorRef.detectChanges();
});
}

ngOnDestroy(): void {
this.mobileQuery.removeListener(this._mobileQueryListener);
this._breakpointSubscription.unsubscribe();
}

shouldRun = /(^|.)(stackblitz|webcontainer).(io|com)$/.test(window.location.host);
Expand Down
Loading