0.1.2 • Published 4 years ago

@walltowall/highlighter v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Highlighter

React component for rendering highlighted words from a string.

Installation

# npm
npm install @walltowall/highlighter

# yarn
yarn add @walltowall/highlighter

Usage

Supply your searchTerm and the React component you wish to render with renderHighlight for any matched word. The render function will receive the word as its first argument and the current index match as its second argument.

import { Highlighter } from '@walltowall/highlighter'

const Example = () => {
  return (
    <Highlighter
      searchTerm="word"
      renderHighlight={(word, idx) => <em key={idx}>{word}</em>}
    >
      Highlight your desired word! Any word will be wrapped with your
      `renderHighlight`!
    </Highlighter>
  )
}

<Highlighter> children must be a plain string. Providing a component such as a <div> will result in an error.

API

PropRequired?Description
searchTermYesThe word to search for in the provided string.
renderHighlightYesA render prop to render any word match.