Skip to content

Commit 79a63f8

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 3ad6ff0 commit 79a63f8

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525
</button>
2626
<div class="demo-toolbar">
2727
<h1>Angular Material Demos</h1>
28-
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
29-
Fullscreen
30-
</button>
31-
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
32-
{{root.dir.toUpperCase()}}
33-
</button>
28+
<div>
29+
<button md-button (click)="toggleChangeDetection()" title="Toggle change detection">
30+
Change detection: {{isPushChangeDetection ? 'OnPush' : 'Default'}}
31+
</button>
32+
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
33+
Fullscreen
34+
</button>
35+
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
36+
{{root.dir.toUpperCase()}}
37+
</button>
38+
</div>
3439
</div>
3540
</md-toolbar>
3641

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: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import {Component, ViewEncapsulation, ElementRef} from '@angular/core';
1+
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';
22

3+
const changeDetectionKey = 'mdDemoChangeDetectionOnPush';
4+
let isPushChangeDetection = false;
5+
6+
// Some browsers will throw when trying to access localStorage in incognito.
7+
try {
8+
isPushChangeDetection = !!localStorage.getItem(changeDetectionKey);
9+
} catch (error) {
10+
console.error(error);
11+
}
312

413
@Component({
514
selector: 'home',
615
template: `
716
<p>Welcome to the development demos for Angular Material!</p>
8-
<p>Open the sidenav to select a demo. </p>
17+
<p>Open the sidenav to select a demo.</p>
918
`
1019
})
1120
export class Home {}
@@ -17,8 +26,12 @@ export class Home {}
1726
templateUrl: 'demo-app.html',
1827
styleUrls: ['demo-app.css'],
1928
encapsulation: ViewEncapsulation.None,
29+
changeDetection: isPushChangeDetection ?
30+
ChangeDetectionStrategy.OnPush :
31+
ChangeDetectionStrategy.Default
2032
})
2133
export class DemoApp {
34+
isPushChangeDetection = isPushChangeDetection;
2235
navItems = [
2336
{name: 'Autocomplete', route: 'autocomplete'},
2437
{name: 'Button', route: 'button'},
@@ -53,9 +66,7 @@ export class DemoApp {
5366
{name: 'Style', route: 'style'}
5467
];
5568

56-
constructor(private _element: ElementRef) {
57-
58-
}
69+
constructor(private _element: ElementRef) { }
5970

6071
toggleFullscreen() {
6172
let elem = this._element.nativeElement.querySelector('.demo-content');
@@ -69,4 +80,17 @@ export class DemoApp {
6980
elem.msRequestFullScreen();
7081
}
7182
}
83+
84+
toggleChangeDetection() {
85+
try {
86+
if (this.isPushChangeDetection) {
87+
localStorage.removeItem(changeDetectionKey);
88+
} else {
89+
localStorage.setItem(changeDetectionKey, 'true');
90+
}
91+
location.reload();
92+
} catch (error) {
93+
console.error(error);
94+
}
95+
}
7296
}

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)