Socket
Socket
Sign inDemoInstall

react-monaco-editor

Package Overview
Dependencies
Maintainers
3
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-monaco-editor

Monaco Editor for React


Version published
Weekly downloads
127K
decreased by-7.74%
Maintainers
3
Weekly downloads
 
Created

What is react-monaco-editor?

The react-monaco-editor package is a React component for integrating the Monaco Editor, which is the code editor that powers Visual Studio Code, into React applications. It provides a rich set of features for code editing, including syntax highlighting, IntelliSense, and more.

What are react-monaco-editor's main functionalities?

Basic Usage

This code demonstrates the basic usage of the react-monaco-editor package. It sets up a Monaco Editor instance with JavaScript syntax highlighting and a dark theme. The editor's content is managed via the component's state.

import React from 'react';
import MonacoEditor from 'react-monaco-editor';

class MyEditor 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);
    this.setState({ code: newValue });
  }

  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.bind(this)}
        editorDidMount={this.editorDidMount.bind(this)}
      />
    );
  }
}

export default MyEditor;

Customizing Editor Options

This example shows how to customize the editor options, such as making the editor read-only and disabling the minimap. The editor is set to use a light theme and displays some read-only code.

import React from 'react';
import MonacoEditor from 'react-monaco-editor';

class CustomEditor extends React.Component {
  render() {
    const options = {
      selectOnLineNumbers: true,
      readOnly: true,
      minimap: { enabled: false }
    };
    return (
      <MonacoEditor
        width="800"
        height="600"
        language="javascript"
        theme="vs-light"
        value={'// read-only code'}
        options={options}
      />
    );
  }
}

export default CustomEditor;

Handling Editor Events

This example demonstrates how to handle editor events, such as cursor position changes. The editorDidMount method is used to attach an event listener to the editor instance.

import React from 'react';
import MonacoEditor from 'react-monaco-editor';

class EventHandlingEditor extends React.Component {
  editorDidMount(editor, monaco) {
    editor.onDidChangeCursorPosition((e) => {
      console.log('Cursor position changed:', e.position);
    });
  }

  render() {
    return (
      <MonacoEditor
        width="800"
        height="600"
        language="javascript"
        theme="vs-dark"
        value={'// type your code...'}
        editorDidMount={this.editorDidMount.bind(this)}
      />
    );
  }
}

export default EventHandlingEditor;

Other packages similar to react-monaco-editor

Keywords

FAQs

Package last updated on 13 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc