Skip to content

chore: store dev-app dark/light theme state in localStorage #18218

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 12, 2020
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
4 changes: 3 additions & 1 deletion src/dev-app/dev-app/dev-app-layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ <h1>Angular Material Demos</h1>
<button mat-icon-button (click)="toggleFullscreen()" title="Toggle fullscreen">
<mat-icon>fullscreen</mat-icon>
</button>
<button mat-button (click)="toggleTheme()">{{dark ? 'Light' : 'Dark'}} theme</button>
<button mat-button (click)="isDark = !isDark">
{{isDark ? 'Light' : 'Dark'}} theme
</button>
<button mat-button (click)="rippleOptions.disabled = !rippleOptions.disabled">
{{rippleOptions.disabled ? 'Enable' : 'Disable'}} ripples
</button>
Expand Down
58 changes: 44 additions & 14 deletions src/dev-app/dev-app/dev-app-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {Directionality} from '@angular/cdk/bidi';
import {ChangeDetectorRef, Component, ElementRef, Inject, ViewEncapsulation} from '@angular/core';
import {DevAppRippleOptions} from './ripple-options';
import {DevAppDirectionality} from './dev-app-directionality';
import {DOCUMENT} from '@angular/common';

const isDarkThemeKey = 'ANGULAR_COMPONENTS_DEV_APP_DARK_THEME';

/** Root component for the dev-app demos. */
@Component({
Expand All @@ -19,7 +22,8 @@ import {DevAppDirectionality} from './dev-app-directionality';
encapsulation: ViewEncapsulation.None,
})
export class DevAppLayout {
dark = false;
readonly darkThemeClass = 'demo-unicorn-dark-theme';
_isDark = false;
strongFocus = false;
navItems = [
{name: 'Examples', route: '/examples'},
Expand Down Expand Up @@ -97,9 +101,39 @@ export class DevAppLayout {

constructor(
private _element: ElementRef<HTMLElement>, public rippleOptions: DevAppRippleOptions,
@Inject(Directionality) public dir: DevAppDirectionality, cdr: ChangeDetectorRef) {
@Inject(Directionality) public dir: DevAppDirectionality, cdr: ChangeDetectorRef,
@Inject(DOCUMENT) private _document: Document) {
dir.change.subscribe(() => cdr.markForCheck());
this.updateDensityClasses();
try {
const isDark = localStorage.getItem(isDarkThemeKey);
if (isDark != null) {
// We avoid calling the setter and apply the themes directly here.
// This avoids writing the same value, that we just read, back to localStorage.
this._isDark = isDark === 'true';
this.updateThemeClass(this._isDark);
}
} catch (error) {
console.error(`Failed to read ${isDarkThemeKey} from localStorage: `, error);
}
}

get isDark(): boolean {
return this._isDark;
}

set isDark(value: boolean) {
// Noop if the value is the same as is already set.
if (value !== this._isDark) {
this._isDark = value;
this.updateThemeClass(this._isDark);

try {
localStorage.setItem(isDarkThemeKey, String(value));
} catch (error) {
console.error(`Failed to write ${isDarkThemeKey} to localStorage: `, error);
}
}
}

toggleFullscreen() {
Expand All @@ -116,15 +150,11 @@ export class DevAppLayout {
}
}

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

this.dark = !this.dark;

if (this.dark) {
document.body.classList.add(darkThemeClass);
updateThemeClass(isDark?: boolean) {
if (isDark) {
this._document.body.classList.add(this.darkThemeClass);
} else {
document.body.classList.remove(darkThemeClass);
this._document.body.classList.remove(this.darkThemeClass);
}
}

Expand All @@ -134,9 +164,9 @@ export class DevAppLayout {
this.strongFocus = !this.strongFocus;

if (this.strongFocus) {
document.body.classList.add(strongFocusClass);
this._document.body.classList.add(strongFocusClass);
} else {
document.body.classList.remove(strongFocusClass);
this._document.body.classList.remove(strongFocusClass);
}
}

Expand All @@ -160,9 +190,9 @@ export class DevAppLayout {
for (let i = 0; i < this.densityScales.length; i++) {
const className = `demo-density-${this.densityScales[i]}`;
if (i === this.currentDensityIndex) {
document.body.classList.add(className);
this._document.body.classList.add(className);
} else {
document.body.classList.remove(className);
this._document.body.classList.remove(className);
}
}
}
Expand Down