2021-09-02 12:38:41 +02:00
|
|
|
import { derived } from "svelte/store"
|
|
|
|
import { appStore } from "./app"
|
|
|
|
import { builderStore } from "./builder"
|
|
|
|
|
2021-09-03 15:43:21 +02:00
|
|
|
// This is the good old acorn bug where having the word "g l o b a l" makes it
|
|
|
|
// think that this is not ES6 compatible and starts throwing errors when using
|
|
|
|
// optional chaining. Piss off acorn.
|
|
|
|
const defaultTheme = "spectrum--light"
|
|
|
|
const defaultCustomTheme = {
|
|
|
|
primaryColor: "var(--spectrum-glo" + "bal-color-blue-600)",
|
|
|
|
primaryColorHover: "var(--spectrum-glo" + "bal-color-blue-500)",
|
2021-09-03 16:28:35 +02:00
|
|
|
buttonBorderRadius: "16px",
|
|
|
|
navBackground: "var(--spectrum-glo" + "bal-color-gray-100)",
|
2021-09-06 17:23:12 +02:00
|
|
|
navTextColor: "var(--spectrum-glo" + "bal-color-gray-800)",
|
2021-09-03 15:43:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:38:41 +02:00
|
|
|
const createThemeStore = () => {
|
|
|
|
const store = derived(
|
|
|
|
[builderStore, appStore],
|
|
|
|
([$builderStore, $appStore]) => {
|
2021-09-06 17:51:40 +02:00
|
|
|
let theme = $appStore.application?.theme
|
|
|
|
let customTheme = $appStore.application?.customTheme
|
|
|
|
if ($builderStore.inBuilder) {
|
|
|
|
theme = $builderStore.theme
|
|
|
|
customTheme = $builderStore.customTheme
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure theme is set
|
|
|
|
theme = theme || defaultTheme
|
2021-09-03 16:28:35 +02:00
|
|
|
|
|
|
|
// Delete and nullish keys from the custom theme
|
|
|
|
if (customTheme) {
|
|
|
|
Object.entries(customTheme).forEach(([key, value]) => {
|
|
|
|
if (value == null || value === "") {
|
|
|
|
delete customTheme[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge custom theme with defaults
|
|
|
|
customTheme = {
|
2021-09-03 15:43:21 +02:00
|
|
|
...defaultCustomTheme,
|
2021-09-03 16:28:35 +02:00
|
|
|
...customTheme,
|
2021-09-03 15:43:21 +02:00
|
|
|
}
|
2021-09-03 16:28:35 +02:00
|
|
|
|
|
|
|
// Build CSS string setting all custom variables
|
2021-09-02 12:38:41 +02:00
|
|
|
let customThemeCss = ""
|
|
|
|
Object.entries(customTheme).forEach(([key, value]) => {
|
|
|
|
customThemeCss += `--${key}:${value};`
|
|
|
|
})
|
2021-09-03 16:28:35 +02:00
|
|
|
|
2021-09-02 12:38:41 +02:00
|
|
|
return {
|
|
|
|
theme,
|
|
|
|
customTheme,
|
|
|
|
customThemeCss,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const themeStore = createThemeStore()
|