Home

Awesome

react-simple-code-editor

Build Status MIT License Version Bundle size (minified + gzip)

Simple no-frills code editor with syntax highlighting.

<a href="https://raw.githubusercontent.com/react-simple-code-editor/react-simple-code-editor/main/demo/demo.gif"><img src="https://raw.githubusercontent.com/react-simple-code-editor/react-simple-code-editor/main/demo/demo.gif" width="400"></a>

Why

Several browser based code editors such as Ace, CodeMirror, Monaco etc. provide the ability to embed a full-featured code editor in your web page. However, if you just need a simple editor with syntax highlighting without any of the extra features, they can be overkill as they don't usually have a small bundle size footprint. This library aims to provide a simple code editor with syntax highlighting support without any of the extra features, perfect for simple embeds and forms where users can submit code.

Features

Installation

npm install react-simple-code-editor

or

yarn add react-simple-code-editor

Usage

You need to use the editor with a third party library which provides syntax highlighting. For example, it'll look like following with prismjs:

import React from 'react';
import Editor from 'react-simple-code-editor';
import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';
import 'prismjs/themes/prism.css'; //Example style, you can use another

function App() {
  const [code, setCode] = React.useState(
    `function add(a, b) {\n  return a + b;\n}`
  );
  return (
    <Editor
      value={code}
      onValueChange={code => setCode(code)}
      highlight={code => highlight(code, languages.js)}
      padding={10}
      style={{
        fontFamily: '"Fira code", "Fira Mono", monospace',
        fontSize: 12,
      }}
    />
  );
}

Note that depending on your syntax highlighter, you might have to include additional CSS for syntax highlighting to work.

Props

The editor accepts all the props accepted by textarea. In addition, you can pass the following props:

Demo

react-simple-code-editor.github.io/react-simple-code-editor

How it works

It works by overlaying a syntax highlighted <pre> block over a <textarea>. When you type, select, copy text etc., you interact with the underlying <textarea>, so the experience feels native. This is a very simple approach compared to other editors which re-implement the behaviour.

The syntax highlighting can be done by any third party library as long as it returns HTML and is fully controllable by the user.

The vanilla <textarea> doesn't support inserting tab characters for indentation, so we re-implement it by listening to keydown events and programmatically updating the text. One caveat with programmatically updating the text is that we lose the undo stack, so we need to maintain our own undo stack. As a result, we can also implement improved undo behaviour such as undoing whole words similar to editors like VSCode.

Limitations

Due to the way it works, it has certain limitations:

Contributing

While developing, you can run the example app to test your changes:

yarn example

Make sure your code passes TypeScript and ESLint. Run the following to verify:

yarn typescript
yarn lint

To fix formatting errors, run the following:

yarn lint -- --fix
<!-- badges -->