Skip to content

Commit 423703a

Browse files
mjcjuliusmarminge
authored andcommitted
Guard theme sync media queries
1 parent 22d0eb2 commit 423703a

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
function installThemeDom(theme: string | null, matchMedia?: Window["matchMedia"]) {
4+
const element = {
5+
name: "",
6+
setAttribute: vi.fn(),
7+
};
8+
const documentElement = {
9+
classList: {
10+
add: vi.fn(),
11+
remove: vi.fn(),
12+
toggle: vi.fn(),
13+
},
14+
offsetHeight: 0,
15+
style: {},
16+
};
17+
const body = {
18+
style: {},
19+
};
20+
21+
vi.stubGlobal("localStorage", {
22+
getItem: vi.fn(() => theme),
23+
setItem: vi.fn(),
24+
});
25+
vi.stubGlobal("window", {
26+
matchMedia,
27+
addEventListener: vi.fn(),
28+
removeEventListener: vi.fn(),
29+
});
30+
vi.stubGlobal("document", {
31+
body,
32+
createElement: vi.fn(() => element),
33+
documentElement,
34+
head: {
35+
append: vi.fn(),
36+
},
37+
querySelector: vi.fn(() => null),
38+
});
39+
vi.stubGlobal(
40+
"getComputedStyle",
41+
vi.fn(() => ({ backgroundColor: "rgb(1, 2, 3)" })),
42+
);
43+
}
44+
45+
describe("useTheme module initialization", () => {
46+
afterEach(() => {
47+
vi.resetModules();
48+
vi.unstubAllGlobals();
49+
});
50+
51+
it("does not read matchMedia for explicit themes", async () => {
52+
const matchMedia = vi.fn(() => ({
53+
matches: true,
54+
addEventListener: vi.fn(),
55+
removeEventListener: vi.fn(),
56+
})) as unknown as Window["matchMedia"];
57+
installThemeDom("dark", matchMedia);
58+
59+
await import("./useTheme");
60+
61+
expect(matchMedia).not.toHaveBeenCalled();
62+
});
63+
64+
it("does not require matchMedia when an explicit theme is stored", async () => {
65+
installThemeDom("light");
66+
67+
await expect(import("./useTheme")).resolves.toBeDefined();
68+
});
69+
});

‎apps/web/src/hooks/useTheme.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ function hasThemeStorage() {
2929
}
3030

3131
function getSystemDark() {
32-
return typeof window !== "undefined" && window.matchMedia(MEDIA_QUERY).matches;
32+
return (
33+
typeof window !== "undefined" &&
34+
typeof window.matchMedia === "function" &&
35+
window.matchMedia(MEDIA_QUERY).matches
36+
);
3337
}
3438

3539
function getStored(): Theme {
@@ -90,7 +94,7 @@ export function syncBrowserChromeTheme() {
9094

9195
function applyTheme(theme: Theme, suppressTransitions = false) {
9296
if (typeof document === "undefined" || typeof window === "undefined") return;
93-
const systemDark = getSystemDark();
97+
const systemDark = theme === "system" ? getSystemDark() : false;
9498
if (lastAppliedTheme?.theme === theme && lastAppliedTheme.systemDark === systemDark) {
9599
syncDesktopTheme(theme);
96100
return;
@@ -156,12 +160,12 @@ function subscribe(listener: () => void): () => void {
156160
listeners.push(listener);
157161

158162
// Listen for system preference changes
159-
const mq = window.matchMedia(MEDIA_QUERY);
163+
const mq = typeof window.matchMedia === "function" ? window.matchMedia(MEDIA_QUERY) : null;
160164
const handleChange = () => {
161165
if (getStored() === "system") applyTheme("system", true);
162166
emitChange();
163167
};
164-
mq.addEventListener("change", handleChange);
168+
mq?.addEventListener("change", handleChange);
165169

166170
// Listen for storage changes from other tabs
167171
const handleStorage = (e: StorageEvent) => {
@@ -174,7 +178,7 @@ function subscribe(listener: () => void): () => void {
174178

175179
return () => {
176180
listeners = listeners.filter((l) => l !== listener);
177-
mq.removeEventListener("change", handleChange);
181+
mq?.removeEventListener("change", handleChange);
178182
window.removeEventListener("storage", handleStorage);
179183
};
180184
}

0 commit comments

Comments
 (0)