|
16 | 16 | import { presets } from './presets'
|
17 | 17 | import toggle from '../support/toggle'
|
18 | 18 | 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' |
20 | 24 | import isObject from '../support/isObject'
|
21 | 25 |
|
22 | 26 | /**
|
|
33 | 37 | * Sets the specified theme as active
|
34 | 38 | * @type {string | null} [theme='dark']
|
35 | 39 | */
|
36 |
| - export let theme = null; |
| 40 | + export let theme = null |
37 | 41 | /**
|
38 | 42 | * Specify custom CSS variable prefix
|
39 | 43 | * @type {string | null} [prefix='theme']
|
|
50 | 54 | */
|
51 | 55 | export let base = {}
|
52 | 56 |
|
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) |
55 | 61 | if (!VALID_MODES.includes(mode)) throw new Error(INVALID_MODE_MESSAGE)
|
56 | 62 |
|
57 | 63 | themesStore.set(themes)
|
58 | 64 | 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) |
65 | 67 | $: currentThemeObject.set(themes[$currentThemeName])
|
66 | 68 |
|
67 | 69 | // create CSS
|
68 | 70 | const style = createCSS(prefix, base, themes)
|
69 | 71 |
|
| 72 | + setContext(CONTEXT_KEY, { |
| 73 | + current: currentThemeName, |
| 74 | + toggle, |
| 75 | + theme: currentThemeName, |
| 76 | + }) |
| 77 | +
|
70 | 78 | onMount(() => {
|
71 | 79 | // detect dark mode
|
72 | 80 | const darkSchemeQuery = matchMedia('(prefers-color-scheme: dark)')
|
|
75 | 83 | // listen for media query status change
|
76 | 84 | darkSchemeQuery.addEventListener(
|
77 | 85 | 'change',
|
78 |
| - ({ matches }) => mode === 'auto' && currentMode.set(matches ? 'dark' : 'light') |
| 86 | + ({ matches }) => |
| 87 | + mode === 'auto' && currentMode.set(matches ? 'dark' : 'light') |
79 | 88 | )
|
80 | 89 |
|
81 | 90 | // loading order: saved, prefers, fallback
|
82 | 91 | const saved = key ? localStorage && localStorage.getItem(key) : null
|
83 | 92 |
|
84 | 93 | if (theme && themes[theme]) {
|
85 | 94 | currentThemeName.set(theme)
|
86 |
| - } else if (saved && themes[saved]) { |
| 95 | + } else if (saved && themes[saved]) { |
87 | 96 | currentThemeName.set(saved)
|
88 | 97 | } else {
|
89 | 98 | if (mode === 'auto' && preferredMode) {
|
|
95 | 104 | }
|
96 | 105 | }
|
97 | 106 |
|
98 |
| - return () => key && localStorage && localStorage.setItem(key, $currentThemeName) |
| 107 | + return () => |
| 108 | + key && localStorage && localStorage.setItem(key, $currentThemeName) |
99 | 109 | })
|
100 | 110 |
|
101 | 111 | afterUpdate(() => {
|
102 |
| - if (document) document.documentElement.setAttribute('theme', $currentThemeName) |
| 112 | + if (document) |
| 113 | + document.documentElement.setAttribute('theme', $currentThemeName) |
103 | 114 | if (key && localStorage) localStorage.setItem(key, $currentThemeName)
|
104 | 115 | })
|
105 | 116 | </script>
|
|
0 commit comments