mancala/src/theme/ThemeManager.ts

41 lines
954 B
TypeScript
Raw Normal View History

2022-07-13 15:40:54 +03:00
import lightTheme from "./LightTheme";
import greyTheme from "./GreyTheme";
2022-05-15 01:58:57 +03:00
import { Theme } from "./Theme";
2022-07-13 15:40:54 +03:00
import darkTheme from "./DarkTheme";
2022-05-15 01:58:57 +03:00
2022-07-13 15:40:54 +03:00
export const themes = [lightTheme, darkTheme, greyTheme];
2022-06-04 19:28:57 +03:00
const THEME_ID = "theme_id";
2022-05-15 01:58:57 +03:00
export default class ThemeManager {
_theme: Theme;
onThemeChange: (theme: Theme) => void;
2022-06-04 19:28:57 +03:00
constructor() {
2022-07-13 15:40:54 +03:00
this._theme = this.readFromLocalStorage() || lightTheme;
2022-05-15 01:58:57 +03:00
}
public get theme() {
return this._theme;
}
2022-06-04 19:28:57 +03:00
public set theme(value: Theme) {
2022-05-15 01:58:57 +03:00
this._theme = value;
this.onThemeChange?.(value);
2022-06-04 19:28:57 +03:00
this.writetToLocalStorage(value);
}
private writetToLocalStorage(value: Theme) {
localStorage.setItem(THEME_ID, value.id);
}
private readFromLocalStorage(): Theme {
const themeID = localStorage.getItem(THEME_ID);
const theme = themes.find((eachTheme: Theme) => themeID === eachTheme.id);
return theme;
}
public get themes(): Theme[] {
return themes;
2022-05-15 01:58:57 +03:00
}
}