Top Related Projects
In-browser code editor (version 5, legacy)
In-browser code editor (version 5, legacy)
CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
Codemirror integrated components for React
Simple no-frills code editor with syntax highlighting
Monaco Editor for React.
Quick Overview
React-CodeMirror is a React component wrapper for the CodeMirror text editor. It provides an easy way to integrate CodeMirror into React applications, allowing developers to create powerful code editing experiences with syntax highlighting, auto-completion, and other advanced features.
Pros
- Seamless integration with React applications
- Easy to use and configure
- Supports all CodeMirror features and options
- Regular updates and maintenance
Cons
- Limited documentation for advanced use cases
- Some users report performance issues with large files
- Occasional compatibility issues with certain React versions
- Learning curve for customizing complex features
Code Examples
- Basic usage:
import React from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
function Editor() {
return (
<CodeMirror
value="console.log('Hello, world!');"
height="200px"
extensions={[javascript({ jsx: true })]}
onChange={(value, viewUpdate) => {
console.log('value:', value);
}}
/>
);
}
- Using multiple languages:
import React from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { html } from '@codemirror/lang-html';
function MultiLanguageEditor() {
return (
<CodeMirror
value="<div>Hello, world!</div>"
height="200px"
extensions={[javascript({ jsx: true }), html()]}
onChange={(value, viewUpdate) => {
console.log('value:', value);
}}
/>
);
}
- Customizing theme:
import React from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { dracula } from '@uiw/codemirror-theme-dracula';
function ThemedEditor() {
return (
<CodeMirror
value="const greeting = 'Hello, world!';"
height="200px"
theme={dracula}
extensions={[javascript({ jsx: true })]}
onChange={(value, viewUpdate) => {
console.log('value:', value);
}}
/>
);
}
Getting Started
To use React-CodeMirror in your project, follow these steps:
- Install the package and its dependencies:
npm install @uiw/react-codemirror @codemirror/lang-javascript
- Import and use the component in your React application:
import React from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
function App() {
return (
<CodeMirror
value="console.log('Hello, world!');"
height="200px"
extensions={[javascript({ jsx: true })]}
onChange={(value, viewUpdate) => {
console.log('value:', value);
}}
/>
);
}
export default App;
This will create a basic CodeMirror editor with JavaScript syntax highlighting. You can customize it further by adding more extensions, changing the theme, or adjusting other props as needed.
Competitor Comparisons
In-browser code editor (version 5, legacy)
Pros of CodeMirror 5
- More comprehensive and feature-rich, offering a wider range of functionalities
- Better documentation and community support
- Can be used with various frameworks, not limited to React
Cons of CodeMirror 5
- Larger bundle size, which may impact performance
- Steeper learning curve due to its extensive API
- Requires more setup and configuration for React integration
Code Comparison
react-codemirror:
import CodeMirror from 'react-codemirror';
<CodeMirror
value={code}
onChange={updateCode}
options={{
mode: 'javascript',
theme: 'monokai'
}}
/>
CodeMirror 5:
import CodeMirror from 'codemirror';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/theme/monokai.css';
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'javascript',
theme: 'monokai'
});
The react-codemirror example shows a more React-friendly implementation, while the CodeMirror 5 example demonstrates its vanilla JavaScript usage. react-codemirror simplifies the integration process for React applications, whereas CodeMirror 5 provides more flexibility but requires additional setup for React projects.
In-browser code editor (version 5, legacy)
Pros of CodeMirror 5
- More comprehensive and feature-rich, offering a wider range of functionalities
- Better documentation and community support
- Can be used with various frameworks, not limited to React
Cons of CodeMirror 5
- Larger bundle size, which may impact performance
- Steeper learning curve due to its extensive API
- Requires more setup and configuration for React integration
Code Comparison
react-codemirror:
import CodeMirror from 'react-codemirror';
<CodeMirror
value={code}
onChange={updateCode}
options={{
mode: 'javascript',
theme: 'monokai'
}}
/>
CodeMirror 5:
import CodeMirror from 'codemirror';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/theme/monokai.css';
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'javascript',
theme: 'monokai'
});
The react-codemirror example shows a more React-friendly implementation, while the CodeMirror 5 example demonstrates its vanilla JavaScript usage. react-codemirror simplifies the integration process for React applications, whereas CodeMirror 5 provides more flexibility but requires additional setup for React projects.
CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
Pros of react-codemirror (uiwjs)
- More actively maintained with frequent updates
- Better TypeScript support and type definitions
- Wider range of language modes and themes available out-of-the-box
Cons of react-codemirror (uiwjs)
- Slightly larger bundle size
- Less established community and fewer third-party extensions
- May have some compatibility issues with older React versions
Code Comparison
react-codemirror (JedWatson):
import CodeMirror from 'react-codemirror';
<CodeMirror
value={this.state.code}
onChange={this.updateCode}
options={{
mode: 'javascript',
theme: 'monokai',
}}
/>
react-codemirror (uiwjs):
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
<CodeMirror
value={code}
height="200px"
extensions={[javascript()]}
onChange={(value, viewUpdate) => {
setCode(value);
}}
/>
The uiwjs version offers a more modern API with better TypeScript support and a more flexible extension system. However, the JedWatson version may be simpler for basic use cases and has a longer history in the React ecosystem.
Codemirror integrated components for React
Pros of react-codemirror2
- Better TypeScript support and type definitions
- More frequent updates and active maintenance
- Improved performance and stability
Cons of react-codemirror2
- Slightly steeper learning curve for new users
- Some breaking changes when migrating from react-codemirror
Code Comparison
react-codemirror:
import CodeMirror from 'react-codemirror';
<CodeMirror
value={this.state.code}
onChange={this.updateCode}
options={{mode: 'javascript'}}
/>
react-codemirror2:
import { Controlled as CodeMirror } from 'react-codemirror2';
<CodeMirror
value={this.state.code}
onBeforeChange={(editor, data, value) => {
this.setState({code: value});
}}
options={{mode: 'javascript'}}
/>
The main difference in usage is that react-codemirror2 uses a Controlled component and requires an onBeforeChange
prop instead of onChange
. This allows for more fine-grained control over the editor's state and behavior.
Both libraries provide a React wrapper for the popular CodeMirror editor, but react-codemirror2 offers more modern features and better integration with current React practices. However, users familiar with react-codemirror may need to adjust to the new API when switching to react-codemirror2.
Simple no-frills code editor with syntax highlighting
Pros of react-simple-code-editor
- Lightweight and minimalistic, with fewer dependencies
- Easy to customize and integrate into existing projects
- Supports real-time syntax highlighting
Cons of react-simple-code-editor
- Limited built-in features compared to CodeMirror
- Lacks advanced code editing capabilities (e.g., code folding, multiple cursors)
- Smaller community and fewer extensions available
Code Comparison
react-simple-code-editor:
import Editor from 'react-simple-code-editor';
import { highlight, languages } from 'prismjs/components/prism-core';
<Editor
value={code}
onValueChange={code => setCode(code)}
highlight={code => highlight(code, languages.js)}
padding={10}
style={{
fontFamily: '"Fira code", "Fira Mono", monospace',
fontSize: 12,
}}
/>
react-codemirror:
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
<CodeMirror
value={code}
height="200px"
extensions={[javascript({ jsx: true })]}
onChange={(value, viewUpdate) => {
console.log('value:', value);
}}
/>
The code comparison shows that react-simple-code-editor requires manual setup for syntax highlighting, while react-codemirror provides more built-in functionality and language support out of the box.
Monaco Editor for React.
Pros of react-monaco-editor
- More feature-rich, offering advanced IDE-like capabilities
- Better support for TypeScript and JavaScript development
- Regularly updated and maintained by a larger community
Cons of react-monaco-editor
- Larger bundle size, which may impact load times
- Steeper learning curve due to more complex API
- May be overkill for simpler code editing needs
Code Comparison
react-monaco-editor:
import MonacoEditor from 'react-monaco-editor';
<MonacoEditor
width="800"
height="600"
language="javascript"
theme="vs-dark"
value={code}
options={options}
onChange={handleEditorChange}
/>
react-codemirror:
import CodeMirror from 'react-codemirror';
<CodeMirror
value={code}
options={{
mode: 'javascript',
theme: 'monokai',
lineNumbers: true
}}
onChange={handleEditorChange}
/>
react-monaco-editor offers more advanced features and better support for modern web development, particularly for TypeScript and JavaScript. However, it comes with a larger bundle size and a steeper learning curve. react-codemirror is lighter and simpler to use, making it suitable for basic code editing needs. The choice between the two depends on the specific requirements of your project and the level of functionality you need in your code editor component.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Codemirror
The excellent CodeMirror editor as a React.js component.
Demo & Examples
Live demo: JedWatson.github.io/react-codemirror
To build the examples locally, run:
npm install
npm start
Then open localhost:8000
in a browser.
Installation
The easiest way to use codemirror is to install it from NPM and include it in your own React build process (using Browserify, Webpack, etc).
You can also use the standalone build by including dist/react-codemirror.js
in your page. If you use this, make sure you have already included React, and it is available as a global variable.
npm install react-codemirror --save
Usage
Require the CodeMirror component and render it with JSX:
var React = require('react');
var CodeMirror = require('react-codemirror');
var App = React.createClass({
getInitialState: function() {
return {
code: "// Code",
};
},
updateCode: function(newCode) {
this.setState({
code: newCode,
});
},
render: function() {
var options = {
lineNumbers: true,
};
return <CodeMirror value={this.state.code} onChange={this.updateCode} options={options} />
}
});
React.render(<App />, document.getElementById('app'));
Include the CSS
Ensure that CodeMirror's stylesheet codemirror.css
is loaded.
If you're using LESS (similar for Sass) you can import the css directly from the codemirror package, as shown in example.less:
@import (inline) "./node_modules/codemirror/lib/codemirror.css";
If you're using Webpack with the css loader, you can require the codemirror css in your application instead:
require('codemirror/lib/codemirror.css');
Alternatively, you can explicitly link the codemirror.css
file from the CodeMirror project in your index.html file, e.g <link href="css/codemirror.css" rel="stylesheet">
.
Methods
focus
focuses the CodeMirror instancegetCodeMirror
returns the CodeMirror instance, available .
You can interact with the CodeMirror instance using a ref
and the getCodeMirror()
method after the componentDidMount
lifecycle event has fired (including inside the componentDidMount
event in a parent Component).
Properties
autoFocus
Boolean
automatically focuses the editor when it is mounted (default false)autoSave
Boolean
automatically persist changes to underlying textarea (default false)className
String
adds a custom css class to the editorcodeMirrorInstance
Function
provides a specific CodeMirror instance (defaults torequire('codemirror')
)defaultValue
String
provides a default (not change tracked) value to the editorname
String
sets the name of the editor input fieldoptions
Object
options passed to the CodeMirror instanceonChange
Function (newValue)
called when a change is madeonCursorActivity
Function (codemirror)
called when the cursor is movedonFocusChange
Function (focused)
called when the editor is focused or loses focusonScroll
Function (scrollInfo)
called when the editor is scrolledpreserveScrollPosition
Boolean=false
preserve previous scroll position after updating valuevalue
String
the editor value
See the CodeMirror API Docs for the available options.
Using Language Modes
Several language modes are included with CodeMirror for syntax highlighting.
By default (to optimise bundle size) all modes are not included. To enable syntax highlighting:
- install the
codemirror
package dependency (in addition toreact-codemirror
) - require the language modes you wish to make available after you require
react-codemirror
itself - set the
mode
option in theoptions
object
var React = require('react');
var CodeMirror = require('react-codemirror');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/xml/xml');
require('codemirror/mode/markdown/markdown');
<CodeMirror ... options={{
mode: 'javascript',
}} />
See the example source for a reference implementation including JavaScript and markdown syntax highlighting.
License
Copyright (c) 2016 Jed Watson. MIT Licensed.
Top Related Projects
In-browser code editor (version 5, legacy)
In-browser code editor (version 5, legacy)
CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
Codemirror integrated components for React
Simple no-frills code editor with syntax highlighting
Monaco Editor for React.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot