Gathering detailed insights and metrics for react-monaco-editor
Gathering detailed insights and metrics for react-monaco-editor
Gathering detailed insights and metrics for react-monaco-editor
Gathering detailed insights and metrics for react-monaco-editor
@monaco-editor/react
Monaco Editor for React - use the monaco-editor in any React application without needing to use webpack (or rollup/parcel/etc) configuration files / plugins
@lingxiteam/monaco-react
Monaco Editor for React - use the monaco-editor in any React application without needing to use webpack (or rollup/parcel/etc) configuration files / plugins
@codingame/monaco-editor-react
Monaco Editor React component
@redhare/lowcode-plugin-base-monaco-editor
代码编辑组件,monaco-editor 的低代码适配封装
npm install react-monaco-editor
56
Supply Chain
92.3
Quality
84.6
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,850 Stars
838 Commits
379 Forks
21 Watching
7 Branches
76 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (66.26%)
JavaScript (32.37%)
HTML (1.37%)
Cumulative downloads
Total Downloads
Last day
-32.8%
23,783
Compared to previous day
Last week
-12.7%
131,484
Compared to previous week
Last month
19.4%
659,629
Compared to previous month
Last year
37.6%
7,455,904
Compared to previous year
1
3
Monaco Editor for React.
To build the examples locally, run:
1yarn 2cd example 3yarn 4yarn start
Then open http://localhost:8886
in a browser.
1yarn add react-monaco-editor
1import React from 'react'; 2import { createRoot } from "react-dom/client"; 3import MonacoEditor from 'react-monaco-editor'; 4 5class App extends React.Component { 6 constructor(props) { 7 super(props); 8 this.state = { 9 code: '// type your code...', 10 } 11 } 12 editorDidMount(editor, monaco) { 13 console.log('editorDidMount', editor); 14 editor.focus(); 15 } 16 onChange(newValue, e) { 17 console.log('onChange', newValue, e); 18 } 19 render() { 20 const code = this.state.code; 21 const options = { 22 selectOnLineNumbers: true 23 }; 24 return ( 25 <MonacoEditor 26 width="800" 27 height="600" 28 language="javascript" 29 theme="vs-dark" 30 value={code} 31 options={options} 32 onChange={::this.onChange} 33 editorDidMount={::this.editorDidMount} 34 /> 35 ); 36 } 37} 38 39const container = document.getElementById('root'); 40const root = createRoot(container); 41root.render(<App />);
Add the Monaco Webpack plugin monaco-editor-webpack-plugin
to your webpack.config.js
:
1const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin'); 2module.exports = { 3 plugins: [ 4 new MonacoWebpackPlugin({ 5 // available options are documented at https://github.com/microsoft/monaco-editor/blob/main/webpack-plugin/README.md#options 6 languages: ['json'] 7 }) 8 ] 9};
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:
1// Specify separate paths 2const path = require('path'); 3const APP_DIR = path.resolve(__dirname, './src'); 4const MONACO_DIR = path.resolve(__dirname, './node_modules/monaco-editor'); 5 6{ 7 test: /\.css$/, 8 include: APP_DIR, 9 use: [{ 10 loader: 'style-loader', 11 }, { 12 loader: 'css-loader', 13 options: { 14 modules: true, 15 namedExport: true, 16 }, 17 }], 18}, { 19 test: /\.css$/, 20 include: MONACO_DIR, 21 use: ['style-loader', 'css-loader'], 22}
All the properties below are optional.
width
width of editor. Defaults to 100%
.
height
height of editor. Defaults to 100%
.
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 IStandaloneEditorConstructionOptions.
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 to componentWillMount
of React).
editorDidMount(editor, monaco)
an event emitted when the editor has been mounted (similar to componentDidMount
of React).
editorWillUnmount(editor, monaco)
an event emitted before the editor unmount (similar to componentWillUnmount
of React).
Refer to Monaco interface IEditor.
The monaco interfaces available by import
1import { monaco } from 'react-monaco-editor';
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:
1const model = this.refs.monaco.editor.getModel(); 2const value = model.getValue();
For example, you may want to configure some JSON schemas before editor mounted, then you can go with editorWillMount(monaco)
:
1class App extends React.Component { 2 editorWillMount(monaco) { 3 monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ 4 validate: true, 5 schemas: [{ 6 uri: "http://myserver/foo-schema.json", 7 fileMatch: ['*'], 8 schema: { 9 type: "object", 10 properties: { 11 p1: { 12 enum: [ "v1", "v2"] 13 }, 14 p2: { 15 $ref: "http://myserver/bar-schema.json" 16 } 17 } 18 } 19 }] 20 }); 21 } 22 render() { 23 return ( 24 <MonacoEditor language="json" editorWillMount={this.editorWillMount} /> 25 ); 26 } 27}
Monaco only supports one theme.
1import React from 'react'; 2import { MonacoDiffEditor } from 'react-monaco-editor'; 3 4class App extends React.Component { 5 render() { 6 const code1 = "// your original code..."; 7 const code2 = "// a different version..."; 8 const options = { 9 //renderSideBySide: false 10 }; 11 return ( 12 <MonacoDiffEditor 13 width="800" 14 height="600" 15 language="javascript" 16 original={code1} 17 value={code2} 18 options={options} 19 /> 20 ); 21 } 22}
create-react-app
The easiest way to use the react-monaco-editor
with create-react-app
is to use the react-app-rewired project. For setting it up, the following steps are required:
react-app-rewired
: npm install -D react-app-rewired
react-scripts
by react-app-rewired
in the scripts section of your packages.json
config-overrides.js
in the root directory of your project with the following content:1const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin'); 2 3module.exports = function override(config, env) { 4 config.plugins.push(new MonacoWebpackPlugin({ 5 languages: ['json'] 6 })); 7 return config; 8}
For more information checkout the documentation of react-app-rewired
here.
MIT, see the LICENSE file for detail.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
29 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 2/3 approved changesets -- score normalized to 6
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More