33import { useRef , useState , useEffect } from "react"
44import Editor , { type Monaco , type OnMount } from "@monaco-editor/react"
55import { Loader2 } from "lucide-react"
6- import { useTheme } from "next-themes"
76
87interface MonacoEditorProps {
98 value : string
@@ -21,24 +20,18 @@ export default function MonacoEditor({
2120 language = "javascript" ,
2221 height = "300px" ,
2322 fontSize = 14 ,
24- theme : editorTheme = "vs-dark " ,
23+ theme : editorTheme = "vs" ,
2524 autoFocus = false ,
2625} : MonacoEditorProps ) {
2726 const editorRef = useRef < any > ( null )
28- const { theme : systemTheme } = useTheme ( )
29- const [ isEditorReady , setIsEditorReady ] = useState ( false )
3027 const [ monacoInstance , setMonacoInstance ] = useState < Monaco | null > ( null )
28+ const [ themesRegistered , setThemesRegistered ] = useState ( false )
29+
3130
3231 // Configure Monaco editor on mount
3332 const handleEditorDidMount : OnMount = ( editor , monaco ) => {
3433 editorRef . current = editor
3534 setMonacoInstance ( monaco )
36- setIsEditorReady ( true )
37-
38- // Focus the editor if autoFocus is true
39- if ( autoFocus ) {
40- editor . focus ( )
41- }
4235
4336 // Configure editor settings
4437 editor . updateOptions ( {
@@ -57,8 +50,37 @@ export default function MonacoEditor({
5750
5851 // Register custom themes
5952 registerCustomThemes ( monaco )
53+
54+ // Mark themes as registered
55+ setThemesRegistered ( true )
56+
57+ // Always start with a built-in theme to ensure stability
58+ monaco . editor . setTheme ( 'vs' )
59+
60+ if ( autoFocus ) {
61+ editor . focus ( )
62+ }
6063 }
6164
65+ useEffect ( ( ) => {
66+ if ( ! monacoInstance || ! themesRegistered ) return ;
67+
68+ // Apply the theme with a longer delay to ensure registration is complete
69+ const customThemes = [ "github" , "monokai" , "dracula" , "nord" ]
70+
71+ if ( customThemes . includes ( editorTheme ) ) {
72+ const timer = setTimeout ( ( ) => {
73+ console . log ( "Applying custom theme:" , editorTheme ) ;
74+ monacoInstance . editor . setTheme ( editorTheme ) ;
75+ } , 300 ) ;
76+
77+ return ( ) => clearTimeout ( timer ) ;
78+ } else {
79+ monacoInstance . editor . setTheme ( editorTheme ) ;
80+ }
81+ } , [ monacoInstance , themesRegistered , editorTheme ] ) ;
82+
83+
6284 // Register custom editor themes
6385 const registerCustomThemes = ( monaco : Monaco ) => {
6486 // GitHub theme
0 commit comments