Skip to content

Commit 5236571

Browse files
committed
fix(ThemeWrapper): set context on init, ref #34
1 parent 957af61 commit 5236571

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

components/ThemeWrapper.svelte

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
import { presets } from './presets'
1717
import toggle from '../support/toggle'
1818
import createCSS from '../support/createCSS'
19-
import { currentThemeName, currentThemeObject, themes as themesStore } from '../support/store'
19+
import {
20+
currentThemeName,
21+
currentThemeObject,
22+
themes as themesStore,
23+
} from '../support/store'
2024
import isObject from '../support/isObject'
2125
2226
/**
@@ -33,7 +37,7 @@
3337
* Sets the specified theme as active
3438
* @type {string | null} [theme='dark']
3539
*/
36-
export let theme = null;
40+
export let theme = null
3741
/**
3842
* Specify custom CSS variable prefix
3943
* @type {string | null} [prefix='theme']
@@ -50,23 +54,27 @@
5054
*/
5155
export let base = {}
5256
53-
if (!isObject(themes) || !Object.keys(themes).length) throw new Error(INVALID_THEMES_MESSAGE)
54-
if (typeof prefix === 'string' && !prefix.trim().length) throw new Error(INVALID_PREFIX_MESSAGE)
57+
if (!isObject(themes) || !Object.keys(themes).length)
58+
throw new Error(INVALID_THEMES_MESSAGE)
59+
if (typeof prefix === 'string' && !prefix.trim().length)
60+
throw new Error(INVALID_PREFIX_MESSAGE)
5561
if (!VALID_MODES.includes(mode)) throw new Error(INVALID_MODE_MESSAGE)
5662
5763
themesStore.set(themes)
5864
const [fallback] = Object.keys(themes)
59-
$: setContext(CONTEXT_KEY, {
60-
current: currentThemeName,
61-
toggle: toggle,
62-
theme: currentThemeObject,
63-
})
64-
$: if (!Object.keys(themes).includes($currentThemeName)) currentThemeName.set(fallback)
65+
$: if (!Object.keys(themes).includes($currentThemeName))
66+
currentThemeName.set(fallback)
6567
$: currentThemeObject.set(themes[$currentThemeName])
6668
6769
// create CSS
6870
const style = createCSS(prefix, base, themes)
6971
72+
setContext(CONTEXT_KEY, {
73+
current: currentThemeName,
74+
toggle,
75+
theme: currentThemeName,
76+
})
77+
7078
onMount(() => {
7179
// detect dark mode
7280
const darkSchemeQuery = matchMedia('(prefers-color-scheme: dark)')
@@ -75,15 +83,16 @@
7583
// listen for media query status change
7684
darkSchemeQuery.addEventListener(
7785
'change',
78-
({ matches }) => mode === 'auto' && currentMode.set(matches ? 'dark' : 'light')
86+
({ matches }) =>
87+
mode === 'auto' && currentMode.set(matches ? 'dark' : 'light')
7988
)
8089
8190
// loading order: saved, prefers, fallback
8291
const saved = key ? localStorage && localStorage.getItem(key) : null
8392
8493
if (theme && themes[theme]) {
8594
currentThemeName.set(theme)
86-
} else if (saved && themes[saved]) {
95+
} else if (saved && themes[saved]) {
8796
currentThemeName.set(saved)
8897
} else {
8998
if (mode === 'auto' && preferredMode) {
@@ -95,11 +104,13 @@
95104
}
96105
}
97106
98-
return () => key && localStorage && localStorage.setItem(key, $currentThemeName)
107+
return () =>
108+
key && localStorage && localStorage.setItem(key, $currentThemeName)
99109
})
100110
101111
afterUpdate(() => {
102-
if (document) document.documentElement.setAttribute('theme', $currentThemeName)
112+
if (document)
113+
document.documentElement.setAttribute('theme', $currentThemeName)
103114
if (key && localStorage) localStorage.setItem(key, $currentThemeName)
104115
})
105116
</script>

0 commit comments

Comments
 (0)