@@ -79,15 +79,17 @@ function modifyTransition(themeTransition = "none") {
79
79
export interface UpdateDOMProps {
80
80
targetId ?: string ;
81
81
themeState : ThemeState ;
82
+ dontSync ?: boolean ;
82
83
}
83
84
84
- function updateDOM ( { targetId, themeState } : UpdateDOMProps ) {
85
+ function updateDOM ( { targetId, themeState, dontSync } : UpdateDOMProps ) {
85
86
const { theme, colorSchemePreference : csp , systemColorScheme : scs } = themeState ;
86
87
const resolvedColorScheme = csp === "system" ? scs : csp ;
88
+ const key = targetId ?? DEFAULT_ID ;
87
89
// update DOM
88
90
let shoulCreateCookie = false ;
89
- const target = document . getElementById ( targetId ?? DEFAULT_ID ) ;
90
- shoulCreateCookie = target ?. getAttribute ( "data-nth" ) === "next" ;
91
+ const target = document . getElementById ( key ) ;
92
+ shoulCreateCookie = ! dontSync && target ?. getAttribute ( "data-nth" ) === "next" ;
91
93
92
94
/** do not update documentElement for local targets */
93
95
const targets = targetId ? [ target ] : [ target , document . documentElement ] ;
@@ -102,7 +104,8 @@ function updateDOM({ targetId, themeState }: UpdateDOMProps) {
102
104
t ?. classList . add ( resolvedColorScheme ) ;
103
105
} ) ;
104
106
105
- return shoulCreateCookie ;
107
+ if ( shoulCreateCookie )
108
+ document . cookie = `${ key } =${ theme } ,${ resolvedColorScheme } ; max-age=31536000; SameSite=Strict;` ;
106
109
}
107
110
108
111
export function ThemeSwitcher ( { targetId, dontSync, themeTransition } : ThemeSwitcherProps ) {
@@ -116,16 +119,13 @@ export function ThemeSwitcher({ targetId, dontSync, themeTransition }: ThemeSwit
116
119
/** update DOM and storage */
117
120
React . useEffect ( ( ) => {
118
121
const restoreTransitions = modifyTransition ( themeTransition ) ;
119
- const shoulCreateCookie = updateDOM ( { targetId, themeState } ) ;
122
+ updateDOM ( { targetId, themeState, dontSync } ) ;
120
123
if ( ! dontSync ) {
121
124
// save to localStorage
122
125
const { theme, colorSchemePreference : csp , systemColorScheme : scs } = themeState ;
123
126
const stateToSave = [ theme , csp , scs ] . join ( "," ) ;
124
127
const key = targetId ?? DEFAULT_ID ;
125
128
localStorage . setItem ( key , stateToSave ) ;
126
- if ( shoulCreateCookie ) {
127
- document . cookie = `${ key } =${ stateToSave } ; max-age=31536000; SameSite=Strict;` ;
128
- }
129
129
}
130
130
restoreTransitions ( ) ;
131
131
} , [ dontSync , targetId , themeState , themeTransition ] ) ;
0 commit comments