Skip to content

Commit 08feee4

Browse files
authored
Merge pull request #15 from react18-tools/minify
Minify
2 parents 71d4de0 + 0bffa97 commit 08feee4

File tree

10 files changed

+60
-40
lines changed

10 files changed

+60
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ If you are using CSP rules for CSS files, you can pass `nonce` argument to the `
220220

221221
Show different images based on the current theme:
222222

223-
```jsx
223+
```ts
224224
import Image from "next/image";
225225
import { useMode } from "nextjs-darkmode/hooks";
226226

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/basic-features/typescript for more information.

lib/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# nextjs-darkmode
22

3+
## 1.0.4
4+
5+
### Patch Changes
6+
7+
- 4cc739a: Do not export the NoFOUCScript.
8+
39
## 1.0.3
410

511
### Patch Changes

lib/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ If you are using CSP rules for CSS files, you can pass `nonce` argument to the `
220220

221221
Show different images based on the current theme:
222222

223-
```jsx
223+
```ts
224224
import Image from "next/image";
225225
import { useMode } from "nextjs-darkmode/hooks";
226226

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nextjs-darkmode",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
44
"private": false,
5-
"version": "1.0.3",
5+
"version": "1.0.4",
66
"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",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { act, cleanup, fireEvent, render, renderHook } from "@testing-library/react";
22
import { afterEach, beforeEach, describe, test } from "vitest";
3-
import { Core, noFOUCScript } from "./core";
3+
import { Core } from "./core";
44
import { useMode } from "../../hooks";
55
import { DARK, LIGHT } from "../../constants";
6+
import { noFOUCScript } from "./no-fouc";
67

78
const STORAGE_KEY = "o";
89
describe("theme-switcher", () => {

lib/src/client/core/core.tsx

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
11
import { DARK, LIGHT } from "../../constants";
22
import { ColorSchemePreference, ResolvedScheme, useStore } from "../../utils";
33
import { memo, useEffect } from "react";
4-
5-
declare global {
6-
// skipcq: JS-0102, JS-0239
7-
var u: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
8-
// skipcq: JS-0102, JS-0239
9-
var m: MediaQueryList;
10-
}
11-
12-
/** function to be injected in script tag for avoiding FOUC */
13-
export const noFOUCScript = (storageKey: string) => {
14-
const [SYSTEM, DARK] = ["system", "dark"] as const;
15-
window.u = (mode: ColorSchemePreference, systemMode: ResolvedScheme) => {
16-
const resolvedMode = mode === SYSTEM ? systemMode : mode;
17-
const el = document.documentElement;
18-
if (resolvedMode === DARK) el.classList.add(DARK);
19-
else el.classList.remove(DARK);
20-
[
21-
["sm", systemMode],
22-
["rm", resolvedMode],
23-
["m", mode],
24-
].forEach(([dataLabel, value]) => el.setAttribute(`data-${dataLabel}`, value));
25-
// System mode is decided by current system state and need not be stored in localStorage
26-
localStorage.setItem(storageKey, mode);
27-
};
28-
window.m = matchMedia(`(prefers-color-scheme: ${DARK})`);
29-
u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, m.matches ? DARK : "");
30-
};
4+
import { noFOUCScript } from "./no-fouc";
315

326
let media: MediaQueryList,
337
updateDOM: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
@@ -40,14 +14,17 @@ interface ScriptProps {
4014
}
4115

4216
/** Avoid rerender of script */
43-
const Script = memo(({ n, k }: ScriptProps) => (
44-
<script
45-
suppressHydrationWarning
46-
// skipcq: JS-0440
47-
dangerouslySetInnerHTML={{ __html: `(${noFOUCScript.toString()})('${k}')` }}
48-
nonce={n}
49-
/>
50-
));
17+
const Script = memo(
18+
({ n, k }: ScriptProps) => (
19+
<script
20+
suppressHydrationWarning
21+
// skipcq: JS-0440
22+
dangerouslySetInnerHTML={{ __html: `(${noFOUCScript.toString()})('${k}')` }}
23+
nonce={n}
24+
/>
25+
),
26+
() => false,
27+
);
5128

5229
export interface CoreProps {
5330
/** themeTransition: force apply CSS transition property to all the elements during theme switching. E.g., `all .3s`

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ColorSchemePreference, ResolvedScheme } from "../../utils";
2+
3+
declare global {
4+
// skipcq: JS-0102, JS-C1002, JS-0239
5+
var u: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
6+
// skipcq: JS-0102, JS-C1002, JS-0239
7+
var m: MediaQueryList;
8+
}
9+
10+
/** function to be injected in script tag for avoiding FOUC */
11+
export const noFOUCScript = (storageKey: string) => {
12+
const [SYSTEM, DARK] = ["system", "dark"] as const;
13+
window.u = (mode: ColorSchemePreference, systemMode: ResolvedScheme) => {
14+
const resolvedMode = mode === SYSTEM ? systemMode : mode;
15+
const el = document.documentElement;
16+
if (resolvedMode === DARK) el.classList.add(DARK);
17+
else el.classList.remove(DARK);
18+
[
19+
["sm", systemMode],
20+
["rm", resolvedMode],
21+
["m", mode],
22+
].forEach(([dataLabel, value]) => el.setAttribute(`data-${dataLabel}`, value));
23+
// System mode is decided by current system state and need not be stored in localStorage
24+
localStorage.setItem(storageKey, mode);
25+
};
26+
window.m = matchMedia(`(prefers-color-scheme: ${DARK})`);
27+
u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, m.matches ? DARK : "");
28+
};

packages/shared/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @repo/shared
22

3+
## 0.0.12
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [4cc739a]
8+
9+
310
## 0.0.11
411

512
### Patch Changes

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@repo/shared",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"private": true,
55
"sideEffects": false,
66
"main": "./dist/index.js",

0 commit comments

Comments
 (0)