Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@if (shouldRun) {
<div class="example-container" [class.example-is-mobile]="mobileQuery.matches">
<div class="example-container" [class.example-is-mobile]="isMobile()">
<mat-toolbar class="example-toolbar">
<button mat-icon-button (click)="snav.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">
[style.marginTop.px]="isMobile() ? 56 : 0">
<mat-sidenav #snav [mode]="isMobile() ? 'over' : 'side'"
[fixedInViewport]="isMobile()" fixedTopGap="56">
<mat-nav-list>
@for (nav of fillerNav; track nav) {
<a mat-list-item routerLink=".">{{nav}}</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import {MediaMatcher} from '@angular/cdk/layout';
import {ChangeDetectorRef, Component, OnDestroy, inject} from '@angular/core';
import {Component, OnDestroy, inject, signal} 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 {RouterLink} from '@angular/router';

/** @title Responsive sidenav */
@Component({
selector: 'sidenav-responsive-example',
templateUrl: 'sidenav-responsive-example.html',
styleUrl: 'sidenav-responsive-example.css',
imports: [MatToolbarModule, MatButtonModule, MatIconModule, MatSidenavModule, MatListModule],
imports: [
MatToolbarModule,
MatButtonModule,
MatIconModule,
MatSidenavModule,
MatListModule,
RouterLink,
],
})
export class SidenavResponsiveExample implements OnDestroy {
mobileQuery: MediaQueryList;
protected readonly fillerNav = Array.from({length: 50}, (_, i) => `Nav Item ${i + 1}`);

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

fillerContent = Array.from(
protected readonly fillerContent = Array.from(
{length: 50},
() =>
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
Expand All @@ -28,20 +34,25 @@ export class SidenavResponsiveExample implements OnDestroy {
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`,
);

private _mobileQueryListener: () => void;
protected readonly isMobile = signal(true);

private readonly _mobileQuery: MediaQueryList;
private readonly _mobileQueryListener: () => void;

constructor() {
const changeDetectorRef = inject(ChangeDetectorRef);
const media = inject(MediaMatcher);

this.mobileQuery = media.matchMedia('(max-width: 600px)');
this._mobileQueryListener = () => changeDetectorRef.detectChanges();
this.mobileQuery.addListener(this._mobileQueryListener);
this._mobileQuery = media.matchMedia('(max-width: 600px)');
this.isMobile.set(this._mobileQuery.matches);
this._mobileQueryListener = () => this.isMobile.set(this._mobileQuery.matches);
this._mobileQuery.addEventListener('change', this._mobileQueryListener);
}

ngOnDestroy(): void {
this.mobileQuery.removeListener(this._mobileQueryListener);
this._mobileQuery.removeEventListener('change', this._mobileQueryListener);
}

shouldRun = /(^|.)(stackblitz|webcontainer).(io|com)$/.test(window.location.host);
protected readonly shouldRun = /(^|.)(stackblitz|webcontainer).(io|com)$/.test(
window.location.host,
);
}
Loading