1- import { COOKIE_KEY , DARK , LIGHT , MAX_AGE , SYSTEM , modes } from "../../constants" ;
1+ import { COOKIE_KEY , DARK , LIGHT , SYSTEM , modes } from "../../constants" ;
22import { ColorSchemePreference , Store , useStore } from "../../utils" ;
33import { useEffect } from "react" ;
44
55const useEffectMinify = useEffect ;
6+ const localStorageMinify = localStorage ;
67export interface CoreProps {
78 /** force apply CSS transition property to all the elements during theme switching. E.g., `all .3s` */
89 t ?: string ;
910}
1011
12+ /** Modify transition globally to avoid patched transitions */
1113const modifyTransition = ( documentMinify : Document , themeTransition = "none" ) => {
1214 const css = documentMinify . createElement ( "style" ) ;
1315 /** split by ';' to prevent CSS injection */
14- const transition = `transition: ${ themeTransition . split ( ";" ) [ 0 ] } !important;` ;
15- css . appendChild (
16- documentMinify . createTextNode (
17- `*{-webkit-${ transition } -moz-${ transition } -o-${ transition } -ms-${ transition } ${ transition } }` ,
18- ) ,
19- ) ;
20- documentMinify . head . appendChild ( css ) ;
16+ css . textContent = `*{transition:${ themeTransition . split ( ";" ) [ 0 ] } !important;}` ;
17+ const head = documentMinify . head ;
18+ head . appendChild ( css ) ;
2119
2220 return ( ) => {
2321 // Force restyle
2422 getComputedStyle ( documentMinify . body ) ;
2523 // Wait for next tick before removing
26- setTimeout ( ( ) => {
27- documentMinify . head . removeChild ( css ) ;
28- } , 1 ) ;
24+ setTimeout ( ( ) => head . removeChild ( css ) , 1 ) ;
2925 } ;
3026} ;
3127
3228/**
33- *
29+ * The Core component wich applies classes and transitions.
30+ * Cookies are set only if corresponding ServerTarget is detected.
3431 *
3532 * @example
3633 * ```tsx
37- * <Core />
34+ * <Core [t="background-color .3s"] />
3835 * ```
3936 *
4037 * @source - Source code
@@ -53,7 +50,7 @@ export const Core = ({ t }: CoreProps) => {
5350
5451 setThemeState ( state => ( {
5552 ...state ,
56- m : ( localStorage . getItem ( COOKIE_KEY ) ?? SYSTEM ) as ColorSchemePreference ,
53+ m : ( localStorageMinify ? .getItem ( COOKIE_KEY ) ?? SYSTEM ) as ColorSchemePreference ,
5754 } ) ) ;
5855 /** Sync the tabs */
5956 const storageListener = ( e : StorageEvent ) : void => {
@@ -83,10 +80,10 @@ export const Core = ({ t }: CoreProps) => {
8380 } ) ;
8481 restoreTransitions ( ) ;
8582 // System mode is decided by current system state and need not be stored in localStorage
86- localStorage . setItem ( COOKIE_KEY , mode ) ;
83+ localStorageMinify ? .setItem ( COOKIE_KEY , mode ) ;
8784 if ( serverTargetEl )
88- documentMinify . cookie = `${ COOKIE_KEY } =${ resolvedMode } ;max-age=${ MAX_AGE } ;SameSite=Strict;` ;
89- } , [ resolvedMode , systemMode , mode ] ) ;
85+ documentMinify . cookie = `${ COOKIE_KEY } =${ resolvedMode } ;max-age=31536000 ;SameSite=Strict;` ;
86+ } , [ resolvedMode , systemMode , mode , t ] ) ;
9087
9188 return null ;
9289} ;
0 commit comments