Skip to content
Open
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: 0 additions & 2 deletions src/app/pages/circular-heatmap/circular-heatmap.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ export class CircularHeatmapComponent implements OnInit, OnDestroy {
}

ngOnInit(): void {
const savedTheme: string = this.themeService.getTheme() || 'light';
this.themeService.setTheme(savedTheme); // sets .light-theme or .dark-theme
requestAnimationFrame(() => {
// Now the DOM has the correct class and CSS vars are live
console.log(`${perfNow()}s: ngOnInit: Initial theme:`, this.theme);
Expand Down
28 changes: 22 additions & 6 deletions src/app/service/theme.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

export type AppTheme = 'light' | 'dark';

@Injectable({ providedIn: 'root' })
export class ThemeService {
private themeSubject = new BehaviorSubject<string>('light');
private readonly STORAGE_KEY = 'theme';
private readonly defaultTheme: AppTheme = 'light';

private themeSubject = new BehaviorSubject<AppTheme>(this.defaultTheme);
public readonly theme$ = this.themeSubject.asObservable();

constructor() {}

initTheme(): void {
const saved = localStorage.getItem('theme') || 'light';
this.setTheme(saved);
const stored = localStorage.getItem(this.STORAGE_KEY);
const theme: AppTheme = stored === 'dark' ? 'dark' : this.defaultTheme;
this.setTheme(theme);
}

setTheme(theme: AppTheme): void {
if (this.themeSubject.value === theme) return;
this.applyTheme(theme);
}

setTheme(theme: string): void {
private applyTheme(theme: AppTheme): void {
document.body.classList.remove('light-theme', 'dark-theme');
document.body.classList.add(`${theme}-theme`);
localStorage.setItem('theme', theme);

localStorage.setItem(this.STORAGE_KEY, theme);
this.themeSubject.next(theme);
}

getTheme(): string {
getTheme(): AppTheme {
return this.themeSubject.value;
}

toggleTheme(): void {
this.setTheme(this.themeSubject.value === 'light' ? 'dark' : 'light');
}
}
1 change: 0 additions & 1 deletion src/custom-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ body.dark-theme {
}
}

@include mat.all-component-themes($DSOMM-dark-theme);

.button-container {
display: flex;
Expand Down