Skip to content

Commit c996be3

Browse files
alecthomasGoosewesbillmanPinky
authored
feat(desktop): add 'Follow system' theme mode (#1262)
Signed-off-by: Alec Thomas <[email protected]> Signed-off-by: Wes <[email protected]> Co-authored-by: Goose <[email protected]> Co-authored-by: Wes <[email protected]> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
1 parent 227518c commit c996be3

4 files changed

Lines changed: 201 additions & 48 deletions

File tree

‎desktop/src/features/settings/ui/SettingsPanels.tsx‎

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Keyboard,
1010
LayoutTemplate,
1111
LockKeyhole,
12+
Monitor,
1213
MonitorCog,
1314
Moon,
1415
Search,
@@ -33,6 +34,7 @@ import {
3334
useTheme,
3435
} from "@/shared/theme/ThemeProvider";
3536
import { SYNTAX_THEMES, isLightTheme } from "@/shared/theme/theme-loader";
37+
import { Switch } from "@/shared/ui/switch";
3638
import { ChannelTemplatesSettingsCard } from "./ChannelTemplatesSettingsCard";
3739
import { DoctorSettingsPanel } from "./DoctorSettingsPanel";
3840
import { ExperimentalFeaturesCard } from "./ExperimentalFeaturesCard";
@@ -188,8 +190,16 @@ function formatThemeLabel(name: string): string {
188190
}
189191

190192
function ThemeSettingsCard() {
191-
const { setTheme, themeName, isDark, accentColor, setAccentColor } =
192-
useTheme();
193+
const {
194+
setTheme,
195+
themeName,
196+
selectedThemeName,
197+
isDark,
198+
accentColor,
199+
setAccentColor,
200+
followSystem,
201+
setFollowSystem,
202+
} = useTheme();
193203
const [search, setSearch] = useState("");
194204
const didScrollRef = useRef(false);
195205
const activeRef = (node: HTMLButtonElement | null) => {
@@ -199,20 +209,25 @@ function ThemeSettingsCard() {
199209
}
200210
};
201211

212+
const selectedTheme = selectedThemeName;
213+
202214
const filtered = useMemo(() => {
203215
const q = search.toLowerCase().trim();
204216
if (!q) return SYNTAX_THEMES;
205217
return SYNTAX_THEMES.filter((name) => name.includes(q));
206218
}, [search]);
207219

208220
return (
209-
<section className="min-w-0" data-testid="settings-theme">
221+
<section
222+
className="flex min-h-0 flex-1 flex-col"
223+
data-testid="settings-theme"
224+
>
210225
<SettingsSectionHeader
211226
title="Appearance"
212-
description="Choose a theme for Buzz. Light and dark mode is auto-detected."
227+
description="Choose a theme for Buzz."
213228
/>
214229

215-
<div className="relative mb-3">
230+
<div className="relative mb-3 shrink-0">
216231
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
217232
<input
218233
autoCapitalize="none"
@@ -226,14 +241,15 @@ function ThemeSettingsCard() {
226241
/>
227242
</div>
228243

229-
<div className="max-h-72 overflow-y-auto rounded-lg border border-border/70 bg-background/70">
244+
<div className="min-h-0 flex-1 overflow-y-auto rounded-lg border border-border/70 bg-background/70">
230245
{filtered.length === 0 ? (
231246
<p className="px-3 py-4 text-center text-sm text-muted-foreground">
232247
No themes match your search.
233248
</p>
234249
) : (
235250
filtered.map((name) => {
236-
const isActive = themeName === name;
251+
const isActive = selectedTheme === name;
252+
const isEffective = themeName === name;
237253
const light = isLightTheme(name);
238254

239255
return (
@@ -243,7 +259,9 @@ function ThemeSettingsCard() {
243259
"flex w-full items-center gap-3 px-3 py-2 text-left text-sm transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
244260
isActive
245261
? "bg-primary/10 text-foreground"
246-
: "text-muted-foreground hover:bg-accent hover:text-accent-foreground",
262+
: isEffective && followSystem
263+
? "bg-primary/5 text-foreground"
264+
: "text-muted-foreground hover:bg-accent hover:text-accent-foreground",
247265
)}
248266
data-testid={`theme-option-${name}`}
249267
key={name}
@@ -262,44 +280,62 @@ function ThemeSettingsCard() {
262280
{isActive && (
263281
<Check className="h-4 w-4 shrink-0 text-primary" />
264282
)}
283+
{!isActive && isEffective && followSystem && (
284+
<Monitor className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
285+
)}
265286
</button>
266287
);
267288
})
268289
)}
269290
</div>
270291

271-
<div className="mt-4">
272-
<h3 className="mb-2 text-sm font-medium">Accent Color</h3>
273-
<div className="flex gap-2">
274-
{ACCENT_COLORS.map((color) => {
275-
const isNeutral = color.value === NEUTRAL_ACCENT;
276-
const swatchColor = isNeutral
277-
? "hsl(var(--foreground))"
278-
: color.value;
279-
const checkClassName =
280-
isNeutral && isDark ? "text-black" : "text-white";
292+
<div className="mt-4 flex shrink-0 flex-col gap-3 pb-2 sm:flex-row sm:items-start sm:justify-between">
293+
<div className="min-w-0">
294+
<h3 className="mb-2 text-sm font-medium">Accent color</h3>
295+
<div className="flex flex-wrap gap-2">
296+
{ACCENT_COLORS.map((color) => {
297+
const isNeutral = color.value === NEUTRAL_ACCENT;
298+
const swatchColor = isNeutral
299+
? "hsl(var(--foreground))"
300+
: color.value;
301+
const checkClassName =
302+
isNeutral && isDark ? "text-black" : "text-white";
281303

282-
return (
283-
<button
284-
className={cn(
285-
"flex h-7 w-7 items-center justify-center rounded-full border border-border/50 transition-transform hover:scale-110",
286-
accentColor === color.value &&
287-
"ring-2 ring-ring ring-offset-2 ring-offset-background",
288-
)}
289-
data-testid={`accent-color-${color.name.toLowerCase()}`}
290-
key={color.value}
291-
onClick={() => setAccentColor(color.value)}
292-
style={{ backgroundColor: swatchColor }}
293-
title={color.name}
294-
type="button"
295-
>
296-
{accentColor === color.value && (
297-
<Check className={cn("h-4 w-4", checkClassName)} />
298-
)}
299-
</button>
300-
);
301-
})}
304+
return (
305+
<button
306+
className={cn(
307+
"flex h-7 w-7 items-center justify-center rounded-full border border-border/50 transition-transform hover:scale-110",
308+
accentColor === color.value &&
309+
"ring-2 ring-ring ring-offset-2 ring-offset-background",
310+
)}
311+
data-testid={`accent-color-${color.name.toLowerCase()}`}
312+
key={color.value}
313+
onClick={() => setAccentColor(color.value)}
314+
style={{ backgroundColor: swatchColor }}
315+
title={color.name}
316+
type="button"
317+
>
318+
{accentColor === color.value && (
319+
<Check className={cn("h-4 w-4", checkClassName)} />
320+
)}
321+
</button>
322+
);
323+
})}
324+
</div>
302325
</div>
326+
327+
<label
328+
className="flex cursor-pointer items-center gap-3 text-sm font-medium text-foreground"
329+
htmlFor="follow-system-switch"
330+
>
331+
<span className="min-w-0 truncate">Use system setting</span>
332+
<Switch
333+
checked={followSystem}
334+
data-testid="follow-system-toggle"
335+
id="follow-system-switch"
336+
onCheckedChange={setFollowSystem}
337+
/>
338+
</label>
303339
</div>
304340
</section>
305341
);

‎desktop/src/features/settings/ui/SettingsView.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export function SettingsView({
278278
<div className="relative z-10 ml-px mt-px flex min-h-0 flex-1 flex-col overflow-hidden rounded-tl-xl bg-background pt-11 shadow-[-1px_-1px_0_0_hsl(var(--sidebar-border)/0.45)]">
279279
<section className="min-h-0 flex-1 overflow-y-auto px-5 pb-5 pt-4 sm:px-6">
280280
<div
281-
className="mx-auto flex w-full max-w-4xl flex-col gap-4"
281+
className="mx-auto flex h-full w-full max-w-4xl flex-col gap-4"
282282
data-testid={`settings-panel-${section}`}
283283
>
284284
{renderSettingsSection(section, {

‎desktop/src/shared/theme/ThemeProvider.tsx‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ import {
1212
SYNTAX_THEMES,
1313
type SyntaxThemeName,
1414
extractThemeInfo,
15+
getThemePair,
1516
loadThemeData,
17+
resolveSystemTheme,
1618
} from "./theme-loader";
1719

1820
export const THEME_STORAGE_KEY = "buzz-theme";
1921
const CACHE_KEY = "buzz-theme-cache";
2022
export const ACCENT_STORAGE_KEY = "buzz-accent-color";
2123
export const NEUTRAL_ACCENT = "neutral";
24+
const FOLLOW_SYSTEM_KEY = "buzz-follow-system";
2225
const VIDEO_REVIEW_NEUTRAL_ACCENT = "0 0% 98%";
2326
const VIDEO_REVIEW_CHIP_SURFACE = "#161616";
2427
const VIDEO_REVIEW_TEXT_CONTRAST = 4.5;
@@ -41,11 +44,15 @@ const DEFAULT_ACCENT = "#3b82f6";
4144

4245
type ThemeContextValue = {
4346
themeName: string;
47+
selectedThemeName: string;
4448
isDark: boolean;
4549
isLoading: boolean;
4650
accentColor: string;
51+
followSystem: boolean;
52+
hasPair: boolean;
4753
setTheme: (name: string) => void;
4854
setAccentColor: (color: string) => void;
55+
setFollowSystem: (enabled: boolean) => void;
4956
};
5057

5158
type ThemeProviderProps = {
@@ -258,9 +265,9 @@ export function ThemeProvider({
258265
defaultTheme = "houston",
259266
}: ThemeProviderProps) {
260267
// Apply cached vars synchronously before first render
261-
const [themeName, setThemeName] = useState<string>(() => {
262-
const cached = applyCachedVars();
263-
return cached ?? readStoredTheme(defaultTheme);
268+
const [selectedTheme, setSelectedTheme] = useState<string>(() => {
269+
applyCachedVars();
270+
return readStoredTheme(defaultTheme);
264271
});
265272
const [isDark, setIsDark] = useState<boolean>(() => {
266273
return document.documentElement.classList.contains("dark");
@@ -270,16 +277,33 @@ export function ThemeProvider({
270277
const [accentColor, setAccentColorState] = useState<string>(() => {
271278
return window.localStorage.getItem(ACCENT_STORAGE_KEY) ?? DEFAULT_ACCENT;
272279
});
280+
const [followSystem, setFollowSystemState] = useState<boolean>(() => {
281+
return window.localStorage.getItem(FOLLOW_SYSTEM_KEY) === "true";
282+
});
283+
const [systemIsDark, setSystemIsDark] = useState<boolean>(() => {
284+
return window.matchMedia("(prefers-color-scheme: dark)").matches;
285+
});
286+
287+
// Resolve the effective theme based on follow-system preference
288+
const effectiveTheme = (() => {
289+
if (!followSystem || !isValidThemeName(selectedTheme)) return selectedTheme;
290+
return resolveSystemTheme(selectedTheme as SyntaxThemeName, systemIsDark);
291+
})();
292+
293+
// Check if the selected theme has a pair (for UI hint)
294+
const hasPair = isValidThemeName(selectedTheme)
295+
? getThemePair(selectedTheme as SyntaxThemeName) !== null
296+
: false;
273297

274298
useEffect(() => {
275-
if (!isValidThemeName(themeName)) return;
299+
if (!isValidThemeName(effectiveTheme)) return;
276300

277301
// Track which theme we're loading to avoid race conditions
278-
const thisTheme = themeName;
302+
const thisTheme = effectiveTheme;
279303
loadingRef.current = thisTheme;
280304
setIsLoading(true);
281305

282-
applyTheme(themeName).then(({ isDark: dark }) => {
306+
applyTheme(effectiveTheme as SyntaxThemeName).then(({ isDark: dark }) => {
283307
// Only update if this is still the theme we want
284308
if (loadingRef.current === thisTheme) {
285309
setIsDark(dark);
@@ -290,15 +314,29 @@ export function ThemeProvider({
290314
);
291315
}
292316
});
293-
}, [themeName]);
317+
}, [effectiveTheme]);
318+
319+
// Listen for system color scheme changes when followSystem is enabled
320+
useEffect(() => {
321+
if (!followSystem) return;
322+
323+
const mq = window.matchMedia("(prefers-color-scheme: dark)");
324+
const handler = (event: MediaQueryListEvent) => {
325+
setSystemIsDark(event.matches);
326+
};
327+
328+
setSystemIsDark(mq.matches);
329+
mq.addEventListener("change", handler);
330+
return () => mq.removeEventListener("change", handler);
331+
}, [followSystem]);
294332

295333
useEffect(() => {
296334
applyAccentColor(accentColor);
297335
}, [accentColor]);
298336

299337
const setTheme = useCallback((name: string) => {
300338
if (!isValidThemeName(name)) return;
301-
setThemeName(name);
339+
setSelectedTheme(name);
302340
window.localStorage.setItem(THEME_STORAGE_KEY, name);
303341
}, []);
304342

@@ -307,13 +345,22 @@ export function ThemeProvider({
307345
setAccentColorState(color);
308346
}, []);
309347

348+
const setFollowSystem = useCallback((enabled: boolean) => {
349+
window.localStorage.setItem(FOLLOW_SYSTEM_KEY, enabled ? "true" : "false");
350+
setFollowSystemState(enabled);
351+
}, []);
352+
310353
const value: ThemeContextValue = {
311-
themeName,
354+
themeName: effectiveTheme,
355+
selectedThemeName: selectedTheme,
312356
isDark,
313357
isLoading,
314358
accentColor,
359+
followSystem,
360+
hasPair,
315361
setTheme,
316362
setAccentColor,
363+
setFollowSystem,
317364
};
318365

319366
return (

0 commit comments

Comments
 (0)