Skip to content

Commit 3bceaf7

Browse files
nullcoderClaude
andauthored
fix: resolve CodeMirror dark mode and theme switching issues (#138)
* fix: improve CodeMirror dark mode theme detection and system theme support - Use resolvedTheme from next-themes for better theme resolution - Add fallback to system preference detection during initialization - Add dedicated effect to handle initial theme resolution - Remove duplicate theme update logic - Fix theme switching when system mode changes This ensures the code editor properly matches the system dark mode and responds correctly to theme changes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <claude@ghostpaste.dev> * fix: resolve CodeMirror theme switching issue Remove duplicate theme update effect that was interfering with proper theme switching. Now the editor correctly responds to manual theme changes while still supporting system theme detection. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <claude@ghostpaste.dev> * fix: improve CodeMirror dark mode theme detection and system theme support - Use resolvedTheme from next-themes for better theme resolution - Add fallback to system preference detection during initialization - Add dedicated effect to handle initial theme resolution - Remove duplicate theme update logic - Fix theme switching when system mode changes This ensures the code editor properly matches the system dark mode and responds correctly to theme changes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <claude@ghostpaste.dev> * fix: resolve CodeMirror theme switching issue Remove duplicate theme update effect that was interfering with proper theme switching. Now the editor correctly responds to manual theme changes while still supporting system theme detection. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <claude@ghostpaste.dev> --------- Co-authored-by: Claude <claude@ghostpaste.dev>
1 parent 65fe20b commit 3bceaf7

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

components/ui/code-editor.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
113113
) {
114114
const containerRef = useRef<HTMLDivElement>(null);
115115
const viewRef = useRef<EditorView | null>(null);
116-
const { theme: systemTheme } = useTheme();
116+
const { resolvedTheme } = useTheme();
117117

118118
// Store the onChange callback in a ref to avoid stale closures
119119
const onChangeRef = useRef(onChange);
@@ -139,8 +139,8 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
139139
[]
140140
);
141141

142-
// Determine the active theme
143-
const activeTheme = themeOverride || systemTheme || "light";
142+
// Determine the active theme - use resolvedTheme which handles system theme properly
143+
const activeTheme = themeOverride || resolvedTheme || "light";
144144

145145
// Get language extension
146146
const getLanguageExtension = useCallback((lang: string) => {
@@ -215,11 +215,19 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
215215
// Debounced onChange for performance
216216
let changeTimeout: ReturnType<typeof setTimeout>;
217217

218+
// Determine theme for initialization - prefer resolved theme but fallback to system preference
219+
const initTheme =
220+
resolvedTheme ||
221+
(typeof window !== "undefined" &&
222+
window.matchMedia("(prefers-color-scheme: dark)").matches
223+
? "dark"
224+
: "light");
225+
218226
// Create extensions with compartments
219227
const extensions = [
220228
...baseExtensions,
221229
languageCompartment.of(getLanguageExtension(language)),
222-
themeCompartment.of(getThemeExtension(activeTheme)),
230+
themeCompartment.of(getThemeExtension(initTheme)),
223231
readOnlyCompartment.of(EditorState.readOnly.of(readOnly)),
224232
lineNumbersCompartment.of(
225233
showLineNumbers ? [lineNumbers(), foldGutter()] : []

0 commit comments

Comments
 (0)