From ad23856bb600ddafa1e39c68a608d7477430fdc4 Mon Sep 17 00:00:00 2001 From: Mayank Date: Thu, 19 Dec 2024 18:37:29 +0530 Subject: [PATCH] optimize --- examples/nextjs/next-env.d.ts | 1 + lib/package.json | 2 +- lib/src/client/core/core.test.tsx | 2 +- lib/src/client/core/core.tsx | 30 +++++++++++++++--------------- lib/src/client/core/no-fouc.ts | 6 +++--- lib/src/utils.ts | 2 +- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/nextjs/next-env.d.ts b/examples/nextjs/next-env.d.ts index 1b3be0840..3cd7048ed 100644 --- a/examples/nextjs/next-env.d.ts +++ b/examples/nextjs/next-env.d.ts @@ -1,5 +1,6 @@ /// /// +/// // NOTE: This file should not be edited // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/lib/package.json b/lib/package.json index 04160d506..47e7180e3 100644 --- a/lib/package.json +++ b/lib/package.json @@ -74,7 +74,7 @@ "vitest": "^2.1.8" }, "dependencies": { - "r18gs": "^3.0.1" + "r18gs": "2.0.2" }, "peerDependencies": { "@types/react": "16.8 - 19", diff --git a/lib/src/client/core/core.test.tsx b/lib/src/client/core/core.test.tsx index 2fee4bee0..8cdda1dbd 100644 --- a/lib/src/client/core/core.test.tsx +++ b/lib/src/client/core/core.test.tsx @@ -42,7 +42,7 @@ describe("theme-switcher", () => { await act(() => { // globalThis.window.media = LIGHT as ResolvedScheme; // @ts-expect-error -- ok - m.onchange?.(); + q.onchange?.(); }); expect(hook.result.current.mode).toBe(DARK); }); diff --git a/lib/src/client/core/core.tsx b/lib/src/client/core/core.tsx index 6645bfd41..65d451a8c 100644 --- a/lib/src/client/core/core.tsx +++ b/lib/src/client/core/core.tsx @@ -1,6 +1,5 @@ import { DARK, LIGHT } from "../../constants"; import { ColorSchemePreference, ResolvedScheme, useStore } from "../../utils"; -import { useEffect } from "react"; import { noFOUCScript } from "./no-fouc"; let media: MediaQueryList, @@ -36,17 +35,18 @@ export interface CoreProps { /** Modify transition globally to avoid patched transitions */ const modifyTransition = (themeTransition = "none", nonce = "") => { - const css = document.createElement("style"); + const doc = document; + const css = doc.createElement("style"); /** split by ';' to prevent CSS injection */ css.textContent = `*,*:after,*:before{transition:${themeTransition.split(";")[0]} !important;}`; - nonce && css.setAttribute("nonce", nonce); - document.head.appendChild(css); + css.setAttribute("nonce", nonce); + doc.head.appendChild(css); return () => { // Force restyle - getComputedStyle(document.body); + getComputedStyle(doc.body); // Wait for next tick before removing - setTimeout(() => document.head.removeChild(css), 1); + setTimeout(() => doc.head.removeChild(css), 1); }; }; @@ -62,14 +62,15 @@ const modifyTransition = (themeTransition = "none", nonce = "") => { * @source - Source code */ export const Core = ({ t, nonce, k = "o" }: CoreProps) => { + const isWindowDefined = typeof window != "undefined"; // handle client side exceptions when script is not run. <- for client side apps like vite or CRA - if (typeof window !== "undefined" && !window.m) noFOUCScript(k); + if (isWindowDefined && !window.q) noFOUCScript(k); - const [{ m: mode, s: systemMode }, setThemeState] = useStore(); + const [{ m, s }, setThemeState] = useStore(); - useEffect(() => { + if (!updateDOM && isWindowDefined) { // store global functions to local variables to avoid any interference - [media, updateDOM] = [m, u]; + [media, updateDOM] = [q, u]; /** Updating media: prefers-color-scheme*/ media.addEventListener("change", () => setThemeState(state => ({ ...state, s: media.matches ? DARK : LIGHT })), @@ -78,13 +79,12 @@ export const Core = ({ t, nonce, k = "o" }: CoreProps) => { addEventListener("storage", (e: StorageEvent): void => { e.key === k && setThemeState(state => ({ ...state, m: e.newValue as ColorSchemePreference })); }); - }, []); - - useEffect(() => { + } + if (updateDOM) { const restoreTransitions = modifyTransition(t, nonce); - updateDOM(mode, systemMode); + updateDOM(m, s); restoreTransitions(); - }, [systemMode, mode, t, nonce]); + } return