From 3c510fa62bcf2b668c387250841e4273cfd8d06a Mon Sep 17 00:00:00 2001 From: Mayank Date: Sat, 22 Jun 2024 16:40:12 +0530 Subject: [PATCH] minify --- examples/nextjs/next-env.d.ts | 1 + lib/src/client/core/core.test.tsx | 3 +- lib/src/client/core/core.tsx | 47 ++++++++----------------------- lib/src/client/core/no-fouc.ts | 28 ++++++++++++++++++ 4 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 lib/src/client/core/no-fouc.ts diff --git a/examples/nextjs/next-env.d.ts b/examples/nextjs/next-env.d.ts index 4f11a03d..fd36f949 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/basic-features/typescript for more information. diff --git a/lib/src/client/core/core.test.tsx b/lib/src/client/core/core.test.tsx index 5f116ba5..2fee4bee 100644 --- a/lib/src/client/core/core.test.tsx +++ b/lib/src/client/core/core.test.tsx @@ -1,8 +1,9 @@ import { act, cleanup, fireEvent, render, renderHook } from "@testing-library/react"; import { afterEach, beforeEach, describe, test } from "vitest"; -import { Core, noFOUCScript } from "./core"; +import { Core } from "./core"; import { useMode } from "../../hooks"; import { DARK, LIGHT } from "../../constants"; +import { noFOUCScript } from "./no-fouc"; const STORAGE_KEY = "o"; describe("theme-switcher", () => { diff --git a/lib/src/client/core/core.tsx b/lib/src/client/core/core.tsx index 442558dd..b0ee4145 100644 --- a/lib/src/client/core/core.tsx +++ b/lib/src/client/core/core.tsx @@ -1,33 +1,7 @@ import { DARK, LIGHT } from "../../constants"; import { ColorSchemePreference, ResolvedScheme, useStore } from "../../utils"; import { memo, useEffect } from "react"; - -declare global { - // skipcq: JS-0102, JS-0239 - var u: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void; - // skipcq: JS-0102, JS-0239 - var m: MediaQueryList; -} - -/** function to be injected in script tag for avoiding FOUC */ -export const noFOUCScript = (storageKey: string) => { - const [SYSTEM, DARK] = ["system", "dark"] as const; - window.u = (mode: ColorSchemePreference, systemMode: ResolvedScheme) => { - const resolvedMode = mode === SYSTEM ? systemMode : mode; - const el = document.documentElement; - if (resolvedMode === DARK) el.classList.add(DARK); - else el.classList.remove(DARK); - [ - ["sm", systemMode], - ["rm", resolvedMode], - ["m", mode], - ].forEach(([dataLabel, value]) => el.setAttribute(`data-${dataLabel}`, value)); - // System mode is decided by current system state and need not be stored in localStorage - localStorage.setItem(storageKey, mode); - }; - window.m = matchMedia(`(prefers-color-scheme: ${DARK})`); - u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, m.matches ? DARK : ""); -}; +import { noFOUCScript } from "./no-fouc"; let media: MediaQueryList, updateDOM: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void; @@ -40,14 +14,17 @@ interface ScriptProps { } /** Avoid rerender of script */ -const Script = memo(({ n, k }: ScriptProps) => ( -