Sync favicon color with primary theme

This commit is contained in:
aditya.chandel
2025-06-28 18:19:10 -06:00
committed by Aditya Chandel
parent 66e3c68b81
commit bba3309310
2 changed files with 43 additions and 4 deletions
@@ -0,0 +1,27 @@
import {Injectable} from '@angular/core';
@Injectable({providedIn: 'root'})
export class FaviconService {
private svgTemplate = (color: string) => `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 126 126" fill="currentColor">
<path d="M59 4.79297C71.5051 11.5557 80 24.7854 80 40C80 40.5959 79.987 41.1888 79.9609 41.7783C79.8609 44.0406 81.7355 46 84 46C106.091 46 124 63.9086 124 86C124 108.091 106.091 126 84 126H10C4.47715 126 0 121.523 0 116V39.0068L0.0126953 38.9941C0.357624 25.0252 7.86506 12.8347 19 5.95215V63.832C19 64.8345 20.0676 65.4391 20.9121 64.9902L21.0771 64.8867L38.2227 52.3428C38.6819 52.0068 39.3064 52.0068 39.7656 52.3428L56.9229 64.8945L57.0879 64.998C57.9324 65.447 59 64.8423 59 63.8398V4.79297Z" fill="${color}"/>
<path d="M40 0C43.8745 0 47.6199 0.552381 51.1631 1.58008V50.9697L44.3926 46.0176L44.0879 45.8037C40.9061 43.6679 36.7098 43.7393 33.5957 46.0176L26.8369 50.9619V2.21875C30.9593 0.782634 35.3881 0 40 0Z" fill="white"/>
</svg>
`;
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;
}
}
@@ -1,5 +1,5 @@
import {CommonModule, isPlatformBrowser} from '@angular/common'; 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 {FormsModule} from '@angular/forms';
import {$t, updatePreset, updateSurfacePalette} from '@primeng/themes'; import {$t, updatePreset, updateSurfacePalette} from '@primeng/themes';
import Aura from '@primeng/themes/aura'; import Aura from '@primeng/themes/aura';
@@ -9,6 +9,7 @@ import {InputSwitchModule} from 'primeng/inputswitch';
import {RadioButtonModule} from 'primeng/radiobutton'; import {RadioButtonModule} from 'primeng/radiobutton';
import {ToggleSwitchModule} from 'primeng/toggleswitch'; import {ToggleSwitchModule} from 'primeng/toggleswitch';
import {AppConfigService} from '../../../core/service/app-config.service'; import {AppConfigService} from '../../../core/service/app-config.service';
import {FaviconService} from './favicon-service';
type ColorPalette = Record<string, string>; type ColorPalette = Record<string, string>;
@@ -30,6 +31,7 @@ export class ThemeConfiguratorComponent implements OnInit {
readonly config = inject(PrimeNG); readonly config = inject(PrimeNG);
readonly configService = inject(AppConfigService); readonly configService = inject(AppConfigService);
readonly platformId = inject(PLATFORM_ID); readonly platformId = inject(PLATFORM_ID);
readonly faviconService = inject(FaviconService);
readonly surfaces: Palette[] = [ readonly surfaces: Palette[] = [
{ {
@@ -172,11 +174,21 @@ export class ThemeConfiguratorComponent implements OnInit {
readonly selectedPrimaryColor = computed(() => this.configService.appState().primary); readonly selectedPrimaryColor = computed(() => this.configService.appState().primary);
readonly selectedSurfaceColor = computed(() => this.configService.appState().surface); readonly selectedSurfaceColor = computed(() => this.configService.appState().surface);
readonly faviconColor = computed(() => {
const name = this.selectedPrimaryColor() ?? 'green';
const presetPalette = (Aura.primitive ?? {}) as Record<string, ColorPalette>;
const colorPalette = presetPalette[name];
return colorPalette?.[500] ?? name;
});
private readonly _faviconSyncEffect = effect(() => {
this.faviconService.updateFavicon(this.faviconColor());
});
readonly primaryColors = computed<Palette[]>(() => { readonly primaryColors = computed<Palette[]>(() => {
const presetPalette = (Aura.primitive ?? {}) as Record<string, ColorPalette>; const presetPalette = (Aura.primitive ?? {}) as Record<string, ColorPalette>;
const colors = ['emerald', 'green', 'lime', 'orange', 'amber', 'yellow', 'teal', 'cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 'fuchsia', 'pink', 'rose']; 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 => ({ colors.map(name => ({
name, name,
palette: presetPalette[name] ?? {} palette: presetPalette[name] ?? {}
@@ -198,7 +210,7 @@ export class ThemeConfiguratorComponent implements OnInit {
if (color.name === 'noir') { if (color.name === 'noir') {
return { return {
semantic: { semantic: {
primary: { ...(surface?.palette ?? {}) }, primary: {...(surface?.palette ?? {})},
colorScheme: { colorScheme: {
dark: { dark: {
primary: { primary: {
@@ -262,6 +274,6 @@ export class ThemeConfiguratorComponent implements OnInit {
onPresetChange(): void { onPresetChange(): void {
const surface = this.surfaces.find(s => s.name === this.selectedSurfaceColor()); const surface = this.surfaces.find(s => s.name === this.selectedSurfaceColor());
const surfacePalette = surface?.palette ?? {}; 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});
} }
} }