1- import { get } from "svelte/store" ;
2- import {
3- derivedMode ,
4- derivedTheme ,
5- disableTransitions ,
6- modeStorageKey ,
7- systemPrefersMode ,
8- themeColors ,
9- themeStorageKey ,
10- theme as themeStore ,
11- userPrefersMode ,
12- } from "./stores.js" ;
1+ import { derivedMode , customTheme , userPrefersMode } from "./states.svelte.js" ;
132import type { Mode , ThemeColors } from "./types.js" ;
143
154/** Toggle between light and dark mode */
165export function toggleMode ( ) : void {
17- userPrefersMode . set ( get ( derivedMode ) === "dark" ? "light" : "dark" ) ;
6+ userPrefersMode . current = derivedMode . current === "dark" ? "light" : "dark" ;
187}
198
209/** Set the mode to light or dark */
2110export function setMode ( mode : Mode ) : void {
22- userPrefersMode . set ( mode ) ;
11+ userPrefersMode . current = mode ;
2312}
2413
2514/** Reset the mode to operating system preference */
2615export function resetMode ( ) : void {
27- userPrefersMode . set ( "system" ) ;
16+ userPrefersMode . current = "system" ;
2817}
2918
3019/** Set the theme to a custom value */
31- export function setTheme ( theme : string ) : void {
32- themeStore . set ( theme ) ;
20+ export function setTheme ( newTheme : string ) : void {
21+ customTheme . current = newTheme ;
3322}
3423
3524export function defineConfig ( config : SetInitialModeArgs ) {
@@ -46,63 +35,58 @@ type SetInitialModeArgs = {
4635 themeStorageKey ?: string ;
4736} ;
4837
38+ // @ts -expect-error - this is fine
39+ // prettier-ignore
40+ export function setInitialMode ( a ) { const { defaultMode :b = "system" , themeColors :c , darkClassNames :d = [ "dark" ] , lightClassNames :e = [ ] , defaultTheme :f = "" , modeStorageKey :g = "mode-watcher-mode" , themeStorageKey :h = "mode-watcher-theme" } = a || { } ; const k = document . documentElement , l = localStorage . getItem ( g ) ?? b , m = localStorage . getItem ( h ) ?? f , n = l == "light" || ( l == "system" && matchMedia ( "(prefers-color-scheme: light)" ) . matches ) , p = ( q , r ) => q . length && k . classList [ r ] ( ...q ) ; if ( n ) { p ( d , "remove" ) ; p ( e , "add" ) } else { p ( e , "remove" ) ; p ( d , "add" ) } k . style . colorScheme = n ?"light" :"dark" ; let s ; if ( c && ( s = document . querySelector ( 'meta[name="theme-color"]' ) ) ) s . setAttribute ( "content" , n ?c . light :c . dark ) ; if ( m ) { k . setAttribute ( "data-theme" , m ) ; localStorage . setItem ( h , m ) } localStorage . setItem ( g , l ) }
41+
42+ // The below code is minified above to reduce client bundle size when stringifying this into a
43+ // script tag.
4944/** Used to set the mode on initial page load to prevent FOUC */
50- export function setInitialMode ( {
51- defaultMode = "system" ,
52- themeColors,
53- darkClassNames = [ "dark" ] ,
54- lightClassNames = [ ] ,
55- defaultTheme = "" ,
56- modeStorageKey = "mode-watcher-mode" ,
57- themeStorageKey = "mode-watcher-theme" ,
58- } : SetInitialModeArgs ) {
59- const rootEl = document . documentElement ;
60- const mode = localStorage . getItem ( modeStorageKey ) || defaultMode ;
61- const theme = localStorage . getItem ( themeStorageKey ) || defaultTheme ;
62- const light =
63- mode === "light" ||
64- ( mode === "system" && window . matchMedia ( "(prefers-color-scheme: light)" ) . matches ) ;
65- if ( light ) {
66- if ( darkClassNames . length ) rootEl . classList . remove ( ...darkClassNames ) ;
67- if ( lightClassNames . length ) rootEl . classList . add ( ...lightClassNames ) ;
68- } else {
69- if ( lightClassNames . length ) rootEl . classList . remove ( ...lightClassNames ) ;
70- if ( darkClassNames . length ) rootEl . classList . add ( ...darkClassNames ) ;
71- }
72- rootEl . style . colorScheme = light ? "light" : "dark" ;
45+ // export function setInitialMode({
46+ // defaultMode = "system",
47+ // themeColors,
48+ // darkClassNames = ["dark"],
49+ // lightClassNames = [],
50+ // defaultTheme = "",
51+ // modeStorageKey = "mode-watcher-mode",
52+ // themeStorageKey = "mode-watcher-theme",
53+ // }: SetInitialModeArgs) {
54+ // const rootEl = document.documentElement;
55+ // const mode = localStorage.getItem(modeStorageKey) ?? defaultMode;
56+ // const theme = localStorage.getItem(themeStorageKey) ?? defaultTheme;
57+ // const light =
58+ // mode === "light" ||
59+ // (mode === "system" && window.matchMedia("(prefers-color-scheme: light)").matches);
60+ // if (light) {
61+ // if (darkClassNames.length) rootEl.classList.remove(...darkClassNames);
62+ // if (lightClassNames.length) rootEl.classList.add(...lightClassNames);
63+ // } else {
64+ // if (lightClassNames.length) rootEl.classList.remove(...lightClassNames);
65+ // if (darkClassNames.length) rootEl.classList.add(...darkClassNames);
66+ // }
67+ // rootEl.style.colorScheme = light ? "light" : "dark";
7368
74- if ( themeColors ) {
75- const themeMetaEl = document . querySelector ( 'meta[name="theme-color"]' ) ;
76- if ( themeMetaEl ) {
77- themeMetaEl . setAttribute (
78- "content" ,
79- mode === "light" ? themeColors . light : themeColors . dark
80- ) ;
81- }
82- }
69+ // if (themeColors) {
70+ // const themeMetaEl = document.querySelector('meta[name="theme-color"]');
71+ // if (themeMetaEl) {
72+ // themeMetaEl.setAttribute(
73+ // "content",
74+ // mode === "light" ? themeColors.light : themeColors.dark
75+ // );
76+ // }
77+ // }
8378
84- if ( theme ) {
85- rootEl . setAttribute ( "data-theme" , theme ) ;
86- localStorage . setItem ( themeStorageKey , theme ) ;
87- }
79+ // if (theme) {
80+ // rootEl.setAttribute("data-theme", theme);
81+ // localStorage.setItem(themeStorageKey, theme);
82+ // }
8883
89- localStorage . setItem ( modeStorageKey , mode ) ;
90- }
84+ // localStorage.setItem(modeStorageKey, mode);
85+ // }
9186
9287/**
9388 * A type-safe way to generate the source expression used to set the initial mode and avoid FOUC.
9489 */
9590export function generateSetInitialModeExpression ( config : SetInitialModeArgs = { } ) : string {
9691 return `(${ setInitialMode . toString ( ) } )(${ JSON . stringify ( config ) } );` ;
9792}
98-
99- export {
100- modeStorageKey ,
101- themeStorageKey ,
102- derivedTheme as theme ,
103- userPrefersMode ,
104- systemPrefersMode ,
105- derivedMode as mode ,
106- themeColors ,
107- disableTransitions ,
108- } ;
0 commit comments