Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 SunCalc from 'suncalc' import { isBefore, isAfter } from 'date-fns' export const state = () => ({ current: 'LIGHT', }) export const getters = { current: state => state.current, resolveAuto: (_, __, rootState) => { const { latitude, longitude } = rootState.location const now = rootState.now const { sunrise, sunset } = SunCalc.getTimes(now, latitude, longitude) if (isBefore(now, sunset) && isAfter(now, sunrise)) { return 'LIGHT' } else { return 'DARK' } }, } export const mutations = { SET: (state, newTheme) => { state.current = newTheme }, } export const actions = { async set({ commit, dispatch, rootState, rootGetters, getters }) { // Load settings await dispatch('settings/load', null, { root: true }) // Get theme setting let theme = rootGetters['settings/value']('theme') // If the theme is 'AUTO', further processing is required. if (theme === 'AUTO') { // Acquire geolocation await dispatch('acquireLocation', null, { root: true }) // Resolve the theme theme = getters.resolveAuto } // Set the theme commit('SET', theme) // Attach the theme to <body> document.body.setAttribute('theme', theme) }, } |