Skip to content

Commit 698743f

Browse files
committed
build: add toggle for OnPush change detection in demo app
Adds a toggle for enabling `OnPush` change detection in the demo app. This should make it easier to test out components with the different change detection strategies.
1 parent 1ec88e0 commit 698743f

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

src/demo-app/demo-app-module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {HttpModule} from '@angular/http';
44
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
55
import {RouterModule} from '@angular/router';
66
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
7-
import {DemoApp, Home} from './demo-app/demo-app';
7+
import {DemoApp, Home, DemoAppOnPush} from './demo-app/demo-app';
88
import {
99
MaterialModule,
1010
OverlayContainer,
@@ -65,6 +65,7 @@ import {StyleDemo} from './style/style-demo';
6565
ChipsDemo,
6666
CheckboxDemo,
6767
DemoApp,
68+
DemoAppOnPush,
6869
DialogDemo,
6970
GesturesDemo,
7071
GridListDemo,

src/demo-app/demo-app/demo-app.html

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@
2525
</button>
2626
<div class="demo-toolbar">
2727
<h1>Angular Material Demos</h1>
28-
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
29-
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
30-
Fullscreen
31-
</button>
32-
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
33-
{{root.dir.toUpperCase()}}
34-
</button>
28+
<div>
29+
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
30+
<button md-button (click)="toggleChangeDetection()" title="Toggle change detection">
31+
Change detection: {{changeDetectionStrategy}}
32+
</button>
33+
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
34+
Fullscreen
35+
</button>
36+
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
37+
{{root.dir.toUpperCase()}}
38+
</button>
39+
</div>
3540
</div>
3641
</md-toolbar>
3742

38-
<div #root="$implicit" dir="ltr" class="demo-content">
39-
<router-outlet></router-outlet>
43+
<div #root="$implicit" dir="ltr" class="demo-content" [ngSwitch]="changeDetectionStrategy">
44+
<div *ngSwitchDefault>
45+
<router-outlet></router-outlet>
46+
</div>
47+
48+
<demo-app-on-push *ngSwitchCase="'OnPush'">
49+
<router-outlet></router-outlet>
50+
</demo-app-on-push>
4051
</div>
4152
</div>
4253
</md-sidenav-container>

src/demo-app/demo-app/demo-app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ body {
3232
.demo-toolbar {
3333
display: flex;
3434
justify-content: space-between;
35+
align-items: center;
3536
width: 100%;
3637
}
3738
}

src/demo-app/demo-app/demo-app.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import {Component, ViewEncapsulation, ElementRef} from '@angular/core';
1+
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';
22

3+
const changeDetectionKey = 'mdDemoChangeDetection';
34

45
@Component({
56
selector: 'home',
67
template: `
78
<p>Welcome to the development demos for Angular Material!</p>
8-
<p>Open the sidenav to select a demo. </p>
9+
<p>Open the sidenav to select a demo.</p>
910
`
1011
})
1112
export class Home {}
1213

14+
@Component({
15+
moduleId: module.id,
16+
selector: 'demo-app-on-push',
17+
template: '<ng-content></ng-content>',
18+
changeDetection: ChangeDetectionStrategy.OnPush,
19+
encapsulation: ViewEncapsulation.None,
20+
})
21+
export class DemoAppOnPush {}
22+
1323
@Component({
1424
moduleId: module.id,
1525
selector: 'demo-app',
@@ -22,7 +32,7 @@ export class Home {}
2232
})
2333
export class DemoApp {
2434
dark = false;
25-
35+
changeDetectionStrategy: string;
2636
navItems = [
2737
{name: 'Autocomplete', route: 'autocomplete'},
2838
{name: 'Button', route: 'button'},
@@ -57,7 +67,12 @@ export class DemoApp {
5767
];
5868

5969
constructor(private _element: ElementRef) {
60-
70+
// Some browsers will throw when trying to access localStorage in incognito.
71+
try {
72+
this.changeDetectionStrategy = localStorage.getItem(changeDetectionKey) || 'Default';
73+
} catch (error) {
74+
console.error(error);
75+
}
6176
}
6277

6378
toggleFullscreen() {
@@ -72,4 +87,15 @@ export class DemoApp {
7287
elem.msRequestFullScreen();
7388
}
7489
}
90+
91+
toggleChangeDetection() {
92+
try {
93+
this.changeDetectionStrategy = this.changeDetectionStrategy === 'Default' ?
94+
'OnPush' : 'Default';
95+
localStorage.setItem(changeDetectionKey, this.changeDetectionStrategy);
96+
location.reload();
97+
} catch (error) {
98+
console.error(error);
99+
}
100+
}
75101
}

src/demo-app/tooltip/tooltip-demo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ChangeDetectionStrategy} from '@angular/core';
1+
import {Component} from '@angular/core';
22
import {TooltipPosition} from '@angular/material';
33

44

@@ -7,7 +7,6 @@ import {TooltipPosition} from '@angular/material';
77
selector: 'tooltip-demo',
88
templateUrl: 'tooltip-demo.html',
99
styleUrls: ['tooltip-demo.css'],
10-
changeDetection: ChangeDetectionStrategy.OnPush // make sure tooltip also works OnPush
1110
})
1211
export class TooltipDemo {
1312
position: TooltipPosition = 'below';

0 commit comments

Comments
 (0)