JSPM

  • Created
  • Published
  • Downloads 197336
  • Score
    100M100P100Q166693F
  • License MIT

Monaco Editor for React

Package Exports

  • react-monaco-editor
  • react-monaco-editor/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-monaco-editor) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-monaco-editor

Monaco Editor for React.

NPM version Downloads

react-monaco-editor

Examples

To build the examples locally, run:

npm install
cd examples
npm start

Then open http://localhost:8886 in a browser.

Installation

npm install react-monaco-editor

Usage

import React from 'react';
import { render } from 'react-dom';
import MonacoEditor from 'react-monaco-editor';

class App extends React.Component {
  onDidMount(editor) {
    console.log('onDidMount', editor);
    editor.focus();
  }
  onChange(newValue, e) {
    console.log('onChange', newValue, e);
  }
  render() {
    const initialCode = '// type your code...';
    const options = {
      selectOnLineNumbers: true
    };
    return (
      <MonacoEditor
        width="800"
        height="600"
        language="javascript"
        value={initialCode}
        options={options}
        onChange={::this.onChange}
        onDidMount={::this.onDidMount}
      />
    );
  }
}

render(
  <App />,
  document.getElementById('root')
);

Note that react-monaco-editor requires webpack and its plugin copy-webpack-plugin, below is an example for webpack.config.js:

const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
  plugins: [
    new CopyWebpackPlugin([
      {
        from: 'node_modules/monaco-editor/min/vs',
        to: 'vs',
      }
    ])
  ]
};

Fill from field with the actual path of monaco-editor package in node_modules.

Properties

  • width width of editor. Defaults to 100%.
  • height height of editor. Defaults to 500.
  • value the initial value of the auto created model in the editor.
  • language the initial language of the auto created model in the editor.
  • options refer to Monaco interface IEditorOptions.
  • onChange an event emitted when the content of the current model has changed.
  • onDidMount an event emitted when the editor has been mounted (similar to componentDidMount of React).

Events & Methods

Refer to Monaco interface IEditor.

Q & A

How to interact with the MonacoEditor instance

Using the first parameter of onDidMount, or using a ref (e.g. <MonacoEditor ref="monaco">) after onDidMount 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 = this.refs.monaco.editor.getModel();
const value = model.getValue();

License

MIT, see the LICENSE file for detail.