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

@letientai299/react-decorated-text

v0.1.1

Published

React component to apply multiple text highlight/decorations within a larger body of text

Downloads

25

Readme

React Decorated Text

Unstyled React component to apply multiple text highlight/decorations within a larger body of text.

Live demo (Source)

Demo

Installation

npm i @letientai299/react-decorated-text

Usage

import DecoratedText, { match } from '@letientai299/react-decorated-text';

function Demo() {
  const text =
    'Give a man a program, and frustrate him for a day. ' +
    'Teach a man to program, frustrate him for a lifetime.';
  const decors = [
    // render to <mark>{children}</mark> by default
    ...match(text, 'man'),
    // search using regexp, render custom element that wrap the matches
    ...match(text, /p\w+m/, (p) => (
      <span style={{ color: 'red' }}>{p.children}</span>
    )),
  ];
  return <DecoratedText text={text} decors={decors} />;
}

DecoratedText takes these props:

  • text: required, the body of text to display.
  • decors: not required, list of Decor to be rendered within the text. Internally, the component will compute the overlap of those decors and generate the sub components
  • as: not required, can be used to change the default HTML tag (span) to wrap the text.

The match function is just a helper to support most common use cases: search the text for some words or a regexp and render those matches using a React components. Its output is Decor[].

For advanced use cases, instead of match, you might want to implement your own function to compute Decor[]. Below is how Decor is defined (simplified).

type Decor = {
  range: [number, number];
  render?: (props: PropsWithChildren) => ReactElement;
};

See the live demo source code for some example.

Design note

This is designed to build a filterable menu, list or tree, in which the item text are: unstructured, immutable, can have some other overlapping decorations. One of such example is the project explorer in Jetbrains IDE.

Jetbrains explorere

This is not designed to work with structured text (e.g. code), or dynamic content (inside a web-based editor). Such cases requires deeper knowledge of the text (e.g. Abstract syntax tree, tokens), thus, requires other tools.

Some other libraries, such as react-highlighter and react-highlight-words, only support single match query and single style against all matches. This one is more low level, but also more powerful:

  • Can render the matches using any React element
  • Can use multiple search query, or implement your own search.

That said, this design leaves the many responsibilities to you, the users:

  • Make sure the searching is correct and fast
  • Handle accessibility for text segments, especially in those overlapped segments. This page might be helpful: How to Accessibly Split Text

License

MIT