-
Notifications
You must be signed in to change notification settings - Fork 15
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
PhilipAB
committed
Oct 25, 2024
1 parent
0e36956
commit 9526918
Showing
7 changed files
with
136 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnDestroy, OnInit } from '@angular/core'; | ||
import '@cds/core/icon/register.js'; | ||
import { ClarityIcons } from '@cds/core/icon'; | ||
import { Router } from '@angular/router'; | ||
import { AppConfigService } from './app-config.service'; | ||
import { Subscription } from 'rxjs'; | ||
import { ThemeService } from './data/theme.service'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
template: '<router-outlet></router-outlet>', | ||
}) | ||
export class AppComponent { | ||
|
||
export class AppComponent implements OnInit, OnDestroy { | ||
private config = this.configService.getConfig(); | ||
public logo = this.config.logo || '/assets/default/logo.svg'; | ||
private logo = this.config.logo || '/assets/default/logo.svg'; | ||
private themeSubscription: Subscription; | ||
|
||
constructor( | ||
public router: Router, | ||
public configService: AppConfigService | ||
) { | ||
this.configService.getLogo(this.logo) | ||
.then((obj: string) => { | ||
ClarityIcons.addIcons(['logo', obj]); | ||
}) | ||
private router: Router, | ||
private configService: AppConfigService, | ||
private themeService: ThemeService | ||
) {} | ||
ngOnInit(): void { | ||
this.configService.getLogo(this.logo).then((obj: string) => { | ||
ClarityIcons.addIcons(['logo', obj]); | ||
}); | ||
this.themeSubscription = this.themeService.listenToThemeChanges(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.themeSubscription.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Injectable, Renderer2, RendererFactory2 } from '@angular/core'; | ||
import { SettingsService, Settings } from './settings.service'; | ||
import { fromEventPattern, Subscription } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ThemeService { | ||
private renderer: Renderer2; | ||
|
||
constructor( | ||
private rendererFactory: RendererFactory2, | ||
private settingsService: SettingsService | ||
) { | ||
this.renderer = this.rendererFactory.createRenderer(null, null); | ||
} | ||
|
||
listenToThemeChanges() { | ||
let mediaQuerySubscription: Subscription | null = null; | ||
|
||
return this.settingsService.settings$.subscribe((settings: Settings) => { | ||
if (settings.theme === 'system') { | ||
const darkModeMediaQuery = window.matchMedia( | ||
'(prefers-color-scheme: dark)' | ||
); | ||
// Applying the current system theme | ||
this.setTheme(darkModeMediaQuery.matches ? 'dark' : 'light'); | ||
|
||
// If system theme listener is not set up already, do it here | ||
if (!mediaQuerySubscription) { | ||
mediaQuerySubscription = fromEventPattern<MediaQueryListEvent>( | ||
(handler) => darkModeMediaQuery.addEventListener('change', handler), | ||
(handler) => | ||
darkModeMediaQuery.removeEventListener('change', handler) | ||
).subscribe((event) => { | ||
this.setTheme(event.matches ? 'dark' : 'light'); | ||
}); | ||
} | ||
} else { | ||
// Applying the user-defined theme directly | ||
this.setTheme(settings.theme); | ||
|
||
// Unsubscribing from media query listener if it exists | ||
if (mediaQuerySubscription) { | ||
mediaQuerySubscription.unsubscribe(); | ||
mediaQuerySubscription = null; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private setTheme(theme: 'light' | 'dark') { | ||
this.renderer.setAttribute(document.body, 'cds-theme', theme); | ||
} | ||
} |
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