-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuseTheme.ts
More file actions
254 lines (218 loc) · 7.03 KB
/
useTheme.ts
File metadata and controls
254 lines (218 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { useCallback, useEffect, useSyncExternalStore } from "react";
import {
applyCustomTheme,
applyFontOverride,
applyRadiusOverride,
getStoredCustomTheme,
initCustomTheme,
removeCustomTheme,
} from "../lib/customTheme";
type Theme = "light" | "dark" | "system";
type ColorTheme =
| "default"
| "iridescent-void"
| "solar-witch"
| "midnight-clarity"
| "carbon"
| "vapor"
| "cathedral-circuit";
type FontFamily = "dm-sans" | "inter" | "plus-jakarta-sans";
type ThemeSnapshot = {
theme: Theme;
systemDark: boolean;
colorTheme: ColorTheme;
fontFamily: FontFamily;
};
export const COLOR_THEMES: { id: ColorTheme; label: string }[] = [
{ id: "default", label: "Default" },
{ id: "iridescent-void", label: "Iridescent Void" },
{ id: "solar-witch", label: "Solar Witch" },
{ id: "midnight-clarity", label: "Midnight Clarity" },
{ id: "carbon", label: "Carbon" },
{ id: "vapor", label: "Vapor" },
{ id: "cathedral-circuit", label: "Cathedral Circuit" },
{ id: "custom", label: "Custom" },
];
export const FONT_FAMILIES: { id: FontFamily; label: string }[] = [
{ id: "inter", label: "Inter" },
{ id: "dm-sans", label: "DM Sans" },
{ id: "plus-jakarta-sans", label: "Plus Jakarta Sans" },
];
const STORAGE_KEY = "okcode:theme";
const COLOR_THEME_STORAGE_KEY = "okcode:color-theme";
const FONT_FAMILY_STORAGE_KEY = "okcode:font-family";
const MEDIA_QUERY = "(prefers-color-scheme: dark)";
let listeners: Array<() => void> = [];
let lastSnapshot: ThemeSnapshot | null = null;
let lastDesktopTheme: Theme | null = null;
function emitChange() {
for (const listener of listeners) listener();
}
function getSystemDark(): boolean {
return window.matchMedia(MEDIA_QUERY).matches;
}
function getStored(): Theme {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw === "light" || raw === "dark" || raw === "system") return raw;
return "system";
}
function getStoredColorTheme(): ColorTheme {
const raw = localStorage.getItem(COLOR_THEME_STORAGE_KEY);
if (
raw === "default" ||
raw === "iridescent-void" ||
raw === "solar-witch" ||
raw === "midnight-clarity" ||
raw === "carbon" ||
raw === "vapor" ||
raw === "cathedral-circuit"
) {
return raw;
}
return "default";
}
function getStoredFontFamily(): FontFamily {
const raw = localStorage.getItem(FONT_FAMILY_STORAGE_KEY);
if (raw === "dm-sans" || raw === "inter" || raw === "plus-jakarta-sans") {
return raw;
}
return "inter";
}
const FONT_FAMILY_MAP: Record<FontFamily, string> = {
inter: '"Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif',
"dm-sans": '"DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif',
"plus-jakarta-sans":
'"Plus Jakarta Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif',
};
function applyFont(fontFamily?: FontFamily) {
const font = fontFamily ?? getStoredFontFamily();
document.documentElement.style.setProperty("--font-ui", FONT_FAMILY_MAP[font]);
}
function applyTheme(theme: Theme, suppressTransitions = false) {
if (suppressTransitions) {
document.documentElement.classList.add("no-transitions");
}
const isDark = theme === "dark" || (theme === "system" && getSystemDark());
document.documentElement.classList.toggle("dark", isDark);
// Apply color theme class
const colorTheme = getStoredColorTheme();
// Remove any existing theme-* classes
const existingThemeClasses = Array.from(document.documentElement.classList).filter((cls) =>
cls.startsWith("theme-"),
);
for (const cls of existingThemeClasses) {
document.documentElement.classList.remove(cls);
}
// Add the new theme class if not default
if (colorTheme !== "default") {
document.documentElement.classList.add(`theme-${colorTheme}`);
}
// Apply font family
applyFont();
syncDesktopTheme(theme);
if (suppressTransitions) {
// Force a reflow so the no-transitions class takes effect before removal
// oxlint-disable-next-line no-unused-expressions
document.documentElement.offsetHeight;
requestAnimationFrame(() => {
document.documentElement.classList.remove("no-transitions");
});
}
}
function syncDesktopTheme(theme: Theme) {
const bridge = window.desktopBridge;
if (!bridge || lastDesktopTheme === theme) {
return;
}
lastDesktopTheme = theme;
void bridge.setTheme(theme).catch(() => {
if (lastDesktopTheme === theme) {
lastDesktopTheme = null;
}
});
}
// Initialize custom theme + overrides on module load
initCustomTheme();
// Apply immediately on module load to prevent flash
applyTheme(getStored());
function getSnapshot(): ThemeSnapshot {
const theme = getStored();
const systemDark = theme === "system" ? getSystemDark() : false;
const colorTheme = getStoredColorTheme();
const fontFamily = getStoredFontFamily();
if (
lastSnapshot &&
lastSnapshot.theme === theme &&
lastSnapshot.systemDark === systemDark &&
lastSnapshot.colorTheme === colorTheme &&
lastSnapshot.fontFamily === fontFamily
) {
return lastSnapshot;
}
lastSnapshot = { theme, systemDark, colorTheme, fontFamily };
return lastSnapshot;
}
function subscribe(listener: () => void): () => void {
listeners.push(listener);
// Listen for system preference changes
const mq = window.matchMedia(MEDIA_QUERY);
const handleChange = () => {
if (getStored() === "system") applyTheme("system", true);
emitChange();
};
mq.addEventListener("change", handleChange);
// Listen for storage changes from other tabs
const handleStorage = (e: StorageEvent) => {
if (
e.key === STORAGE_KEY ||
e.key === COLOR_THEME_STORAGE_KEY ||
e.key === FONT_FAMILY_STORAGE_KEY
) {
applyTheme(getStored(), true);
emitChange();
}
};
window.addEventListener("storage", handleStorage);
return () => {
listeners = listeners.filter((l) => l !== listener);
mq.removeEventListener("change", handleChange);
window.removeEventListener("storage", handleStorage);
};
}
export function useTheme() {
const snapshot = useSyncExternalStore(subscribe, getSnapshot);
const theme = snapshot.theme;
const colorTheme = snapshot.colorTheme;
const fontFamily = snapshot.fontFamily;
const resolvedTheme: "light" | "dark" =
theme === "system" ? (snapshot.systemDark ? "dark" : "light") : theme;
const setTheme = useCallback((next: Theme) => {
localStorage.setItem(STORAGE_KEY, next);
applyTheme(next, true);
emitChange();
}, []);
const setColorTheme = useCallback((next: ColorTheme) => {
localStorage.setItem(COLOR_THEME_STORAGE_KEY, next);
applyTheme(getStored(), true);
emitChange();
}, []);
const setFontFamily = useCallback((next: FontFamily) => {
localStorage.setItem(FONT_FAMILY_STORAGE_KEY, next);
applyFont(next);
emitChange();
}, []);
// Keep DOM in sync on mount/change
useEffect(() => {
applyTheme(theme);
}, [theme]);
return {
theme,
setTheme,
resolvedTheme,
colorTheme,
setColorTheme,
fontFamily,
setFontFamily,
} as const;
}
export type { Theme, ColorTheme };