11import { DARK , LIGHT } from "../../constants" ;
2- import { ColorSchemePreference , ResolvedScheme , Store , useStore } from "../../utils" ;
3- import { useEffect } from "react" ;
2+ import { ColorSchemePreference , ResolvedScheme , useStore } from "../../utils" ;
3+ import { memo , useEffect } from "react" ;
44
55declare global {
6+ // skipcq: JS-0102, JS-0239
67 var u : ( mode : ColorSchemePreference , systemMode : ResolvedScheme ) => void ;
8+ // skipcq: JS-0102, JS-0239
79 var m : MediaQueryList ;
810}
911
1012/** function to be injected in script tag for avoiding FOUC */
11- export const s = ( storageKey : string ) => {
13+ export const noFOUCScript = ( storageKey : string ) => {
1214 const [ SYSTEM , DARK ] = [ "system" , "dark" ] as const ;
1315 window . u = ( mode : ColorSchemePreference , systemMode : ResolvedScheme ) => {
1416 const resolvedMode = mode === SYSTEM ? systemMode : mode ;
@@ -30,6 +32,23 @@ export const s = (storageKey: string) => {
3032let media : MediaQueryList ,
3133 updateDOM : ( mode : ColorSchemePreference , systemMode : ResolvedScheme ) => void ;
3234
35+ interface ScriptProps {
36+ /** nonce */
37+ n ?: string ;
38+ /** storageKey */
39+ k : string ;
40+ }
41+
42+ /** Avoid rerender of script */
43+ const Script = memo ( ( { n, k } : ScriptProps ) => (
44+ < script
45+ suppressHydrationWarning
46+ // skipcq: JS-0440
47+ dangerouslySetInnerHTML = { { __html : `(${ noFOUCScript . toString ( ) } )('${ k } ')` } }
48+ nonce = { n }
49+ />
50+ ) ) ;
51+
3352export interface CoreProps {
3453 /** themeTransition: force apply CSS transition property to all the elements during theme switching. E.g., `all .3s`
3554 * @defaultValue 'none'
@@ -70,7 +89,7 @@ const modifyTransition = (themeTransition = "none", nonce = "") => {
7089 */
7190export const Core = ( { t, nonce, k = "o" } : CoreProps ) => {
7291 // handle client side exceptions when script is not run. <- for client side apps like vite or CRA
73- if ( typeof window !== "undefined" && ! window . m ) s ( k ) ;
92+ if ( typeof window !== "undefined" && ! window . m ) noFOUCScript ( k ) ;
7493
7594 const [ { m : mode , s : systemMode } , setThemeState ] = useStore ( ) ;
7695
@@ -79,7 +98,7 @@ export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
7998 [ media , updateDOM ] = [ m , u ] ;
8099 /** Updating media: prefers-color-scheme*/
81100 media . addEventListener ( "change" , ( ) =>
82- setThemeState ( state => ( { ...state , s : media . matches ? DARK : LIGHT } ) as Store ) ,
101+ setThemeState ( state => ( { ...state , s : media . matches ? DARK : LIGHT } ) ) ,
83102 ) ;
84103 /** Sync the tabs */
85104 addEventListener ( "storage" , ( e : StorageEvent ) : void => {
@@ -93,5 +112,5 @@ export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
93112 restoreTransitions ( ) ;
94113 } , [ systemMode , mode , t , nonce ] ) ;
95114
96- return < script dangerouslySetInnerHTML = { { __html : `( ${ s . toString ( ) } )(' ${ k } ')` } } nonce = { nonce } /> ;
115+ return < Script { ... { n : nonce , k } } /> ;
97116} ;
0 commit comments