React components for integrating Microsoft's Monaco Editor into React applications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
React Monaco Editor provides React components for integrating Microsoft's Monaco Editor (the editor that powers VS Code) into React applications. It offers both a standard MonacoEditor component and a MonacoDiffEditor component for comparing code differences.
npm install react-monaco-editor monaco-editorimport MonacoEditor, { MonacoDiffEditor, monaco } from "react-monaco-editor";For named imports:
import { MonacoEditor, MonacoDiffEditor, monaco } from "react-monaco-editor";CommonJS:
const MonacoEditor = require("react-monaco-editor").default;
const { MonacoDiffEditor, monaco } = require("react-monaco-editor");import React, { useState } from "react";
import MonacoEditor from "react-monaco-editor";
function CodeEditor() {
const [code, setCode] = useState('// type your code...');
const options = {
selectOnLineNumbers: true,
roundedSelection: false,
readOnly: false,
cursorStyle: 'line' as const,
automaticLayout: false,
};
return (
<MonacoEditor
width="800"
height="600"
language="javascript"
theme="vs-dark"
value={code}
options={options}
onChange={(newValue) => setCode(newValue)}
editorDidMount={(editor, monaco) => {
console.log('editorDidMount', editor);
editor.focus();
}}
/>
);
}React Monaco Editor is built around several key components:
Main code editor component with full Monaco Editor functionality including syntax highlighting, IntelliSense, and customizable themes.
declare const MonacoEditor: React.ForwardRefExoticComponent<
MonacoEditorProps & React.RefAttributes<MonacoEditorHandle>
>;
interface MonacoEditorProps extends MonacoEditorBaseProps {
value?: string | null;
options?: monaco.editor.IStandaloneEditorConstructionOptions;
overrideServices?: monaco.editor.IEditorOverrideServices;
editorWillMount?: EditorWillMount;
editorDidMount?: EditorDidMount;
editorWillUnmount?: EditorWillUnmount;
onChange?: ChangeHandler;
uri?: (monaco: typeof monaco) => monaco.Uri;
}
interface MonacoEditorHandle {
editor: monaco.editor.IStandaloneCodeEditor;
}Diff editor component for side-by-side code comparison with change highlighting and merge capabilities.
declare const MonacoDiffEditor: React.ForwardRefExoticComponent<
MonacoDiffEditorProps & React.RefAttributes<MonacoDiffEditorHandle>
>;
interface MonacoDiffEditorProps extends MonacoEditorBaseProps {
original?: string;
value?: string;
options?: monaco.editor.IDiffEditorConstructionOptions;
overrideServices?: monaco.editor.IEditorOverrideServices;
editorWillMount?: DiffEditorWillMount;
editorDidMount?: DiffEditorDidMount;
editorWillUnmount?: DiffEditorWillUnmount;
onChange?: DiffChangeHandler;
originalUri?: (monaco: typeof monaco) => monaco.Uri;
modifiedUri?: (monaco: typeof monaco) => monaco.Uri;
}
interface MonacoDiffEditorHandle {
editor: monaco.editor.IStandaloneDiffEditor;
}interface MonacoEditorBaseProps {
/** Width of editor. Defaults to 100%. */
width?: string | number;
/** Height of editor. Defaults to 100%. */
height?: string | number;
/** The initial value of the auto created model in the editor. */
defaultValue?: string;
/** The initial language of the auto created model in the editor. Defaults to 'javascript'. */
language?: string;
/** Theme to be used for rendering. */
theme?: Theme | (string & NonNullable<unknown>) | null;
/** Optional string classname to append to the editor. */
className?: string | null;
}
type Theme = "vs" | "vs-dark" | "hc-black";
type EditorConstructionOptions = NonNullable<
Parameters<typeof monaco.editor.create>[1]
>;
type EditorWillMount = (
monaco: typeof monaco
) => void | EditorConstructionOptions;
type EditorDidMount = (
editor: monaco.editor.IStandaloneCodeEditor,
monaco: typeof monaco
) => void;
type EditorWillUnmount = (
editor: monaco.editor.IStandaloneCodeEditor,
monaco: typeof monaco
) => void | EditorConstructionOptions;
type ChangeHandler = (
value: string,
event: monaco.editor.IModelContentChangedEvent
) => void;
type DiffEditorWillMount = (
monaco: typeof monaco
) => void | monaco.editor.IStandaloneEditorConstructionOptions;
type DiffEditorDidMount = (
editor: monaco.editor.IStandaloneDiffEditor,
monaco: typeof monaco
) => void;
type DiffEditorWillUnmount = (
editor: monaco.editor.IStandaloneDiffEditor,
monaco: typeof monaco
) => void;
type DiffChangeHandler = ChangeHandler;The package re-exports the Monaco Editor API for direct access to Monaco functionality:
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
export { monaco };This provides access to Monaco's language services, themes, editor APIs, and all other Monaco Editor functionality.