Skip to content

build: toggle demo app dark theme for overlay components #6305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/demo-app/demo-app/demo-app.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>Angular Material Demos</h1>
<button md-icon-button (click)="toggleFullscreen()" title="Toggle fullscreen">
<md-icon class="md-24">fullscreen</md-icon>
</button>
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
<button md-button (click)="toggleTheme()">{{dark ? 'Light' : 'Dark'}} theme</button>
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
{{root.dir.toUpperCase()}}
</button>
Expand Down
31 changes: 26 additions & 5 deletions src/demo-app/demo-app/demo-app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';
import {
Component,
ViewEncapsulation,
ElementRef,
ChangeDetectionStrategy,
Renderer2,
} from '@angular/core';
import {OverlayContainer} from '@angular/material';

const changeDetectionKey = 'mdDemoChangeDetection';

Expand Down Expand Up @@ -45,9 +52,6 @@ export class DemoAppOnPush {}
selector: 'demo-app',
templateUrl: 'demo-app.html',
styleUrls: ['demo-app.css'],
host: {
'[class.unicorn-dark-theme]': 'dark',
},
encapsulation: ViewEncapsulation.None,
})
export class DemoApp {
Expand Down Expand Up @@ -90,7 +94,10 @@ export class DemoApp {
{name: 'Typography', route: '/typography'}
];

constructor(private _element: ElementRef) {
constructor(
private _element: ElementRef,
private _renderer: Renderer2,
private _overlayContainer: OverlayContainer) {
// Some browsers will throw when trying to access localStorage in incognito.
try {
this.changeDetectionStrategy = window.localStorage.getItem(changeDetectionKey) || 'Default';
Expand Down Expand Up @@ -122,4 +129,18 @@ export class DemoApp {
console.error(error);
}
}

toggleTheme() {
const darkThemeClass = 'unicorn-dark-theme';

this.dark = !this.dark;

if (this.dark) {
this._renderer.addClass(this._element.nativeElement, darkThemeClass);
this._overlayContainer.themeClass = darkThemeClass;
} else {
this._renderer.removeClass(this._element.nativeElement, darkThemeClass);
this._overlayContainer.themeClass = '';
}
}
}