• Stars
    star
    296
  • Rank 140,464 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

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

Repository Details

A small customizable textarea for React to colorize, highlight, decorate texts, offer autocomplete and much more.

rich-textarea

npm npm bundle size npm check demo

A small customizable textarea for React to colorize, highlight, decorate texts, offer autocomplete and much more.

Demo

https://inokawa.github.io/rich-textarea/

Features

  • Styleable texts: Not just highlighting texts like similar libraries, this library also supports colorizing, decorating and more. Regex or any tokenizers can be used.
  • Easy to interact with events: You can get caret position and can catch some mouse events on texts, which are useful to display something reflects user actions.
  • Compatible with textarea: Except added features, this library is designed to behave as native textarea as much as possible. If not worked properly, please report it in an issue or PR.
  • Out of the box integration: Supports formik and react-hook-form. Supports SSR in Next.js.
  • IME composition handling: IME related events have some cross browser problems. This library handles them for easy to use.
  • Lightweight: Trying to support many usecases but also keep bundle size small. Currently about 3kB (gzipped).

Motivation

Sometimes we need customized text editor in web. However creating it with raw contenteditable is so hard to do properly and editor frameworks are usually too heavy... Maybe you really need is just a textarea with highlighting and some hovered menus, but native textarea and many of textarea libraries are far from it because of the limited customizability. This library is aiming to solve the problem.

Install

npm install rich-textarea

Requirements

  • react >= 16.14

If you use ESM and webpack 5, use react >= 18 to avoid Can't resolve react/jsx-runtime error.

Usage

You can create your own render function

import { useState } from "react";
import { RichTextarea } from "rich-textarea";

export const App = () => {
  const [text, setText] = useState("Lorem ipsum");

  return (
    <RichTextarea
      value={text}
      style={{ width: "600px", height: "400px" }}
      onChange={(e) => setText(e.target.value)}
    >
      {(v) => {
        return v.split("").map((t, i) => (
          <span key={i} style={{ color: i % 2 === 0 ? "red" : undefined }}>
            {t}
          </span>
        ));
      }}
    </RichTextarea>
  );
};

or you can use helper for regex.

import { useState } from "react";
import { RichTextarea, createRegexRenderer } from "rich-textarea";

const renderer = createRegexRenderer([
  [/[A-Z][a-z]+/g, { borderRadius: "3px", backgroundColor: "#d0bfff" }],
]);

export const App = () => {
  const [text, setText] = useState("Lorem ipsum");

  return (
    <RichTextarea
      value={text}
      style={{ width: "600px", height: "400px" }}
      onChange={(e) => setText(e.target.value)}
    >
      {renderer}
    </RichTextarea>
  );
};

And see examples for more usages.

Documentation

Contribute

All contributions are welcome. If you find a problem, feel free to create an issue or a PR.

Making a Pull Request

  1. Clone this repo.
  2. Run npm install.
  3. Commit your fix.
  4. Make a PR and confirm all the CI checks passed.

More Repositories

1

virtua

A zero-config, fast and small (~3kB) virtual list (and grid) component for React, Vue, Solid and Svelte.
TypeScript
1,166
star
2

react-native-react-bridge

An easy way to integrate your React (or Preact/React Native Web) app into React Native app with WebView.
TypeScript
253
star
3

remark-slate-transformer

remark plugin to transform remark syntax tree (mdast) to Slate document tree, and vice versa. Made for WYSIWYG markdown editor.
TypeScript
105
star
4

react-native-wasm

A polyfill to use WebAssembly in React Native.
Kotlin
83
star
5

lang-box

๐Ÿ’ป Update a pinned gist to contain languages of your recent commits in GitHub
JavaScript
52
star
6

react-animatable

Tiny(~1kB) animation hooks for React, built on Web Animations API.
TypeScript
38
star
7

remark-docx

remark plugin to compile markdown to docx (Microsoft Word, Office Open XML).
TypeScript
33
star
8

rust-editor

An implementation of text editor with Rust/WebAssembly.
Rust
25
star
9

remark-pdf

remark plugin to compile markdown to pdf.
TypeScript
24
star
10

react-use-d3

A small React hook to use D3 in declarative way, for data visualization & flexible animation.
TypeScript
7
star
11

remark-extract-toc

remark plugin to store table of contents
JavaScript
6
star
12

react-reconciler-practice

A practice with react-reconciler.
TypeScript
4
star
13

monaco-diff

Text diff library exported from monaco-editor-core, which is core of VS Code.
TypeScript
4
star
14

dotparser-normalized

A wrapper of dotparser to parse GraphViz dot file and collect nodes / edges.
TypeScript
3
star
15

react-typescript-example

Sandbox for React and TypeScript
TypeScript
3
star
16

wasm-ts-runtime

[WIP]A toy WebAssembly runtime implementation with TypeScript.
TypeScript
2
star
17

inokawa.github.io

GitHub Pages
TypeScript
2
star
18

rust-browser

A toy browser engine implementation with Rust.
Rust
2
star
19

react-native-with-web-example

A setup of React Native with React Native Web.
Java
2
star
20

apollo-graphql-practice

JavaScript
1
star
21

node-bundler

A toy JavaScript bundler with Node.js.
JavaScript
1
star
22

react-grpc-web

[WIP] Typed React hooks for gRPC-web.
TypeScript
1
star
23

tweened

A simple, declarative and composable animation library for React.
TypeScript
1
star
24

webgl-practice

WebGL sandbox
TypeScript
1
star
25

reagram

React component to render customizable graph/diagram.
TypeScript
1
star
26

arduino-sketches

Sandbox for Arduino
C++
1
star
27

rust-wasm-example

Some examples of WebAssembly generated by Rust.
Rust
1
star
28

d3-sandbox

D3.js Practice
JavaScript
1
star
29

monaco-editor-node

Wrapper of monaco-editor to use some functionalities in Node.js.
JavaScript
1
star
30

react-native-accelerate

Java
1
star
31

babel-plugin-evaluate-modules

A babel plugin to evaluate modules at build-time.
TypeScript
1
star
32

web3d-sandbox

WebGL/WebGPU/GLSL Practice
JavaScript
1
star
33

canvas-game

A toy 2D shooting game built on pure Canvas API.
TypeScript
1
star
34

webgl-mdn-practice

JavaScript
1
star
35

webgpu-practice

WebGPU sandbox
TypeScript
1
star