To build the examples locally, run:
yarn
cd example
yarn
yarn startThen open http://localhost:8886 in a browser.
yarn add react-monaco-editorimport React from 'react';
import { render } from 'react-dom';
import MonacoEditor from 'react-monaco-editor';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
code: '// type your code...',
}
}
editorDidMount(editor, monaco) {
console.log('editorDidMount', editor);
editor.focus();
}
onChange(newValue, e) {
console.log('onChange', newValue, e);
}
render() {
const code = this.state.code;
const options = {
selectOnLineNumbers: true
};
return (
<MonacoEditor
width="800"
height="600"
language="javascript"
theme="vs-dark"
value={code}
options={options}
onChange={::this.onChange}
editorDidMount={::this.editorDidMount}
/>
);
}
}
render(
<App />,
document.getElementById('root')
);Add the Monaco Webpack plugin monaco-editor-webpack-plugin to your webpack.config.js:
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
module.exports = {
plugins: [
new MonacoWebpackPlugin({
// available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
languages: ['json']
})
]
};Sidenote: Monaco Editor uses CSS imports internally, so if you're using CSS Modules in your project - you're likely to get conflict by default. In order to avoid that - separate css-loader for app and monaco-editor package:
// Specify separate paths
const path = require('path');
const APP_DIR = path.resolve(__dirname, './src');
const MONACO_DIR = path.resolve(__dirname, './node_modules/monaco-editor');
{
test: /\.css$/,
include: APP_DIR,
use: [{
loader: 'style-loader',
}, {
loader: 'css-loader',
options: {
modules: true,
namedExport: true,
},
}],
}, {
test: /\.css$/,
include: MONACO_DIR,
use: ['style-loader', 'css-loader'],
}All the properties below are optional.
-
widthwidth of editor. Defaults to100%. -
heightheight of editor. Defaults to100%. -
valuevalue of the auto created model in the editor. -
defaultValuethe initial value of the auto created model in the editor. -
languagethe initial language of the auto created model in the editor. -
themethe theme of the editor -
optionsrefer to Monaco interface IEditorConstructionOptions. -
overrideServicesrefer to Monaco Interface IEditorOverrideServices. It depends on monaco's internal implementations and may change over time, check github issue for more details. -
onChange(newValue, event)an event emitted when the content of the current model has changed. -
editorWillMount(monaco)an event emitted before the editor mounted (similar tocomponentWillMountof React). -
editorDidMount(editor, monaco)an event emitted when the editor has been mounted (similar tocomponentDidMountof React). -
contextallow to pass a different context then the global window onto which the Monaco instance will be loaded. Useful if you want to load the editor in an iframe.
Refer to Monaco interface IEditor.
Make sure to use the Monaco Webpack plugin or follow the instructions on how to load the ESM version of Monaco.
Using the first parameter of editorDidMount, or using a ref (e.g. <MonacoEditor ref="monaco">) after editorDidMount event has fired.
Then you can invoke instance methods via this.refs.monaco.editor, e.g. this.refs.monaco.editor.focus() to focuses the MonacoEditor instance.
Using this.refs.monaco.editor.getValue() or via method of Model instance:
const model = this.refs.monaco.editor.getModel();
const value = model.getValue();For example, you may want to configure some JSON schemas before editor mounted, then you can go with editorWillMount(monaco):
class App extends React.Component {
editorWillMount(monaco) {
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemas: [{
uri: "http://myserver/foo-schema.json",
fileMatch: ['*'],
schema: {
type: "object",
properties: {
p1: {
enum: [ "v1", "v2"]
},
p2: {
$ref: "http://myserver/bar-schema.json"
}
}
}
}]
});
}
render() {
return (
<MonacoEditor language="json" editorWillMount={this.editorWillMount} />
);
}
}Monaco only supports one theme.
import React from 'react';
import { MonacoDiffEditor } from 'react-monaco-editor';
class App extends React.Component {
render() {
const code1 = "// your original code...";
const code2 = "// a different version...";
const options = {
//renderSideBySide: false
};
return (
<MonacoDiffEditor
width="800"
height="600"
language="javascript"
original={code1}
value={code2}
options={options}
/>
);
}
}MIT, see the LICENSE file for detail.

