Add theme switching

More reactivity in Library Browser
This commit is contained in:
aditya.chandel
2024-12-18 11:34:24 -07:00
parent 61e9580409
commit 71f5927364
24 changed files with 239 additions and 132 deletions
@@ -0,0 +1,11 @@
<div>
<p-dropdown
[options]="themes"
[(ngModel)]="currentTheme"
(onChange)="changeTheme($event)"
id="theme"
optionLabel="label"
optionValue="value"
placeholder="Choose a theme">
</p-dropdown>
</div>
@@ -0,0 +1,59 @@
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {DropdownChangeEvent, DropdownModule} from 'primeng/dropdown';
import {FormsModule} from '@angular/forms';
@Component({
selector: 'app-theme-switcher',
templateUrl: './theme-switcher.component.html',
standalone: true,
imports: [
FormsModule,
DropdownModule
],
styleUrl: './theme-switcher.component.scss'
})
export class ThemeSwitcherComponent implements OnInit {
themes: { value: string; label: string }[] = [];
currentTheme: string = '';
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.http
.get<{ value: string; label: string }[]>('assets/themes/themes.json')
.subscribe(
(data) => {
this.themes = data;
const savedTheme = localStorage.getItem('selected-theme');
if (savedTheme) {
this.currentTheme = savedTheme;
this.applyTheme(savedTheme);
} else if (this.themes.length > 0) {
this.currentTheme = this.themes[0].value;
this.applyTheme(this.currentTheme);
}
},
(error) => {
console.error('Error loading themes:', error);
}
);
}
changeTheme(event: DropdownChangeEvent) {
const selectedTheme = event.value;
this.currentTheme = selectedTheme;
this.applyTheme(selectedTheme);
localStorage.setItem('selected-theme', selectedTheme);
}
private applyTheme(theme: string) {
const themeLink = document.getElementById('theme-link') as HTMLLinkElement;
if (themeLink) {
themeLink.href = `assets/layout/styles/theme/${theme}/theme.css`;
console.log(`Theme applied: ${theme}`);
} else {
console.error('Theme link element not found!');
}
}
}