2024-10-24 10:07:36 +02:00
|
|
|
import {
|
|
|
|
Constants,
|
|
|
|
createLocalStorageStore,
|
|
|
|
ensureValidTheme,
|
|
|
|
getThemeClassNames,
|
|
|
|
} from "@budibase/frontend-core"
|
|
|
|
import { derived } from "svelte/store"
|
2020-10-30 14:23:49 +01:00
|
|
|
|
|
|
|
export const getThemeStore = () => {
|
2024-10-24 10:07:36 +02:00
|
|
|
const defaultBuilderTheme = Constants.Themes.Darkest
|
2020-10-30 14:23:49 +01:00
|
|
|
const themeElement = document.documentElement
|
|
|
|
const initialValue = {
|
2024-10-24 10:07:36 +02:00
|
|
|
theme: defaultBuilderTheme,
|
2020-10-30 14:23:49 +01:00
|
|
|
}
|
2022-01-20 12:19:37 +01:00
|
|
|
const store = createLocalStorageStore("bb-theme", initialValue)
|
2024-10-24 10:07:36 +02:00
|
|
|
const derivedStore = derived(store, $store => ({
|
|
|
|
...$store,
|
|
|
|
theme: ensureValidTheme($store.theme, defaultBuilderTheme),
|
|
|
|
}))
|
2020-10-30 14:23:49 +01:00
|
|
|
|
2021-04-27 14:15:49 +02:00
|
|
|
// Update theme class when store changes
|
2024-10-24 10:07:36 +02:00
|
|
|
derivedStore.subscribe(({ theme }) => {
|
|
|
|
const classNames = getThemeClassNames(theme).split(" ")
|
|
|
|
Constants.ThemeOptions.forEach(option => {
|
|
|
|
const className = `${Constants.ThemeClassPrefix}${option.id}`
|
|
|
|
themeElement.classList.toggle(className, classNames.includes(className))
|
2021-04-27 14:15:49 +02:00
|
|
|
})
|
2020-10-30 14:23:49 +01:00
|
|
|
})
|
|
|
|
|
2024-10-24 10:07:36 +02:00
|
|
|
return {
|
|
|
|
...store,
|
|
|
|
subscribe: derivedStore.subscribe,
|
|
|
|
}
|
2020-10-30 14:23:49 +01:00
|
|
|
}
|
2023-10-30 13:46:44 +01:00
|
|
|
|
|
|
|
export const themeStore = getThemeStore()
|