typescript - Custom Monaco Editor Theme Not Applying in Next.js - Stack Overflow

I'm working on a Next.js 15 project and using @monaco-editorreact to implement a code editor with

I'm working on a Next.js 15 project and using @monaco-editor/react to implement a code editor with a custom One Dark Pro theme. However, the theme is not applying properly.

I tried creating a .json theme file and defined my theme on mount:

import OneDarkPro from './themes/atom-dark.json';


interface CodeEditorProps {
  defaultTheme?: 'vs-light' | 'vs-dark' | 'hc-black' | 'hc-light' | 'atom-dark';
}

const themes = [
  { value: 'vs-light', label: 'Light' },
  { value: 'vs-dark', label: 'Dark' },
  { value: 'hc-black', label: 'High Contrast Black' },
  { value: 'hc-light', label: 'High Contrast Light' },
  { value: 'atom-dark', label: 'One Dark Pro' },
];

type Theme = 'vs-light' | 'vs-dark' | 'hc-black' | 'hc-light' | 'atom-dark';

export default function CodeEditor{(
const handleEditorDidMount: OnMount = useCallback((editor, monaco) => {
  monaco.editor.defineTheme('atom-dark', {
    base: 'vs-dark',
    inherit: true,
    rules: OneDarkPro.rules,
    colors: OneDarkPro.colors
  });

monaco.editor.setTheme(theme === 'atom-dark' ? 'OneDarkPro' : theme);

  editorRef.current = editor;
  setIsEditorReady(true);
    
  setTimeout(() => {
    editor.getAction("editor.action.formatDocument")?.run();
  }, 100);
    
  editor.onDidChangeModelContent(() => {
    updateEditorHeight(editor, monaco);
  });
    
  updateEditorHeight(editor, monaco);
}, []);

const handleThemeChange = useCallback((value: string) => {
  setTheme(value as Theme);
}, []);
})



<Editor
  aria-label="Code Editor"
  className={maxHeight ? `max-h-[${maxHeight}]` : `max-h-[${minHeight}]`}
  height={contentHeight}
  value={editorCode}
  theme={theme}
  language={language}
  onChange={handleCodeChange}
  onMount={handleEditorDidMount}
  options={{
    ...editorOptions,
    wordWrap: 'on',
    autoIndent: 'advanced',
    cursorSmoothCaretAnimation: "explicit",
    lineNumbers: readOnly ? "off" : isReadOnly ? "off" : "on",
    accessibilitySupport: 'on',
    acceptSuggestionOnEnter: 'smart',
    renderLineHighlight: readOnly ? "none" : isReadOnly ? "none" : "all",
    placeholder: isReadOnly || readOnly ? '' : placeholder,
    tabSize: 2,
    scrollbar: {
      vertical: 'visible',
      horizontal: 'visible',
    }
  }}
  loading={
    <div className="flex w-full h-full items-center justify-center bg-transparent">
      <Spinner />
    </div>
  }
/>

Got the One Dark Theme from here: .json

then converted it here: /

Issue: The theme is defined, but it does not apply to the editor.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744509654a4577914.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信