11import React from "react" ;
2+ import { default as UnencryptedCookies } from 'js-cookie' ;
3+ import { RootLoaderData } from "~/types/RootLoaderData" ;
4+ import { useRouteLoaderData } from "@remix-run/react" ;
25
3- function getColorScheme ( ) {
6+ function getColorScheme ( defaultTheme ?: "light" | "dark" ) {
47
58 if ( typeof window === 'undefined' ) {
6- return 'light' ;
9+ defaultTheme = defaultTheme || 'light' ;
10+ console . log ( "getColorScheme: window is undefined: " , defaultTheme ) ;
11+ return defaultTheme ;
712 }
813
914 if ( document . documentElement . hasAttribute ( 'data-bs-theme' ) ) {
15+ console . log ( "getColorScheme: data-bs-theme: " , document . documentElement . getAttribute ( 'data-bs-theme' ) ) ;
1016 return document . documentElement . getAttribute ( 'data-bs-theme' ) ;
1117 }
1218
1319 if ( window . matchMedia && window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) {
20+ console . log ( "getColorScheme: dark" ) ;
1421 return 'dark' ;
1522 } else {
23+ console . log ( "getColorScheme: light" ) ;
1624 return 'light' ;
1725 }
1826}
1927
2028export function ColorSchemeToggle ( ) {
21- const [ currentScheme , setColorScheme ] = React . useState ( getColorScheme ( ) ) ;
29+ const { theme } = useRouteLoaderData < RootLoaderData > ( "root" ) as unknown as RootLoaderData ;
30+ const [ currentScheme , setColorScheme ] = React . useState ( getColorScheme ( theme ) ) ;
2231
2332 const onClick = ( scheme : 'light' | 'dark' | 'auto' ) => {
2433 if ( scheme == 'auto' ) {
@@ -28,6 +37,8 @@ export function ColorSchemeToggle() {
2837 scheme = 'light' ;
2938 }
3039 }
40+ UnencryptedCookies . set ( 'color-theme' , scheme , { path : '/' } ) ;
41+ localStorage . setItem ( 'color-theme' , scheme ) ;
3142 document . documentElement . setAttribute ( 'data-bs-theme' , scheme ) ;
3243 setColorScheme ( scheme ) ;
3344 }
0 commit comments