Skip to content

Commit ed911f8

Browse files
committed
store global functions to local variables to avoid any interference
1 parent f69c487 commit ed911f8

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

lib/src/client/core/core.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import { COOKIE_KEY, DARK, LIGHT, MEDIA, SYSTEM, modes } from "../../constants";
2-
import { ColorSchemePreference, Store, useStore } from "../../utils";
1+
import { DARK, LIGHT } from "../../constants";
2+
import { ColorSchemePreference, ResolvedScheme, Store, useStore } from "../../utils";
33
import { useEffect } from "react";
44
import { s } from "./script";
55

6+
let media: MediaQueryList,
7+
updateDOM: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
8+
69
export interface CoreProps {
7-
/** force apply CSS transition property to all the elements during theme switching. E.g., `all .3s` */
10+
/** themeTransition: force apply CSS transition property to all the elements during theme switching. E.g., `all .3s`
11+
* @defaultValue 'none'
12+
*/
813
t?: string;
9-
/** The nonce value for your Content Security Policy. */
14+
/** The nonce value for your Content Security Policy. @defaultValue '' */
1015
nonce?: string;
16+
/** storageKey @defaultValue 'o' */
17+
k?: string;
1118
}
1219

1320
/** Modify transition globally to avoid patched transitions */
@@ -37,28 +44,29 @@ const modifyTransition = (themeTransition = "none", nonce = "") => {
3744
*
3845
* @source - Source code
3946
*/
40-
export const Core = ({ t, nonce }: CoreProps) => {
47+
export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
4148
const [{ m: mode, s: systemMode }, setThemeState] = useStore();
4249

4350
useEffect(() => {
44-
const media = matchMedia(MEDIA);
51+
// store global functions to local variables to avoid any interference
52+
[media, updateDOM] = [m, u];
4553
/** Updating media: prefers-color-scheme*/
46-
const updateSystemColorScheme = () =>
47-
setThemeState(state => ({ ...state, s: media.matches ? DARK : LIGHT }) as Store);
48-
media.addEventListener("change", updateSystemColorScheme);
54+
media.addEventListener("change", () =>
55+
setThemeState(state => ({ ...state, s: media.matches ? DARK : LIGHT }) as Store),
56+
);
4957
/** Sync the tabs */
5058
const storageListener = (e: StorageEvent): void => {
51-
if (e.key === COOKIE_KEY)
59+
if (e.key === k)
5260
setThemeState(state => ({ ...state, m: e.newValue as ColorSchemePreference }));
5361
};
5462
addEventListener("storage", storageListener);
5563
}, []);
5664

5765
useEffect(() => {
5866
const restoreTransitions = modifyTransition(t, nonce);
59-
u(mode, systemMode);
67+
updateDOM(mode, systemMode);
6068
restoreTransitions();
6169
}, [systemMode, mode, t, nonce]);
6270

63-
return <script dangerouslySetInnerHTML={{ __html: `(${s.toString()})('${COOKIE_KEY}')` }} />;
71+
return <script dangerouslySetInnerHTML={{ __html: `(${s.toString()})('${k}')` }} />;
6472
};

lib/src/client/core/script.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ColorSchemePreference, ResolvedScheme } from "../../utils";
22

33
declare global {
44
var u: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
5+
var m: MediaQueryList;
56
}
67
export const s = (storageKey: string) => {
78
const [SYSTEM, DARK] = ["system", "dark"] as const;
@@ -18,8 +19,6 @@ export const s = (storageKey: string) => {
1819
// System mode is decided by current system state and need not be stored in localStorage
1920
localStorage.setItem(storageKey, mode);
2021
};
21-
u(
22-
(localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference,
23-
matchMedia(`(prefers-color-scheme: ${DARK})`).matches ? DARK : "",
24-
);
22+
m = matchMedia(`(prefers-color-scheme: ${DARK})`);
23+
u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, m.matches ? DARK : "");
2524
};

0 commit comments

Comments
 (0)