|
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'; |
2 | 9 |
|
3 | 10 | const changeDetectionKey = 'mdDemoChangeDetection'; |
4 | 11 |
|
@@ -45,9 +52,6 @@ export class DemoAppOnPush {} |
45 | 52 | selector: 'demo-app', |
46 | 53 | templateUrl: 'demo-app.html', |
47 | 54 | styleUrls: ['demo-app.css'], |
48 | | - host: { |
49 | | - '[class.unicorn-dark-theme]': 'dark', |
50 | | - }, |
51 | 55 | encapsulation: ViewEncapsulation.None, |
52 | 56 | }) |
53 | 57 | export class DemoApp { |
@@ -90,7 +94,10 @@ export class DemoApp { |
90 | 94 | {name: 'Typography', route: '/typography'} |
91 | 95 | ]; |
92 | 96 |
|
93 | | - constructor(private _element: ElementRef) { |
| 97 | + constructor( |
| 98 | + private _element: ElementRef, |
| 99 | + private _renderer: Renderer2, |
| 100 | + private _overlayContainer: OverlayContainer) { |
94 | 101 | // Some browsers will throw when trying to access localStorage in incognito. |
95 | 102 | try { |
96 | 103 | this.changeDetectionStrategy = window.localStorage.getItem(changeDetectionKey) || 'Default'; |
@@ -122,4 +129,18 @@ export class DemoApp { |
122 | 129 | console.error(error); |
123 | 130 | } |
124 | 131 | } |
| 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 | + } |
125 | 146 | } |
0 commit comments