UNPKG

2.12 kBMarkdownView Raw
1<img src="https://cloud.githubusercontent.com/assets/29597/11913937/0d2dcd78-a629-11e5-83e7-6a17b6d765a5.png" width="260" height="260">
2
3React component to highlight words within a larger body of text.
4
5Check out a demo [here](https://bvaughn.github.io/react-highlight-words).
6
7## Usage
8
9To use it, just provide it with an array of search terms and a body of text to highlight:
10
11```html
12<Highlighter
13 highlightClassName='YourHighlightClass'
14 searchWords={['and', 'or', 'the']}
15 textToHighlight="The dog is chasing the cat. Or perhaps they're just playing?"
16/>
17```
18
19And the `Highlighter` will mark all occurrences of search terms within the text:
20
21<img width="368" alt="screen shot 2015-12-19 at 8 23 43 am" src="https://cloud.githubusercontent.com/assets/29597/11914033/e3c319f6-a629-11e5-896d-1a5ce22c9ea2.png">
22
23## Props
24
25| Property | Type | Required? | Description |
26|:---|:---|:---:|:---|
27| activeClassName | String | | The class name to be applied to an active match. Use along with `activeIndex` |
28| activeIndex | String | | Specify the match index that should be actively highlighted. Use along with `activeClassName` |
29| autoEscape | Boolean | | Escape characters which are meaningful in regular expressions |
30| className | String | | CSS class name applied to the outer/wrapper `<span>` |
31| highlightClassName | String | | CSS class name applied to highlighted text |
32| highlightStyle | Object | | Inline styles applied to highlighted text |
33| highlightTag | String | | Type of tag to wrap around highlighted matches; defaults to `mark` |
34| sanitize | Function | | Process each search word and text to highlight before comparing (eg remove accents); signature `(text: string): string` |
35| searchWords | Array<String> | ✓ | Array of search words |
36| textToHighlight | String | ✓ | Text to highlight matches in |
37| unhighlightClassName | String | | CSS class name applied to unhighlighted text |
38| unhighlightStyle | Object | | Inline styles applied to unhighlighted text |
39
40## Installation
41```
42npm i --save react-highlight-words
43```
44
45## License
46MIT License - fork, modify and use however you want.