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/examples/nextjs/tsconfig.json b/examples/nextjs/tsconfig.json index aaddcaa8..12ccfa01 100644 --- a/examples/nextjs/tsconfig.json +++ b/examples/nextjs/tsconfig.json @@ -1,5 +1,7 @@ { - "exclude": ["node_modules"], + "exclude": [ + "node_modules" + ], "extends": "@repo/typescript-config/nextjs.json", "compilerOptions": { "outDir": "dist", @@ -7,7 +9,13 @@ { "name": "next" } - ] + ], + "strictNullChecks": true }, - "include": ["src", "next.config.js", "next-env.d.ts", ".next/types/**/*.ts"] + "include": [ + "src", + "next.config.js", + "next-env.d.ts", + ".next/types/**/*.ts" + ] } diff --git a/lib/CHANGELOG.md b/lib/CHANGELOG.md index 62ca6581..641699e0 100644 --- a/lib/CHANGELOG.md +++ b/lib/CHANGELOG.md @@ -1,5 +1,11 @@ # nextjs-darkmode +## 1.0.2 + +### Patch Changes + +- 722eeae: Supress Hydration Warning + ## 1.0.1 ### Patch Changes diff --git a/lib/package.json b/lib/package.json index cdb84cb4..655bce37 100644 --- a/lib/package.json +++ b/lib/package.json @@ -2,7 +2,7 @@ "name": "nextjs-darkmode", "author": "Mayank Kumar Chaudhari ", "private": false, - "version": "1.0.1", + "version": "1.0.2", "description": "Unleash the Power of React Server Components! Use dark/light mode on your site with confidence, without losing any advantages of React Server Components", "license": "MPL-2.0", "main": "./dist/index.js", diff --git a/lib/src/client/core/core.test.tsx b/lib/src/client/core/core.test.tsx index 299e71e8..5f116ba5 100644 --- a/lib/src/client/core/core.test.tsx +++ b/lib/src/client/core/core.test.tsx @@ -1,6 +1,6 @@ import { act, cleanup, fireEvent, render, renderHook } from "@testing-library/react"; import { afterEach, beforeEach, describe, test } from "vitest"; -import { Core, s } from "./core"; +import { Core, noFOUCScript } from "./core"; import { useMode } from "../../hooks"; import { DARK, LIGHT } from "../../constants"; @@ -9,7 +9,7 @@ describe("theme-switcher", () => { afterEach(cleanup); beforeEach(() => { - s(STORAGE_KEY); + noFOUCScript(STORAGE_KEY); render(); }); diff --git a/lib/src/client/core/core.tsx b/lib/src/client/core/core.tsx index 73eff48f..442558dd 100644 --- a/lib/src/client/core/core.tsx +++ b/lib/src/client/core/core.tsx @@ -1,14 +1,16 @@ import { DARK, LIGHT } from "../../constants"; -import { ColorSchemePreference, ResolvedScheme, Store, useStore } from "../../utils"; -import { useEffect } from "react"; +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 s = (storageKey: string) => { +export const noFOUCScript = (storageKey: string) => { const [SYSTEM, DARK] = ["system", "dark"] as const; window.u = (mode: ColorSchemePreference, systemMode: ResolvedScheme) => { const resolvedMode = mode === SYSTEM ? systemMode : mode; @@ -30,6 +32,23 @@ export const s = (storageKey: string) => { let media: MediaQueryList, updateDOM: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void; +interface ScriptProps { + /** nonce */ + n?: string; + /** storageKey */ + k: string; +} + +/** Avoid rerender of script */ +const Script = memo(({ n, k }: ScriptProps) => ( +