|
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"; |
3 | 3 | import { useEffect } from "react"; |
4 | 4 | import { s } from "./script"; |
5 | 5 |
|
| 6 | +let media: MediaQueryList, |
| 7 | + updateDOM: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void; |
| 8 | + |
6 | 9 | 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 | + */ |
8 | 13 | t?: string; |
9 | | - /** The nonce value for your Content Security Policy. */ |
| 14 | + /** The nonce value for your Content Security Policy. @defaultValue '' */ |
10 | 15 | nonce?: string; |
| 16 | + /** storageKey @defaultValue 'o' */ |
| 17 | + k?: string; |
11 | 18 | } |
12 | 19 |
|
13 | 20 | /** Modify transition globally to avoid patched transitions */ |
@@ -37,28 +44,29 @@ const modifyTransition = (themeTransition = "none", nonce = "") => { |
37 | 44 | * |
38 | 45 | * @source - Source code |
39 | 46 | */ |
40 | | -export const Core = ({ t, nonce }: CoreProps) => { |
| 47 | +export const Core = ({ t, nonce, k = "o" }: CoreProps) => { |
41 | 48 | const [{ m: mode, s: systemMode }, setThemeState] = useStore(); |
42 | 49 |
|
43 | 50 | useEffect(() => { |
44 | | - const media = matchMedia(MEDIA); |
| 51 | + // store global functions to local variables to avoid any interference |
| 52 | + [media, updateDOM] = [m, u]; |
45 | 53 | /** 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 | + ); |
49 | 57 | /** Sync the tabs */ |
50 | 58 | const storageListener = (e: StorageEvent): void => { |
51 | | - if (e.key === COOKIE_KEY) |
| 59 | + if (e.key === k) |
52 | 60 | setThemeState(state => ({ ...state, m: e.newValue as ColorSchemePreference })); |
53 | 61 | }; |
54 | 62 | addEventListener("storage", storageListener); |
55 | 63 | }, []); |
56 | 64 |
|
57 | 65 | useEffect(() => { |
58 | 66 | const restoreTransitions = modifyTransition(t, nonce); |
59 | | - u(mode, systemMode); |
| 67 | + updateDOM(mode, systemMode); |
60 | 68 | restoreTransitions(); |
61 | 69 | }, [systemMode, mode, t, nonce]); |
62 | 70 |
|
63 | | - return <script dangerouslySetInnerHTML={{ __html: `(${s.toString()})('${COOKIE_KEY}')` }} />; |
| 71 | + return <script dangerouslySetInnerHTML={{ __html: `(${s.toString()})('${k}')` }} />; |
64 | 72 | }; |
0 commit comments