budibase/packages/builder/src/builderStore/store/theme.js

29 lines
727 B
JavaScript
Raw Normal View History

import { localStorageStore } from "./localStorage"
export const getThemeStore = () => {
const themeElement = document.documentElement
const initialValue = {
theme: "darkest",
options: ["lightest", "light", "dark", "darkest"],
}
const store = localStorageStore("bb-theme", initialValue)
// Update theme class when store changes
2021-05-03 09:31:09 +02:00
store.subscribe((state) => {
// Handle any old local storage values - this can be removed after the update
if (state.darkMode !== undefined) {
store.set(initialValue)
return
}
2021-05-03 09:31:09 +02:00
state.options.forEach((option) => {
themeElement.classList.toggle(
`spectrum--${option}`,
option === state.theme
)
})
})
return store
}