Files
micro-frontend/ng-ttc-frontend/src/app/services/theme.service.ts
x2Skyz f0f3392dbb
All checks were successful
Build Docker Image / Build Docker Image (push) Successful in 6m57s
Build Docker Image / Restart Docker Compose (push) Successful in 1s
-theme demo
2025-11-29 12:54:41 +07:00

42 lines
1.2 KiB
TypeScript

import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ThemeService {
private currentTheme: string = 'theme-red'; // ธีมเริ่มต้น
constructor() {
this.loadTheme();
}
// โหลดธีมจาก LocalStorage หรือใช้ค่าเริ่มต้น
private loadTheme() {
const savedTheme = localStorage.getItem('app-theme');
if (savedTheme) {
this.currentTheme = savedTheme;
}
this.applyTheme(this.currentTheme);
}
// ฟังก์ชันเปลี่ยนธีม
setTheme(themeName: string) {
this.currentTheme = themeName;
localStorage.setItem('app-theme', themeName);
this.applyTheme(themeName);
}
// อัปเดต Class ที่ <html> tag
private applyTheme(theme: string) {
// ลบธีมเก่าออกทั้งหมด (สมมติมี theme-red, theme-blue, theme-green)
document.documentElement.classList.remove('theme-red', 'theme-blue', 'theme-green');
// เพิ่มธีมใหม่
document.documentElement.classList.add(theme);
}
getCurrentTheme() {
return this.currentTheme;
}
}