The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 03:45 UTC
Avoid repeated theme DOM sync during startup (#2779)
Co-authored-by: Julius Marminge <jmarminge@gmail.com>
Mika Cohen committed
Jun 18, 2026 at 23:46 UTC
20f37f367a146fb4118bd0f0182336f4ab8c6ac8
1 file changed
+17
-5
apps/web/src/hooks/useTheme.ts
+17
-5
@@ -18,6 +18,7 @@ const DYNAMIC_THEME_COLOR_SELECTOR = `meta[name="${THEME_COLOR_META_NAME}"][data
18
let listeners: Array<() => void> = [];
19
let lastSnapshot: ThemeSnapshot | null = null;
20
let lastDesktopTheme: Theme | null = null;
21
+let lastAppliedTheme: ThemeSnapshot | null = null;
22
23
function emitChange() {
24
for (const listener of listeners) listener();
@@ -28,7 +29,11 @@ function hasThemeStorage() {
29
}
30
31
function getSystemDark() {
31
- 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
+ );
37
}
38
39
function getStored(): Theme {
@@ -89,11 +94,18 @@ export function syncBrowserChromeTheme() {
94
95
function applyTheme(theme: Theme, suppressTransitions = false) {
96
if (typeof document === "undefined" || typeof window === "undefined") return;
97
+ const systemDark = theme === "system" ? getSystemDark() : false;
98
+ if (lastAppliedTheme?.theme === theme && lastAppliedTheme.systemDark === systemDark) {
99
+ syncDesktopTheme(theme);
100
+ return;
101
+ }
102
+
103
if (suppressTransitions) {
104
document.documentElement.classList.add("no-transitions");
105
}
95
- const isDark = theme === "dark" || (theme === "system" && getSystemDark());
106
+ const isDark = theme === "dark" || (theme === "system" && systemDark);
107
document.documentElement.classList.toggle("dark", isDark);
108
+ lastAppliedTheme = { theme, systemDark };
109
syncBrowserChromeTheme();
110
syncDesktopTheme(theme);
111
if (suppressTransitions) {
@@ -148,12 +160,12 @@ function subscribe(listener: () => void): () => void {
160
listeners.push(listener);
161
162
// Listen for system preference changes
151
- const mq = window.matchMedia(MEDIA_QUERY);
163
+ const mq = typeof window.matchMedia === "function" ? window.matchMedia(MEDIA_QUERY) : null;
164
const handleChange = () => {
165
if (getStored() === "system") applyTheme("system", true);
166
emitChange();
167
};
156
- mq.addEventListener("change", handleChange);
168
+ mq?.addEventListener("change", handleChange);
169
170
// Listen for storage changes from other tabs
171
const handleStorage = (e: StorageEvent) => {
@@ -166,7 +178,7 @@ function subscribe(listener: () => void): () => void {
178
179
return () => {
180
listeners = listeners.filter((l) => l !== listener);
169
- mq.removeEventListener("change", handleChange);
181
+ mq?.removeEventListener("change", handleChange);
182
window.removeEventListener("storage", handleStorage);
183
};
184
}