1.0.3 • Published 1 year ago

react-multi-highlight v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-multi-highlight

NPM version NPM downloads

react component to highlight multiple words in text according to each config

Image text

Usage

try example in Code Sandbox:

Edit react-multi-highlight

basic usage:

import Highlighter from 'react-multi-highlight';

export default () => (
  <Highlighter
    config={[
      {
        word: 'and',
        className: 'a',
        style: {
          color: 'blue',
        },
      },
      {
        word: 'nd light-off tim',
        className: 'c',
        style: {
          textDecoration: 'underline',
          textDecorationColor: 'green',
        },
      },
    ]}
    highlightTag="span"
    normalTag="span"
    text="Life, thin and light-off time and time again"
  />
);

Props

PropertyTypeRequired?Description
textstringtext string will be highlight
configIHighlightConfig[] IHighlightConfigconfig or config array, match word in text to be highlight or ranges to be highlight
highlightTagkeyof HTMLElementTagNameMaphtml element tag to wrap highlighted text
normalTagkeyof HTMLElementTagNameMaphtml element tag to wrap normal text
caseSensitivebooleanword match is case sensitive, default true

IHighlightConfig

PropertyTypeRequired?Description
wordstringword to generate a regex, then match to highlight
classNamestringclassName of the highlight tag
stylestringstyle of the highlight tag
rangesstart: number, end: numberranges in text will be highlight, only effective without word configuration

Development

# install dependencies
$ pnpm install

# develop library by docs demo
$ pnpm start

# build library source code
$ pnpm run build

# build library source code in watch mode
$ pnpm run build:watch

# build docs
$ pnpm run docs:build

# check your project for potential problems
$ pnpm run doctor

LICENSE

MIT