Skip to content

Commit ad23856

Browse files
committed
optimize
1 parent 63d2487 commit ad23856

File tree

6 files changed

+22
-21
lines changed

6 files changed

+22
-21
lines changed

examples/nextjs/next-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3+
/// <reference types="next/navigation-types/compat/navigation" />
34

45
// NOTE: This file should not be edited
56
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"vitest": "^2.1.8"
7575
},
7676
"dependencies": {
77-
"r18gs": "^3.0.1"
77+
"r18gs": "2.0.2"
7878
},
7979
"peerDependencies": {
8080
"@types/react": "16.8 - 19",

lib/src/client/core/core.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("theme-switcher", () => {
4242
await act(() => {
4343
// globalThis.window.media = LIGHT as ResolvedScheme;
4444
// @ts-expect-error -- ok
45-
m.onchange?.();
45+
q.onchange?.();
4646
});
4747
expect(hook.result.current.mode).toBe(DARK);
4848
});

lib/src/client/core/core.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DARK, LIGHT } from "../../constants";
22
import { ColorSchemePreference, ResolvedScheme, useStore } from "../../utils";
3-
import { useEffect } from "react";
43
import { noFOUCScript } from "./no-fouc";
54

65
let media: MediaQueryList,
@@ -36,17 +35,18 @@ export interface CoreProps {
3635

3736
/** Modify transition globally to avoid patched transitions */
3837
const modifyTransition = (themeTransition = "none", nonce = "") => {
39-
const css = document.createElement("style");
38+
const doc = document;
39+
const css = doc.createElement("style");
4040
/** split by ';' to prevent CSS injection */
4141
css.textContent = `*,*:after,*:before{transition:${themeTransition.split(";")[0]} !important;}`;
42-
nonce && css.setAttribute("nonce", nonce);
43-
document.head.appendChild(css);
42+
css.setAttribute("nonce", nonce);
43+
doc.head.appendChild(css);
4444

4545
return () => {
4646
// Force restyle
47-
getComputedStyle(document.body);
47+
getComputedStyle(doc.body);
4848
// Wait for next tick before removing
49-
setTimeout(() => document.head.removeChild(css), 1);
49+
setTimeout(() => doc.head.removeChild(css), 1);
5050
};
5151
};
5252

@@ -62,14 +62,15 @@ const modifyTransition = (themeTransition = "none", nonce = "") => {
6262
* @source - Source code
6363
*/
6464
export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
65+
const isWindowDefined = typeof window != "undefined";
6566
// handle client side exceptions when script is not run. <- for client side apps like vite or CRA
66-
if (typeof window !== "undefined" && !window.m) noFOUCScript(k);
67+
if (isWindowDefined && !window.q) noFOUCScript(k);
6768

68-
const [{ m: mode, s: systemMode }, setThemeState] = useStore();
69+
const [{ m, s }, setThemeState] = useStore();
6970

70-
useEffect(() => {
71+
if (!updateDOM && isWindowDefined) {
7172
// store global functions to local variables to avoid any interference
72-
[media, updateDOM] = [m, u];
73+
[media, updateDOM] = [q, u];
7374
/** Updating media: prefers-color-scheme*/
7475
media.addEventListener("change", () =>
7576
setThemeState(state => ({ ...state, s: media.matches ? DARK : LIGHT })),
@@ -78,13 +79,12 @@ export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
7879
addEventListener("storage", (e: StorageEvent): void => {
7980
e.key === k && setThemeState(state => ({ ...state, m: e.newValue as ColorSchemePreference }));
8081
});
81-
}, []);
82-
83-
useEffect(() => {
82+
}
83+
if (updateDOM) {
8484
const restoreTransitions = modifyTransition(t, nonce);
85-
updateDOM(mode, systemMode);
85+
updateDOM(m, s);
8686
restoreTransitions();
87-
}, [systemMode, mode, t, nonce]);
87+
}
8888

8989
return <Script {...{ n: nonce, k }} />;
9090
};

lib/src/client/core/no-fouc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare global {
44
// skipcq: JS-0102, JS-C1002, JS-0239
55
var u: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
66
// skipcq: JS-0102, JS-C1002, JS-0239
7-
var m: MediaQueryList;
7+
var q: MediaQueryList;
88
}
99

1010
/** function to be injected in script tag for avoiding FOUC */
@@ -23,6 +23,6 @@ export const noFOUCScript = (storageKey: string) => {
2323
// System mode is decided by current system state and need not be stored in localStorage
2424
localStorage.setItem(storageKey, mode);
2525
};
26-
window.m = matchMedia(`(prefers-color-scheme: ${DARK})`);
27-
u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, m.matches ? DARK : "");
26+
window.q = matchMedia(`(prefers-color-scheme: ${DARK})`);
27+
u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, q.matches ? DARK : "");
2828
};

lib/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface Store {
1111
/** local abstaction of RGS to avoid multiple imports */
1212
export const useStore = () =>
1313
useRGS<Store>("ndm", () => {
14-
if (typeof document === "undefined") return { m: SYSTEM, s: DARK };
14+
if (typeof document == "undefined") return { m: SYSTEM, s: DARK };
1515
const [m, s] = ["m", "sm"].map(dt => document.documentElement.getAttribute("data-" + dt));
1616
return {
1717
m: (m ?? SYSTEM) as ColorSchemePreference,

0 commit comments

Comments
 (0)