-theme demo
This commit is contained in:
41
ng-ttc-frontend/src/app/services/theme.service.ts
Normal file
41
ng-ttc-frontend/src/app/services/theme.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user