npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rich-textarea

v0.26.4

Published

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

Downloads

45,141

Readme

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.

[!NOTE] You may also like https://github.com/inokawa/edix

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: Works as both controlled and uncontrolled. Supports formik, react-hook-form, @tanstack/react-form and more. Supports SSR. Supports form with Server Actions in Next.js and action in Remix.
  • 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 ~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

Controlled

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>
  );
};

Uncontrolled

import { RichTextarea } from "rich-textarea";

export const App = () => {
  return (
    <RichTextarea
      defaultValue="Lorem ipsum"
      style={{ width: "600px", height: "400px" }}
    >
      {(v) => {
        return v.split("").map((t, i) => (
          <span key={i} style={{ color: i % 2 === 0 ? "red" : undefined }}>
            {t}
          </span>
        ));
      }}
    </RichTextarea>
  );
};

Regex helper

You can use helper for regex if you don't want to create your own render function.

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>
  );
};

Next.js Server Actions

// _components/Textarea.tsx
"use client";
import { RichTextarea, RichTextareaProps } from "rich-textarea";

export const Textarea = (
  props: Omit<RichTextareaProps, "children" | "ref">
) => {
  return (
    <RichTextarea {...props}>
      {(v) => {
        return v.split("").map((t, i) => (
          <span key={i} style={{ color: i % 2 === 0 ? "red" : undefined }}>
            {t}
          </span>
        ));
      }}
    </RichTextarea>
  );
};

// page.tsx in App Router of Next.js
import { Textarea } from "../_components/Textarea";

async function hello(formData: FormData) {
  "use server";
  console.log(formData.get("hello"));
}

export default () => {
  return (
    <div>
      <div>this is server!</div>
      <form action={hello}>
        <Textarea defaultValue="Lorem ipsum" name="hello" />
        <button type="submit">submit</button>
      </form>
    </div>
  );
};

Remix action

import type { ActionFunction } from "@remix-run/node";
import { Form } from "@remix-run/react";
import { RichTextarea, RichTextareaProps } from "rich-textarea";

const Textarea = (props: Omit<RichTextareaProps, "children" | "ref">) => {
  return (
    <RichTextarea {...props}>
      {(v) => {
        return v.split("").map((t, i) => (
          <span key={i} style={{ color: i % 2 === 0 ? "red" : undefined }}>
            {t}
          </span>
        ));
      }}
    </RichTextarea>
  );
};

export const action: ActionFunction = async ({ request }) => {
  console.log((await request.formData()).get("hello"));
};

export default () => {
  return (
    <Form method="post">
      <Textarea defaultValue="Lorem ipsum" name="hello" />
      <button type="submit">submit</button>
    </Form>
  );
};

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. Fork this repo.
  2. Run npm install.
  3. Commit your fix.
  4. Make a PR and confirm all the CI checks passed.