-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable mode-watcher to manage the theme-color meta tag (#48)
Co-authored-by: Hunter Johnston <[email protected]>
- Loading branch information
Showing
20 changed files
with
285 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'mode-watcher': minor | ||
--- | ||
|
||
Allow `mode-watcher` to manage the theme-color meta tag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,48 @@ | ||
<script lang="ts"> | ||
import { onMount } from 'svelte'; | ||
import { systemPrefersMode, setMode, localStorageKey, mode } from './mode'; | ||
import { | ||
systemPrefersMode, | ||
setMode, | ||
localStorageKey, | ||
mode, | ||
themeColors as themeColorsStore, | ||
setInitialMode, | ||
} from './mode.js'; | ||
import type { Mode, ThemeColors } from './types.js'; | ||
import { isValidMode } from './stores.js'; | ||
export let track = true; | ||
export let defaultMode: 'light' | 'dark' | 'system' = 'system'; | ||
export let defaultMode: Mode = 'system'; | ||
export let themeColors: ThemeColors = undefined; | ||
themeColorsStore.set(themeColors); | ||
onMount(() => { | ||
const unsubscriber = mode.subscribe(() => {}); | ||
systemPrefersMode.tracking(track); | ||
systemPrefersMode.query(); | ||
setMode((localStorage.getItem(localStorageKey) as 'dark' | 'light' | 'system') || defaultMode); | ||
const localStorageMode = localStorage.getItem(localStorageKey); | ||
setMode(isValidMode(localStorageMode) ? localStorageMode : defaultMode); | ||
return () => { | ||
unsubscriber(); | ||
}; | ||
}); | ||
function setInitialMode() { | ||
const elem = document.documentElement, | ||
mode = localStorage.getItem('mode') || '<DEFAULT_MODE>', | ||
light = | ||
mode === 'light' || | ||
(mode === 'system' && window.matchMedia('(prefers-color-scheme: light)').matches); | ||
elem.classList[light ? 'remove' : 'add']('dark'); | ||
elem.style.colorScheme = light ? 'light' : 'dark'; | ||
localStorage.setItem('mode', mode); | ||
} | ||
const stringified = setInitialMode.toString().replace('<DEFAULT_MODE>', defaultMode); | ||
const args = `"${defaultMode}"${themeColors ? `, ${JSON.stringify(themeColors)}` : ''}`; | ||
</script> | ||
|
||
<svelte:head> | ||
{#if themeColors} | ||
<!-- default to dark mode for to allow testing --> | ||
<!-- this will be overwritten by FOUC prevention snippet below --> | ||
<!-- but that snippet does not run in vitest --> | ||
<meta name="theme-color" content={themeColors.dark} /> | ||
{/if} | ||
<!-- eslint-disable-next-line svelte/no-at-html-tags --> | ||
{@html `<script nonce="%sveltekit.nonce%">(` + stringified + `)();</script>`} | ||
{@html `<script nonce="%sveltekit.nonce%">(` + | ||
setInitialMode.toString() + | ||
`)(` + | ||
args + | ||
`);</script>`} | ||
</svelte:head> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.