Skip to content

Commit 30e913f

Browse files
committed
fix: peerDeps
1 parent 823a3c7 commit 30e913f

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
"r18gs": "2.0.2"
7878
},
7979
"peerDependencies": {
80-
"@types/react": "16.8 - 19",
81-
"react": "16.8 - 19"
80+
"@types/react": ">= 16.8",
81+
"react": ">= 16.8"
8282
},
8383
"funding": [
8484
{

lib/src/client/core/core.tsx

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ let media: MediaQueryList,
66
updateDOM: (mode: ColorSchemePreference, systemMode: ResolvedScheme) => void;
77

88
interface ScriptProps {
9+
/** themeTransition */
10+
t: string;
911
/** nonce */
10-
n?: string;
12+
n: string;
1113
/** storageKey */
1214
k: string;
1315
}
1416

1517
/** Avoid rerender of script */
16-
const Script = ({ n, k }: ScriptProps) => (
18+
const Script = ({ n, k, t }: ScriptProps) => (
1719
<script
1820
suppressHydrationWarning
1921
// skipcq: JS-0440
20-
dangerouslySetInnerHTML={{ __html: `(${noFOUCScript.toString()})('${k}')` }}
22+
dangerouslySetInnerHTML={{ __html: `(${noFOUCScript.toString()})('${[k, t, n]}')` }}
2123
nonce={n}
2224
/>
2325
);
@@ -33,23 +35,6 @@ export interface CoreProps {
3335
k?: string;
3436
}
3537

36-
/** Modify transition globally to avoid patched transitions */
37-
const modifyTransition = (themeTransition = "none", nonce = "") => {
38-
const doc = document;
39-
const css = doc.createElement("style");
40-
/** split by ';' to prevent CSS injection */
41-
css.textContent = `*,*:after,*:before{transition:${themeTransition.split(";")[0]} !important;}`;
42-
css.setAttribute("nonce", nonce);
43-
doc.head.appendChild(css);
44-
45-
return () => {
46-
// Force restyle
47-
getComputedStyle(doc.body);
48-
// Wait for next tick before removing
49-
setTimeout(() => doc.head.removeChild(css), 1);
50-
};
51-
};
52-
5338
/**
5439
* The Core component wich applies classes and transitions.
5540
* Cookies are set only if corresponding ServerTarget is detected.
@@ -61,10 +46,10 @@ const modifyTransition = (themeTransition = "none", nonce = "") => {
6146
*
6247
* @source - Source code
6348
*/
64-
export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
49+
export const Core = ({ t = "none", nonce = "", k = "o" }: CoreProps) => {
6550
const isWindowDefined = typeof window != "undefined";
6651
// handle client side exceptions when script is not run. <- for client side apps like vite or CRA
67-
if (isWindowDefined && !window.q) noFOUCScript(k);
52+
if (isWindowDefined && !window.q) noFOUCScript(k, t, nonce);
6853

6954
const [{ m, s }, setThemeState] = useStore();
7055

@@ -80,11 +65,7 @@ export const Core = ({ t, nonce, k = "o" }: CoreProps) => {
8065
e.key === k && setThemeState(state => ({ ...state, m: e.newValue as ColorSchemePreference }));
8166
});
8267
}
83-
if (updateDOM) {
84-
const restoreTransitions = modifyTransition(t, nonce);
85-
updateDOM(m, s);
86-
restoreTransitions();
87-
}
68+
updateDOM?.(m, s);
8869

89-
return <Script {...{ n: nonce, k }} />;
70+
return <Script {...{ n: nonce, k, t }} />;
9071
};

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ declare global {
88
}
99

1010
/** function to be injected in script tag for avoiding FOUC */
11-
export const noFOUCScript = (storageKey: string) => {
11+
export const noFOUCScript = (storageKey: string, themeTransition: string, nonce: string) => {
1212
const [SYSTEM, DARK] = ["system", "dark"] as const;
1313
window.u = (mode: ColorSchemePreference, systemMode: ResolvedScheme) => {
1414
const resolvedMode = mode === SYSTEM ? systemMode : mode;
15+
const doc = document;
1516
const el = document.documentElement;
17+
const css = doc.createElement("style");
18+
/** split by ';' to prevent CSS injection */
19+
css.textContent = `*,*:after,*:before{transition:${themeTransition.split(";")[0]} !important;}`;
20+
css.setAttribute("nonce", nonce);
21+
doc.head.appendChild(css);
22+
1623
if (resolvedMode === DARK) el.classList.add(DARK);
1724
else el.classList.remove(DARK);
1825
[
@@ -22,6 +29,11 @@ export const noFOUCScript = (storageKey: string) => {
2229
].forEach(([dataLabel, value]) => el.setAttribute(`data-${dataLabel}`, value));
2330
// System mode is decided by current system state and need not be stored in localStorage
2431
localStorage.setItem(storageKey, mode);
32+
33+
// Force restyle
34+
getComputedStyle(doc.body);
35+
// Wait for next tick before removing
36+
setTimeout(() => doc.head.removeChild(css), 1);
2537
};
2638
window.q = matchMedia(`(prefers-color-scheme: ${DARK})`);
2739
u((localStorage.getItem(storageKey) ?? SYSTEM) as ColorSchemePreference, q.matches ? DARK : "");

scripts/lite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const config = require("./rebrand.config.json");
77
const packageJson = require("../lib/package.json");
88

99
const ref = packageJson.name;
10-
packageJson.peerDependencies.r18gs = `${packageJson.dependencies.r18gs.split(".")[0]}`;
10+
packageJson.peerDependencies.r18gs = `>=${packageJson.dependencies.r18gs.split(".")[0]}`;
1111
delete packageJson.dependencies.r18gs;
1212
if (Object.keys(packageJson.devDependencies).length === 0) delete packageJson.devDependencies;
1313
packageJson.name = `${ref}-lite`;

0 commit comments

Comments
 (0)