-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththemesStore.ts
48 lines (47 loc) · 1.24 KB
/
themesStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { defineStore } from 'pinia';
import type { Theme } from '@/types/Theme'
import allThemes from '~/assets/tailwind/themes';
export const useThemesStore = defineStore({
id: 'themesStore',
state: () => ({
theme: '' as keyof typeof allThemes,
color: '',
}),
getters: {
currentTheme(state) {
return state.theme;
},
currentColor(state) {
return state.color;
},
getAllThemes() {
return Object.keys(allThemes)
}
},
actions: {
setTheme(themeName: keyof typeof allThemes) {
this.removeThemeProperties();
this.theme = themeName;
this.color = 'primary';
this.addThemeProperties();
},
setColor(colorName: string) {
this.color = colorName;
},
removeThemeProperties() {
if (!this.theme) return;
const props: Theme = allThemes[this.theme];
const root = document.documentElement;
Object.keys(props).forEach((property) => {
root.style.removeProperty(property);
});
},
addThemeProperties() {
const props: Theme = allThemes[this.theme];
const root = document.documentElement;
Object.keys(props).forEach((property) => {
root.style.setProperty(property, props[property]);
});
}
},
});