• Stars
    star
    1,103
  • Rank 40,630 (Top 0.9 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple no-frills code editor with syntax highlighting

react-simple-code-editor

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

Simple no-frills code editor with syntax highlighting.

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

  • Modular syntax highlighting with third party library
  • Indent line or selected text by pressing tab key, with customizable indentation
  • Automatic indent on new lines
  • Wrap selected text in parens, brackets, or quotes
  • Undo whole words instead of letter by letter
  • Accessible, use Ctrl+Shift+M (Mac) / Ctrl+M to toggle capturing tab key

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:

  • value (string): Current value of the editor i.e. the code to display. This must be a controlled prop.
  • onValueChange (string => mixed): Callback which is called when the value of the editor changes. You'll need to update the value prop when this is called.
  • highlight (string => string | React.Node): Callback which will receive text to highlight. You'll need to return an HTML string or a React element with syntax highlighting using a library such as prismjs.
  • tabSize (number): The number of characters to insert when pressing tab key. For example, for 4 space indentation, tabSize will be 4 and insertSpaces will be true. Default: 2.
  • insertSpaces (boolean): Whether to use spaces for indentation. Default: true. If you set it to false, you might also want to set tabSize to 1.
  • ignoreTabKey (boolean): Whether the editor should ignore tab key presses so that keyboard users can tab past the editor. Users can toggle this behaviour using Ctrl+Shift+M (Mac) / Ctrl+M manually when this is false. Default: false.
  • padding (number): Optional padding for code. Default: 0.
  • textareaId (string): An ID for the underlying textarea, can be useful for setting a label.
  • textareaClassName (string): A className for the underlying textarea, can be useful for more precise control of its styles.
  • preClassName (string): A className for the underlying pre, can be useful for more precise control of its styles.

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:

  • The syntax highlighted code cannot have different font family, font weight, font style, line height etc. for its content. Since the editor works by aligning the highlighted code over a <textarea>, changing anything that affects the layout can misalign it.
  • The custom undo stack is incompatible with undo/redo items browser's context menu. However, other full featured editors don't support browser's undo/redo menu items either.
  • The editor is not optimized for performance and large documents can affect the typing speed.
  • We hide text in the textarea using -webkit-text-fill-color: transparent, which works in all modern browsers (even non-webkit ones such as Firefox and Edge). On IE 10+, we use color: transparent which doesn't hide the cursor. Text may appear bolder in unsupported browsers.

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

More Repositories

1

react-native-tab-view

A cross-platform Tab View component for React Native
TypeScript
5,134
star
2

quik

🚀 A quick way to prototype and build apps with React and Babel with zero-setup.
JavaScript
477
star
3

react-navigation-addons

Add-ons for React Navigation
JavaScript
282
star
4

web-worker-proxy

A better way of working with web workers
JavaScript
225
star
5

PocketGear

A clean and simple Pokédex app for Pokémon GO
TypeScript
197
star
6

monaco-editor-boilerplate

A simple boilerplate for Monaco editor with React.
JavaScript
187
star
7

gtk-theme-config

A tool to configure GTK theme colors.
Vala
89
star
8

babel-test

An opinionated library to make testing babel plugins easier.
JavaScript
80
star
9

react-boilerplate

An simple webpack boilerplate for React projects.
JavaScript
61
star
10

react-navigation-native-modal

React Navigation integration for React Native's Modal component
TypeScript
60
star
11

babel-plugin-css-prop

Babel plugin to transpile `css` prop to a styled component. (Experimental)
JavaScript
55
star
12

babel-plugin-optional-require

Babel plugin to optionaly require modules
JavaScript
52
star
13

react-native-in-app-purchase

IAP for React Native
Java
49
star
14

eslint-config-satya164

An ESLint config with automatic overrides for common environments such as TypeScript, Jest etc.
JavaScript
49
star
15

jest-file-snapshot

Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest
JavaScript
41
star
16

babel-plugin-object-styles-to-template

Babel plugin to transpile object styles to template literal
JavaScript
35
star
17

typescript-template

Template repository for TypeScript projects with Babel
JavaScript
32
star
18

use-latest-callback

TypeScript
28
star
19

gjs-helpers

GNOME JavaScript helpers for async tasks
JavaScript
26
star
20

react-native-image-chooser

A React Native module to show system Image chooser. Currently only supports Android.
Java
26
star
21

Evolve

Evolve is a simple and minimal light theme designed to be easy on the eyes.
CSS
24
star
22

github-transfer-issues

Bulk transfer GitHub issues with puppeteer
JavaScript
23
star
23

emoji-invaders

Simple Emoji Invaders game
JavaScript
14
star
24

animated-to-reanimated

Helper to convert an Animated value to Reanimated and vice-versa
Java
12
star
25

grrr

A small tool to generate gresource files
JavaScript
11
star
26

minimal.widget

A minimal widget for Ãœbersicht which displays a panel on top right and apps list on top left
JavaScript
11
star
27

pigment

Color information, conversion and manipulation library
JavaScript
9
star
28

react-canvas-renderer

Custom React renderer which renders to <canvas />
JavaScript
8
star
29

random-color

Show a random color on tap.
Objective-C
6
star
30

publish-monorepo-packages

Tool to make easier publish packages in monorepo to the registry.
TypeScript
6
star
31

react-native-share

Java
5
star
32

warn-once

Show a warning once
JavaScript
5
star
33

language-x

An all new programming language
JavaScript
4
star
34

snack-modules

JavaScript
4
star
35

meme-generator

Meme Generator app built with Expo
JavaScript
4
star
36

json2scss

Convert JSON config files to SCSS!
JavaScript
4
star
37

croma

JavaScript
4
star
38

reanimated-2-drawer

TypeScript
3
star
39

huematic

Generate color schemes based on algorithms
JavaScript
3
star
40

react-native-jsitestlibrary

Java
3
star
41

actions-playground

Playing with GitHub actions
3
star
42

git-hooks-scripts

Run custom scripts as Git hooks
JavaScript
3
star
43

turbo-module-example

Example for backward compatible Turbo module
Java
2
star
44

drop-shadows

Generate drop shadows
OCaml
2
star
45

webrtc-messaging

TypeScript
2
star
46

magnetic-field-meter

Detect Magnetic field using device's Magnetometer
JavaScript
2
star
47

simple-website-boilerplate

A simple website boilerplate with BrowserSync and Sass
HTML
2
star
48

remark-syntax-highlight

(WIP) Syntax highlight code blocks with a custom highlight function in remark
JavaScript
2
star
49

turtle-v2-example

Java
2
star
50

timezones

JavaScript
1
star
51

dotfiles

JavaScript
1
star
52

animated-helpers

Helpers for React Native's Animated API
TypeScript
1
star
53

node-app-tasks

The action allows to run any npm/yarn command or a task defined in package.json
Shell
1
star
54

keycode

Find keyCode of a key
HTML
1
star