2.0.2 • Published 1 year ago

react-highlight-string v2.0.2

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

Tiny react component to highlight given string in text. Written in TypeScript. Based on highlight-string.

Sandbox with code example.

Usage

Just pass a text and an array of strings to search for.

import ReactDOM from 'react-dom/client'
import { Highlighter } from 'react-highlight-string'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
  <Highlighter text="Hello word" substrings={['hello']} />,
)

API

Highlighter

ParameterTypeDescription
textstringText to search and highlight. Required.
substringsArray<string>Array of strings to search for. Required.
caseSensitivebooleanPass true if search should be case sensitive. false by default.
sanitize(text: string) => stringSanitize function.
activeClassNamestringThe class name to be applied to an active match. Use along with activeIndex.
activeIndexnumberSpecify the match index that should be actively highlighted. Use along with activeClassName.
activeStyleCSSPropertiesThe inline style to be applied to an active match. Use along with activeIndex.
classNamestringCSS class name applied to the outer/wrapper span.
highlightClassNamestringCSS class name applied to highlighted text or object mapping search term matches to class names.
highlightStyleCSSPropertiesInline styles applied to highlighted text.
highlightComponentkeyof ReactHTML \| ComponentType<HighlightComponentProps>Type of tag to wrap around highlighted matches. Defaults to mark but can also be a React component (class or functional).
unhighlightClassNamestringCSS class name applied to unhighlighted text.
unhighlightStyleCSSPropertiesInline styles applied to unhighlighted text.
unhighlightComponentkeyof ReactHTML \| ComponentType<HTMLAttributes<Element>>Type of tag applied to unhighlighted parts. Defaults to React Fragment but can also be a React component (class or functional).
*Any other html attribute for wrapper span.

HighlightComponent

ParameterTypeDescription
activeIndexnumberPassed from parent component.
highlightIndexnumberPassed from parent component.
classNamestringCSS class or classes that will contain activeClassName and highlightClassName if they were provided. Passed from parent component.

License

MIT