Skip to content

Commit af67e7c

Browse files
crisbetommalerba
authored andcommitted
build: toggle demo app dark theme for overlay components (#6305)
Fixes the dark theme not being added to overlay-based components.
1 parent 49d6080 commit af67e7c

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h1>Angular Material Demos</h1>
3636
<button md-icon-button (click)="toggleFullscreen()" title="Toggle fullscreen">
3737
<md-icon class="md-24">fullscreen</md-icon>
3838
</button>
39-
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
39+
<button md-button (click)="toggleTheme()">{{dark ? 'Light' : 'Dark'}} theme</button>
4040
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
4141
{{root.dir.toUpperCase()}}
4242
</button>

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';
1+
import {
2+
Component,
3+
ViewEncapsulation,
4+
ElementRef,
5+
ChangeDetectionStrategy,
6+
Renderer2,
7+
} from '@angular/core';
8+
import {OverlayContainer} from '@angular/material';
29

310
const changeDetectionKey = 'mdDemoChangeDetection';
411

@@ -45,9 +52,6 @@ export class DemoAppOnPush {}
4552
selector: 'demo-app',
4653
templateUrl: 'demo-app.html',
4754
styleUrls: ['demo-app.css'],
48-
host: {
49-
'[class.unicorn-dark-theme]': 'dark',
50-
},
5155
encapsulation: ViewEncapsulation.None,
5256
})
5357
export class DemoApp {
@@ -90,7 +94,10 @@ export class DemoApp {
9094
{name: 'Typography', route: '/typography'}
9195
];
9296

93-
constructor(private _element: ElementRef) {
97+
constructor(
98+
private _element: ElementRef,
99+
private _renderer: Renderer2,
100+
private _overlayContainer: OverlayContainer) {
94101
// Some browsers will throw when trying to access localStorage in incognito.
95102
try {
96103
this.changeDetectionStrategy = window.localStorage.getItem(changeDetectionKey) || 'Default';
@@ -122,4 +129,18 @@ export class DemoApp {
122129
console.error(error);
123130
}
124131
}
132+
133+
toggleTheme() {
134+
const darkThemeClass = 'unicorn-dark-theme';
135+
136+
this.dark = !this.dark;
137+
138+
if (this.dark) {
139+
this._renderer.addClass(this._element.nativeElement, darkThemeClass);
140+
this._overlayContainer.themeClass = darkThemeClass;
141+
} else {
142+
this._renderer.removeClass(this._element.nativeElement, darkThemeClass);
143+
this._overlayContainer.themeClass = '';
144+
}
145+
}
125146
}

0 commit comments

Comments
 (0)