Skip to content

Commit 5f32af8

Browse files
committed
Optimize minzip
1 parent 7f6e8e8 commit 5f32af8

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

lib/src/client/core/core.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@ import { COOKIE_KEY, DARK, LIGHT, SYSTEM, modes } from "../../constants";
22
import { ColorSchemePreference, Store, useStore } from "../../utils";
33
import { useEffect } from "react";
44

5-
const useEffectMinify = useEffect;
65
export interface CoreProps {
76
/** force apply CSS transition property to all the elements during theme switching. E.g., `all .3s` */
87
t?: string;
98
}
109

1110
/** Modify transition globally to avoid patched transitions */
12-
const modifyTransition = (documentMinify: Document, themeTransition = "none") => {
13-
const css = documentMinify.createElement("style");
11+
const modifyTransition = (themeTransition = "none") => {
12+
const css = document.createElement("style");
1413
/** split by ';' to prevent CSS injection */
1514
css.textContent = `*{transition:${themeTransition.split(";")[0]} !important;}`;
16-
const head = documentMinify.head;
17-
head.appendChild(css);
15+
document.head.appendChild(css);
1816

1917
return () => {
2018
// Force restyle
21-
getComputedStyle(documentMinify.body);
19+
getComputedStyle(document.body);
2220
// Wait for next tick before removing
23-
setTimeout(() => head.removeChild(css), 1);
21+
setTimeout(() => document.head.removeChild(css), 1);
2422
};
2523
};
2624

@@ -39,7 +37,7 @@ export const Core = ({ t }: CoreProps) => {
3937
const [{ m: mode, s: systemMode }, setThemeState] = useStore();
4038
const resolvedMode = mode === SYSTEM ? systemMode : mode; // resolvedMode is the actual mode that will be used
4139

42-
useEffectMinify(() => {
40+
useEffect(() => {
4341
const media = matchMedia(`(prefers-color-scheme: ${DARK})`);
4442
/** Updating media: prefers-color-scheme*/
4543
const updateSystemColorScheme = () =>
@@ -59,13 +57,12 @@ export const Core = ({ t }: CoreProps) => {
5957
addEventListener("storage", storageListener);
6058
}, []);
6159

62-
useEffectMinify(() => {
63-
const documentMinify = document;
64-
const restoreTransitions = modifyTransition(documentMinify, t);
65-
const serverTargetEl = documentMinify.querySelector("[data-ndm]");
60+
useEffect(() => {
61+
const restoreTransitions = modifyTransition(t);
62+
const serverTargetEl = document.querySelector("[data-ndm]");
6663
// We need to always update documentElement to support Tailwind configuration
6764
// skipcq: JS-D008, JS-0042 -> map keyword is shorter
68-
[documentMinify.documentElement, serverTargetEl].map(el => {
65+
[document.documentElement, serverTargetEl].map(el => {
6966
// skipcq: JS-0042
7067
if (!el) return;
7168
const clsList = el.classList;
@@ -81,7 +78,7 @@ export const Core = ({ t }: CoreProps) => {
8178
// System mode is decided by current system state and need not be stored in localStorage
8279
localStorage.setItem(COOKIE_KEY, mode);
8380
if (serverTargetEl)
84-
documentMinify.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=31536000;SameSite=Strict;`;
81+
document.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=31536000;SameSite=Strict;`;
8582
}, [resolvedMode, systemMode, mode, t]);
8683

8784
return null;

0 commit comments

Comments
 (0)