rich-textarea
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
- Clone this repo.
- Run
npm install
. - Commit your fix.
- Make a PR and confirm all the CI checks passed.