@@ -50,7 +50,6 @@ const setCSSVariables = (variables: Record<string, string>) => {
50
50
let globalMonaco : Monaco | null = null ;
51
51
52
52
// Function to register Monaco
53
-
54
53
export const registerMonaco = ( monaco : Monaco ) => {
55
54
globalMonaco = monaco ;
56
55
@@ -61,16 +60,35 @@ export const registerMonaco = (monaco: Monaco) => {
61
60
: null ;
62
61
if ( savedTheme && globalMonaco ) {
63
62
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 ) ;
64
72
}
65
73
} ;
66
74
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
+
67
85
export const initEditorTheme = ( ) => {
68
86
if ( typeof window === 'undefined' ) return ; // Skip during SSR
69
87
70
88
const savedTheme = localStorage . getItem ( 'editorTheme' ) ;
71
89
72
90
if ( savedTheme ) {
73
- // Apply theme variables
91
+ // Apply saved theme variables
74
92
if ( savedTheme in DEFAULT_THEMES ) {
75
93
// For default themes
76
94
const themeConfig =
@@ -132,15 +150,40 @@ export const initEditorTheme = () => {
132
150
console . error ( 'Failed to load theme:' , error ) ;
133
151
}
134
152
}
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
+ }
135
175
}
136
176
137
177
// 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 ) ;
140
183
}
141
184
} ;
142
185
143
- // Run initialization immediately (without hooks)
186
+ // Run initialization immediately
144
187
if ( typeof window !== 'undefined' ) {
145
188
// Use setTimeout to ensure this runs after the browser has fully loaded
146
189
setTimeout ( initEditorTheme , 0 ) ;
0 commit comments