|
| 1 | +import { browser } from '$app/environment'; |
| 2 | +import { isString } from '$lib/typeguards'; |
| 3 | + |
| 4 | +export enum ColorScheme { |
| 5 | + Dark = 'dark', |
| 6 | + Light = 'light', |
| 7 | + System = 'system', |
| 8 | +} |
| 9 | + |
| 10 | +function isColorSchemeEnum(value: unknown): value is ColorScheme { |
| 11 | + if (!isString(value)) return false; |
| 12 | + return Object.values(ColorScheme).includes(value as ColorScheme); |
| 13 | +} |
| 14 | + |
| 15 | +function getLocalStoreState(): ColorScheme { |
| 16 | + // If we are not in a browser environment, return default |
| 17 | + if (!browser) { |
| 18 | + return ColorScheme.System; |
| 19 | + } |
| 20 | + |
| 21 | + // If the stored value is invalid, reset it and return default |
| 22 | + const scheme = localStorage.getItem('theme'); |
| 23 | + if (!isColorSchemeEnum(scheme)) { |
| 24 | + localStorage.setItem('theme', ColorScheme.System); |
| 25 | + return ColorScheme.System; |
| 26 | + } |
| 27 | + |
| 28 | + return scheme; |
| 29 | +} |
| 30 | + |
| 31 | +export function getDarkReaderState() { |
| 32 | + const rootHtml = document.documentElement; |
| 33 | + |
| 34 | + const proxyInjected = rootHtml.getAttribute('data-darkreader-proxy-injected'); |
| 35 | + const metaElement = rootHtml.querySelector('head meta[name="darkreader"]'); |
| 36 | + const scheme = rootHtml.getAttribute('data-darkreader-scheme'); |
| 37 | + |
| 38 | + return { |
| 39 | + isInjected: proxyInjected === 'true', |
| 40 | + isActive: metaElement !== null, |
| 41 | + scheme, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function resolveDarkMode(userPreference: ColorScheme): boolean { |
| 46 | + if (userPreference !== ColorScheme.System) { |
| 47 | + return userPreference !== ColorScheme.Light; |
| 48 | + } |
| 49 | + |
| 50 | + // If a user has Dark Reader extension installed, assume they prefer dark mode |
| 51 | + const darkReaderState = getDarkReaderState(); |
| 52 | + if (darkReaderState.isInjected) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + // Check if the user has a system theme preference for light mode |
| 57 | + if (window.matchMedia('(prefers-color-scheme: light)').matches) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + // Default to dark mode |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +function setDarkMode(preference: ColorScheme) { |
| 66 | + document.documentElement.classList.toggle('dark', resolveDarkMode(preference)); |
| 67 | +} |
| 68 | + |
| 69 | +class ColorSchemeState { |
| 70 | + #value; |
| 71 | + |
| 72 | + constructor() { |
| 73 | + this.#value = $state<ColorScheme>(getLocalStoreState()); |
| 74 | + } |
| 75 | + |
| 76 | + get Value() { |
| 77 | + return this.#value; |
| 78 | + } |
| 79 | + set Value(value: ColorScheme) { |
| 80 | + localStorage.setItem('theme', value); |
| 81 | + setDarkMode(value); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export const colorScheme = new ColorSchemeState(); |
| 86 | + |
| 87 | +function handleMediaQueryChange() { |
| 88 | + setDarkMode(getLocalStoreState()); |
| 89 | +} |
| 90 | +export function initializeDarkModeStore() { |
| 91 | + handleMediaQueryChange(); |
| 92 | + |
| 93 | + window |
| 94 | + .matchMedia('(prefers-color-scheme: light)') |
| 95 | + .addEventListener('change', handleMediaQueryChange); |
| 96 | + window |
| 97 | + .matchMedia('(prefers-color-scheme: dark)') |
| 98 | + .addEventListener('change', handleMediaQueryChange); |
| 99 | + |
| 100 | + window.addEventListener('storage', (event) => { |
| 101 | + if (event.key !== 'theme') return; |
| 102 | + |
| 103 | + setDarkMode(isColorSchemeEnum(event.newValue) ? event.newValue : ColorScheme.System); |
| 104 | + }); |
| 105 | +} |
0 commit comments