From bba3309310ac92fc1af844885e9aa46b1d5e7d7a Mon Sep 17 00:00:00 2001 From: "aditya.chandel" Date: Sat, 28 Jun 2025 18:18:34 -0600 Subject: [PATCH] Sync favicon color with primary theme --- .../theme-configurator/favicon-service.ts | 27 +++++++++++++++++++ .../theme-configurator.component.ts | 20 +++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 booklore-ui/src/app/layout/component/theme-configurator/favicon-service.ts diff --git a/booklore-ui/src/app/layout/component/theme-configurator/favicon-service.ts b/booklore-ui/src/app/layout/component/theme-configurator/favicon-service.ts new file mode 100644 index 000000000..6283061b1 --- /dev/null +++ b/booklore-ui/src/app/layout/component/theme-configurator/favicon-service.ts @@ -0,0 +1,27 @@ +import {Injectable} from '@angular/core'; + +@Injectable({providedIn: 'root'}) +export class FaviconService { + private svgTemplate = (color: string) => ` + + + + + `; + + updateFavicon(color: string) { + const svg = this.svgTemplate(color); + const blob = new Blob([svg], {type: 'image/svg+xml'}); + const url = URL.createObjectURL(blob); + + let favicon = document.querySelector("link[rel*='icon']") as HTMLLinkElement; + if (!favicon) { + favicon = document.createElement('link'); + favicon.rel = 'icon'; + document.head.appendChild(favicon); + } + + favicon.type = 'image/svg+xml'; + favicon.href = url; + } +} diff --git a/booklore-ui/src/app/layout/component/theme-configurator/theme-configurator.component.ts b/booklore-ui/src/app/layout/component/theme-configurator/theme-configurator.component.ts index c5bb03c2a..04667b4ab 100644 --- a/booklore-ui/src/app/layout/component/theme-configurator/theme-configurator.component.ts +++ b/booklore-ui/src/app/layout/component/theme-configurator/theme-configurator.component.ts @@ -1,5 +1,5 @@ import {CommonModule, isPlatformBrowser} from '@angular/common'; -import {Component, computed, inject, OnInit, PLATFORM_ID} from '@angular/core'; +import {Component, computed, effect, inject, OnInit, PLATFORM_ID} from '@angular/core'; import {FormsModule} from '@angular/forms'; import {$t, updatePreset, updateSurfacePalette} from '@primeng/themes'; import Aura from '@primeng/themes/aura'; @@ -9,6 +9,7 @@ import {InputSwitchModule} from 'primeng/inputswitch'; import {RadioButtonModule} from 'primeng/radiobutton'; import {ToggleSwitchModule} from 'primeng/toggleswitch'; import {AppConfigService} from '../../../core/service/app-config.service'; +import {FaviconService} from './favicon-service'; type ColorPalette = Record; @@ -30,6 +31,7 @@ export class ThemeConfiguratorComponent implements OnInit { readonly config = inject(PrimeNG); readonly configService = inject(AppConfigService); readonly platformId = inject(PLATFORM_ID); + readonly faviconService = inject(FaviconService); readonly surfaces: Palette[] = [ { @@ -172,11 +174,21 @@ export class ThemeConfiguratorComponent implements OnInit { readonly selectedPrimaryColor = computed(() => this.configService.appState().primary); readonly selectedSurfaceColor = computed(() => this.configService.appState().surface); + readonly faviconColor = computed(() => { + const name = this.selectedPrimaryColor() ?? 'green'; + const presetPalette = (Aura.primitive ?? {}) as Record; + const colorPalette = presetPalette[name]; + return colorPalette?.[500] ?? name; + }); + + private readonly _faviconSyncEffect = effect(() => { + this.faviconService.updateFavicon(this.faviconColor()); + }); readonly primaryColors = computed(() => { const presetPalette = (Aura.primitive ?? {}) as Record; const colors = ['emerald', 'green', 'lime', 'orange', 'amber', 'yellow', 'teal', 'cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 'fuchsia', 'pink', 'rose']; - return [{ name: 'noir', palette: {} }].concat( + return [{name: 'noir', palette: {}}].concat( colors.map(name => ({ name, palette: presetPalette[name] ?? {} @@ -198,7 +210,7 @@ export class ThemeConfiguratorComponent implements OnInit { if (color.name === 'noir') { return { semantic: { - primary: { ...(surface?.palette ?? {}) }, + primary: {...(surface?.palette ?? {})}, colorScheme: { dark: { primary: { @@ -262,6 +274,6 @@ export class ThemeConfiguratorComponent implements OnInit { onPresetChange(): void { const surface = this.surfaces.find(s => s.name === this.selectedSurfaceColor()); const surfacePalette = surface?.palette ?? {}; - $t().preset(Aura).preset(this.getPresetExt()).surfacePalette(surfacePalette).use({ useDefaultOptions: true }); + $t().preset(Aura).preset(this.getPresetExt()).surfacePalette(surfacePalette).use({useDefaultOptions: true}); } }