Skip to content

Commit 27683ba

Browse files
committed
fix: FOUC
1 parent 9139366 commit 27683ba

File tree

7 files changed

+31
-26
lines changed

7 files changed

+31
-26
lines changed

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"vitest": "^1.6.0"
6868
},
6969
"dependencies": {
70-
"r18gs": "^1.0.2"
70+
"r18gs": "^1.1.0"
7171
},
7272
"peerDependencies": {
7373
"@types/react": "16.8 - 19",

lib/src/client/core/core.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { COOKIE_KEY, DARK, LIGHT, SYSTEM, modes } from "../../constants";
1+
import { COOKIE_KEY, DARK, LIGHT, MEDIA, SYSTEM, modes } from "../../constants";
22
import { ColorSchemePreference, Store, useStore } from "../../utils";
33
import { useEffect } from "react";
44

@@ -41,17 +41,11 @@ export const Core = ({ t, nonce }: CoreProps) => {
4141
const resolvedMode = mode === SYSTEM ? systemMode : mode; // resolvedMode is the actual mode that will be used
4242

4343
useEffect(() => {
44-
const media = matchMedia(`(prefers-color-scheme: ${DARK})`);
44+
const media = matchMedia(MEDIA);
4545
/** Updating media: prefers-color-scheme*/
4646
const updateSystemColorScheme = () =>
4747
setThemeState(state => ({ ...state, s: media.matches ? DARK : LIGHT }) as Store);
48-
updateSystemColorScheme();
4948
media.addEventListener("change", updateSystemColorScheme);
50-
51-
setThemeState(state => ({
52-
...state,
53-
m: (localStorage.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
54-
}));
5549
/** Sync the tabs */
5650
const storageListener = (e: StorageEvent): void => {
5751
if (e.key === COOKIE_KEY)

lib/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export const LIGHT: ColorSchemePreference = "light";
66

77
export const COOKIE_KEY = "gx";
88
export const modes: ColorSchemePreference[] = [SYSTEM, DARK, LIGHT];
9+
10+
export const MEDIA = `(prefers-color-scheme: ${DARK})`;

lib/src/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import useRGS from "r18gs";
2-
import { DARK, SYSTEM } from "./constants";
2+
import { COOKIE_KEY, DARK, LIGHT, MEDIA, SYSTEM } from "./constants";
33

44
export type ColorSchemePreference = "system" | "dark" | "light";
55
export type ResolvedScheme = "dark" | "light";
@@ -14,4 +14,12 @@ const DEFAULT_STORE_VAL: Store = {
1414
};
1515

1616
/** local abstaction of RGS to avoid multiple imports */
17-
export const useStore = () => useRGS<Store>("ndm", DEFAULT_STORE_VAL);
17+
export const useStore = () =>
18+
useRGS<Store>("ndm", () =>
19+
typeof localStorage === "undefined"
20+
? DEFAULT_STORE_VAL
21+
: {
22+
m: (localStorage.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
23+
s: (matchMedia(MEDIA).matches ? DARK : LIGHT) as ResolvedScheme,
24+
},
25+
);

lib/vitest.setup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { vi } from "vitest";
22

3+
const mediaListeners: (() => void)[] = [];
34
// mock matchMedia
45
Object.defineProperty(window, "matchMedia", {
56
writable: true,
67
value: vi.fn().mockImplementation((query: string) => ({
78
matches: query.includes(window.media),
89
media: query,
9-
onchange: null,
10-
addEventListener: vi.fn(),
10+
onchange: () => mediaListeners.forEach(listener => listener()),
11+
addEventListener: (listener: () => void) => mediaListeners.push(listener),
1112
removeEventListener: vi.fn(),
1213
dispatchEvent: vi.fn(),
1314
})),

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@repo/scripts": "workspace:*",
4343
"nextjs-darkmode": "workspace:*",
4444
"nextjs-themes": "^3.1.1",
45-
"r18gs": "^1.0.2",
45+
"r18gs": "^1.1.0",
4646
"react-live": "^4.1.6",
4747
"react18-loaders": "^1.1.1"
4848
},

pnpm-lock.yaml

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)