Skip to content

Commit d52acc9

Browse files
Airbladertinayuangao
authored andcommitted
fix(nav-schematics): Subscribe to breakpoint observable (#11310)
* fix(nav-schematics): Subscribe to handset for role in nav schematic. Otherwise, we are only checking if the observable is defined rather than actually checking the breakpoint. * style(nav-schematics): Suffix observable with '$' This follow the Google guidelines for naming observables. * style(nav-schematics): Map breakpoint observer result to matches property
1 parent f5377dd commit d52acc9

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#drawer
44
class="sidenav"
55
fixedInViewport="true"
6-
[attr.role]="isHandset ? 'dialog' : 'navigation'"
7-
[mode]="(isHandset | async)!.matches ? 'over' : 'side'"
8-
[opened]="!(isHandset | async)!.matches">
6+
[attr.role]="isHandset$ | async ? 'dialog' : 'navigation'"
7+
[mode]="isHandset$ | async ? 'over' : 'side'"
8+
[opened]="!(isHandset$ | async)">
99
<mat-toolbar color="primary">Menu</mat-toolbar>
1010
<mat-nav-list>
1111
<a mat-list-item href="#">Link 1</a>
@@ -20,7 +20,7 @@
2020
aria-label="Toggle sidenav"
2121
mat-icon-button
2222
(click)="drawer.toggle()"
23-
*ngIf="(isHandset | async)!.matches">
23+
*ngIf="isHandset$ | async">
2424
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
2525
</button>
2626
<span>Application Title</span>

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
22
import { BreakpointObserver, Breakpoints, BreakpointState } from '@angular/cdk/layout';
33
import { Observable } from 'rxjs';
4+
import { map } from 'rxjs/operators';
45

56
@Component({
67
selector: '<%= selector %>',<% if(inlineTemplate) { %>
@@ -10,9 +11,9 @@ import { Observable } from 'rxjs';
1011
#drawer
1112
class="sidenav"
1213
fixedInViewport="true"
13-
[attr.role]="isHandset ? 'dialog' : 'navigation'"
14-
[mode]="(isHandset | async)!.matches ? 'over' : 'side'"
15-
[opened]="!(isHandset | async)!.matches">
14+
[attr.role]="isHandset$ | async ? 'dialog' : 'navigation'"
15+
[mode]="isHandset$ | async ? 'over' : 'side'"
16+
[opened]="!(isHandset$ | async)">
1617
<mat-toolbar color="primary">Menu</mat-toolbar>
1718
<mat-nav-list>
1819
<a mat-list-item href="#">Link 1</a>
@@ -27,7 +28,7 @@ import { Observable } from 'rxjs';
2728
aria-label="Toggle sidenav"
2829
mat-icon-button
2930
(click)="drawer.toggle()"
30-
*ngIf="(isHandset | async)!.matches">
31+
*ngIf="isHandset$ | async">
3132
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
3233
</button>
3334
<span>Application Title</span>
@@ -53,6 +54,12 @@ import { Observable } from 'rxjs';
5354
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
5455
})
5556
export class <%= classify(name) %>Component {
56-
isHandset: Observable<BreakpointState> = this.breakpointObserver.observe(Breakpoints.Handset);
57+
58+
isHandset$: Observable<BreakpointState> = this.breakpointObserver.observe(Breakpoints.Handset)
59+
.pipe(
60+
map(result => result.matches)
61+
);
62+
5763
constructor(private breakpointObserver: BreakpointObserver) {}
58-
}
64+
65+
}

0 commit comments

Comments
 (0)