-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
128 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ export enum ThemeList { | |
Light = 'light', | ||
Dark = 'dark', | ||
} | ||
|
||
export const defaultBaseTheme = ThemeList.System; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,94 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { Inject, Injectable, OnDestroy } from '@angular/core'; | ||
import { getItem, setItem, StorageItem } from '@app/@core/utils'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { ThemeList } from './theme.config'; | ||
import { fromEventPattern, Subject } from 'rxjs'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
import { defaultBaseTheme, ThemeList } from './theme.config'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ThemeService { | ||
private currentTheme$ = new BehaviorSubject<ThemeList>( | ||
this.currentTheme || ThemeList.System, | ||
export class ThemeService implements OnDestroy { | ||
destroy$ = new Subject(); | ||
|
||
private readonly mediaQuery = window.matchMedia( | ||
'(prefers-color-scheme: dark)', | ||
); | ||
|
||
constructor(@Inject(DOCUMENT) private document: Document) {} | ||
|
||
get currentTheme(): ThemeList { | ||
get systemTheme(): ThemeList.Light | ThemeList.Dark { | ||
return this.mediaQuery.matches ? ThemeList.Dark : ThemeList.Light; | ||
} | ||
|
||
get currentAppTheme(): ThemeList { | ||
return getItem(StorageItem.Theme) as ThemeList; | ||
} | ||
|
||
/** | ||
* Makes initial check for system preferences and attach mediaQuery listener | ||
* | ||
*/ | ||
init(): void { | ||
if (!this.currentTheme) { | ||
this.listenForMediaQuery(); | ||
if (this.currentAppTheme) { | ||
this.setTheme(this.currentAppTheme); | ||
} else if (this.currentAppTheme === ThemeList.System) { | ||
this.setTheme(this.systemTheme); | ||
} else { | ||
this.setTheme(defaultBaseTheme); | ||
} | ||
|
||
this.listenForThemeChanges(); | ||
this.listenForMediaQueryChanges(); | ||
} | ||
|
||
changeTheme(theme: ThemeList): void { | ||
this.currentTheme$.next(theme); | ||
} | ||
|
||
private listenForMediaQuery(): void { | ||
const colorScheme = window.matchMedia('(prefers-color-scheme: dark)'); | ||
|
||
colorScheme.addEventListener('change', (e: MediaQueryListEvent) => { | ||
const currentTheme = this.currentTheme; | ||
/** | ||
* Manually changes theme in BehaviorSubject, LocalStorage & HTML element | ||
* | ||
* @param theme new theme | ||
*/ | ||
setTheme(theme: ThemeList): void { | ||
this.clearThemes(); | ||
setItem(StorageItem.Theme, theme); | ||
|
||
if (currentTheme === ThemeList.System) { | ||
const theme = e.matches ? ThemeList.Dark : ThemeList.Light; | ||
this.setTheme(theme); | ||
} | ||
}); | ||
} | ||
let bodyClass = theme; | ||
|
||
private listenForThemeChanges(): void { | ||
this.currentTheme$.subscribe((theme) => { | ||
this.setTheme(theme); | ||
}); | ||
if (theme === ThemeList.System) { | ||
bodyClass = this.systemTheme; | ||
} | ||
this.document.body.classList.add(bodyClass); | ||
} | ||
|
||
private setTheme(theme: ThemeList): void { | ||
this.clearThemes(); | ||
this.document.body.classList.add(theme); | ||
setItem(StorageItem.Theme, theme); | ||
/** | ||
* Handles system color preference changes | ||
* | ||
*/ | ||
private listenForMediaQueryChanges(): void { | ||
fromEventPattern<MediaQueryListEvent>( | ||
this.mediaQuery.addListener.bind(this.mediaQuery), | ||
this.mediaQuery.removeListener.bind(this.mediaQuery), | ||
) | ||
.pipe(takeUntil(this.destroy$)) | ||
.subscribe(() => { | ||
// Only applies changes when the current theme is "system" | ||
if (this.currentAppTheme === ThemeList.System) { | ||
this.setTheme(ThemeList.System); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Clears all themes in ThemeList enum from the HTML element | ||
* | ||
*/ | ||
private clearThemes(): void { | ||
for (const theme in ThemeList) { | ||
const key: ThemeList = ThemeList[theme as keyof typeof ThemeList]; | ||
this.document.body.classList.remove(key); | ||
} | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.destroy$.complete(); | ||
this.destroy$.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51a9766
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: