react-monaco-editor-fix
Examples
To build the examples locally, run:
yarncd exampleyarnyarn start
Then open http://localhost:8886
in a browser.
Installation
yarn add react-monaco-editor-fix
Using with Webpack
;;; Component { superprops; thisstate = code: '// type your code...' } { console; editor; } { console; } { const code = thisstatecode; const options = selectOnLineNumbers: true ; return <MonacoEditor width="800" height="600" language="javascript" theme="vs-dark" value=code options=options onChange=::thisonChange editorDidMount=::thiseditorDidMount /> ; } ;
Add the Monaco Webpack plugin monaco-editor-webpack-plugin
to your webpack.config.js
:
const MonacoWebpackPlugin = ;moduleexports = plugins: // 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 pathsconst path = ;const APP_DIR = path;const MONACO_DIR = path; 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'
Properties
All the properties below are optional.
-
width
width of editor. Defaults to100%
. -
height
height of editor. Defaults to100%
. -
value
value of the auto created model in the editor. -
defaultValue
the initial value of the auto created model in the editor. -
language
the initial language of the auto created model in the editor. -
theme
the theme of the editor -
options
refer to Monaco interface IEditorConstructionOptions. -
overrideServices
refer 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 tocomponentWillMount
of React). -
editorDidMount(editor, monaco)
an event emitted when the editor has been mounted (similar tocomponentDidMount
of React). -
context
allow 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.
Events & Methods
Refer to Monaco interface IEditor.
Q & A
I don't get syntax highlighting / autocomplete / validation.
Make sure to use the Monaco Webpack plugin or follow the instructions on how to load the ESM version of Monaco.
How to interact with the MonacoEditor instance
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.
How to get value of editor
Using this.refs.monaco.editor.getValue()
or via method of Model
instance:
const model = thisrefsmonacoeditor;const value = model;
Do something before editor mounted
For example, you may want to configure some JSON schemas before editor mounted, then you can go with editorWillMount(monaco)
:
Component { monacolanguagesjsonjsonDefaults; } { return <MonacoEditor language="json" editorWillMount=thiseditorWillMount /> ; }
Use multiple themes
Monaco only supports one theme.
How to use the diff editor
;; Component { 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 /> ; }
License
MIT, see the LICENSE file for detail.