Giter Club home page Giter Club logo

rich-textarea's Introduction

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.
  • 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

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.

Props

For other props not mentioned below will be passed to textarea.

Name Type Default Description
value string required Same as textarea but only string
defaultValue - - Omitted for simplicity of logic
children (value: string) => React.ReactNode undefined This function should return ReactNodes which texts are positioned the same as textarea (see examples for detailed usage). Currently limited event handlers will work for the nodes (onClick, onMouseOver, onMouseOut, onMouseMove, onMouseDown and onMouseUp)
autoHeight boolean undefined If true, textarea height is automatically resized and height of style prop does not work. Set maxHeight to style prop if you need limit.
onSelectionChange (pos: CaretPosition, value: string) => void undefined Called when selection in textarea changes. It gives position of caret at the time, which is useful to position menu.

Methods

Name Type Description
ref React.RefObject<HTMLTextAreaElement> Ref of textarea
selectionStart number Same as textarea but has handling of composition event
selectionEnd number Same as textarea but has handling of composition event
focus Same as textarea
blur Same as textarea
select Same as textarea
setSelectionRange Same as textarea
setRangeText Same as textarea but has fallback to document.execCommand("insertText")

Contribute

All contributions are welcome! If you have any feature requests or improvements, please create a issue or 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.

rich-textarea's People

Contributors

inokawa avatar renovate[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.