Skip to content

Commit 52b0b83

Browse files
committed
fix: light theme not applying correctly to new user
1 parent b6c502b commit 52b0b83

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

apps/client/src/components/settings-sheet/components/editor-theme.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,28 @@ const DEFAULT_THEMES = {
6666
},
6767
};
6868

69+
// Function to detect system color preference
70+
const getSystemTheme = (): 'vs-dark' | 'light' => {
71+
if (typeof window === 'undefined') return 'vs-dark'; // Default for SSR
72+
73+
return window.matchMedia &&
74+
window.matchMedia('(prefers-color-scheme: dark)').matches
75+
? 'vs-dark'
76+
: 'light';
77+
};
78+
6979
const EditorThemeSettings = ({ monaco }: EditorThemeSettingsProps) => {
7080
const { setTheme } = useTheme();
7181
const [open, setOpen] = useState(false);
72-
const [editorTheme, setEditorTheme] = useState('vs-dark'); // Set default theme
82+
83+
// Initialize with system preference if no saved theme
84+
const savedTheme =
85+
typeof localStorage !== 'undefined'
86+
? localStorage.getItem('editorTheme')
87+
: null;
88+
const initialTheme = savedTheme || getSystemTheme();
89+
90+
const [editorTheme, setEditorTheme] = useState(initialTheme);
7391

7492
// Register Monaco when the component mounts
7593
useEffect(() => {
@@ -104,6 +122,11 @@ const EditorThemeSettings = ({ monaco }: EditorThemeSettingsProps) => {
104122
console.error('Failed to sync theme:', error);
105123
}
106124
}
125+
} else {
126+
// No saved theme, use system preference
127+
const systemTheme = getSystemTheme();
128+
setEditorTheme(systemTheme);
129+
setTheme(systemTheme === 'vs-dark' ? 'dark' : 'light');
107130
}
108131
}, [setTheme]);
109132

apps/client/src/lib/init-editor-theme.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const setCSSVariables = (variables: Record<string, string>) => {
5050
let globalMonaco: Monaco | null = null;
5151

5252
// Function to register Monaco
53-
5453
export const registerMonaco = (monaco: Monaco) => {
5554
globalMonaco = monaco;
5655

@@ -61,16 +60,35 @@ export const registerMonaco = (monaco: Monaco) => {
6160
: null;
6261
if (savedTheme && globalMonaco) {
6362
globalMonaco.editor.setTheme(savedTheme);
63+
} else if (globalMonaco) {
64+
// If no saved theme, check system preference
65+
const systemPrefersDark =
66+
typeof window !== 'undefined' &&
67+
window.matchMedia &&
68+
window.matchMedia('(prefers-color-scheme: dark)').matches;
69+
70+
const defaultTheme = systemPrefersDark ? 'vs-dark' : 'light';
71+
globalMonaco.editor.setTheme(defaultTheme);
6472
}
6573
};
6674

75+
// Function to check system preference for dark mode
76+
const getSystemPreference = (): 'dark' | 'light' => {
77+
if (typeof window === 'undefined') return 'dark'; // Default for SSR
78+
79+
return window.matchMedia &&
80+
window.matchMedia('(prefers-color-scheme: dark)').matches
81+
? 'dark'
82+
: 'light';
83+
};
84+
6785
export const initEditorTheme = () => {
6886
if (typeof window === 'undefined') return; // Skip during SSR
6987

7088
const savedTheme = localStorage.getItem('editorTheme');
7189

7290
if (savedTheme) {
73-
// Apply theme variables
91+
// Apply saved theme variables
7492
if (savedTheme in DEFAULT_THEMES) {
7593
// For default themes
7694
const themeConfig =
@@ -132,15 +150,40 @@ export const initEditorTheme = () => {
132150
console.error('Failed to load theme:', error);
133151
}
134152
}
153+
} else {
154+
// No saved theme, check system preference and apply appropriate default theme
155+
const systemPreference = getSystemPreference();
156+
const defaultTheme = systemPreference === 'dark' ? 'vs-dark' : 'light';
157+
158+
// Store in localStorage to maintain consistency
159+
localStorage.setItem('editorTheme', defaultTheme);
160+
161+
// Apply default theme based on system preference
162+
const themeConfig =
163+
DEFAULT_THEMES[defaultTheme as keyof typeof DEFAULT_THEMES];
164+
setCSSVariables(themeConfig.variables);
165+
setCSSVariables({ '--status-bar-text': '#fff' });
166+
167+
// Set document classes
168+
if (defaultTheme === 'vs-dark') {
169+
document.documentElement.classList.add('dark');
170+
document.documentElement.classList.remove('light');
171+
} else {
172+
document.documentElement.classList.remove('dark');
173+
document.documentElement.classList.add('light');
174+
}
135175
}
136176

137177
// Apply theme to Monaco if it's available
138-
if (globalMonaco && savedTheme) {
139-
globalMonaco.editor.setTheme(savedTheme);
178+
if (globalMonaco) {
179+
const theme =
180+
localStorage.getItem('editorTheme') ||
181+
(getSystemPreference() === 'dark' ? 'vs-dark' : 'light');
182+
globalMonaco.editor.setTheme(theme);
140183
}
141184
};
142185

143-
// Run initialization immediately (without hooks)
186+
// Run initialization immediately
144187
if (typeof window !== 'undefined') {
145188
// Use setTimeout to ensure this runs after the browser has fully loaded
146189
setTimeout(initEditorTheme, 0);

0 commit comments

Comments
 (0)