-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
34 lines (30 loc) · 879 Bytes
/
store.js
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
import { createStore } from 'vuex'
export default createStore({
state:{
theme: {}
},
getters:{
getTheme: (state) => {
return state.theme;
}
},
mutations: {
SET_THEME(state, theme) {
state.theme = theme;
localStorage.theme = theme;
}
},
actions:{
initTheme({ commit }) {
const cachedTheme = localStorage.theme ? localStorage.theme : false;
// `true` if the user has set theme to `dark` on browser/OS
const userPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (cachedTheme)
commit('SET_THEME', cachedTheme)
else if (userPrefersDark)
commit('SET_THEME', 'dark')
else
commit('SET_THEME', 'light')
},
}
})