├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── circle.yml ├── karma.conf.js ├── package.json ├── src ├── Highlighter.example.css ├── Highlighter.example.js ├── Highlighter.js ├── Highlighter.test.js ├── index.js ├── test-utils.js └── tests.js ├── webpack.config.dev.js ├── webpack.config.dist.base.js ├── webpack.config.dist.cjs.js ├── webpack.config.dist.umd.js ├── webpack.config.test.js ├── webpack.config.website.js ├── website ├── Application.css ├── Application.js ├── index.css ├── index.html └── index.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "stage": 0, 3 | "env": { 4 | "development": { 5 | "plugins": ["react-transform"], 6 | "extra": { 7 | "react-transform": { 8 | "transforms": [{ 9 | "transform": "react-transform-hmr", 10 | "imports": ["react"], 11 | "locals": ["module"] 12 | }] 13 | } 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | /log/* 3 | !/log/.keep 4 | /tmp 5 | *.log 6 | 7 | # Dependency directory 8 | node_modules 9 | 10 | # Test stuff 11 | coverage 12 | 13 | # OS X 14 | .DS_Store 15 | 16 | # Misc 17 | node_modules 18 | npm-debug.log 19 | build 20 | dist 21 | 22 | # IDE 23 | .idea 24 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | website 2 | node_modules 3 | karma.conf.js 4 | src 5 | webpack* 6 | .idea 7 | .babelrc 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Treasure Data 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | React component to highlight words within a larger body of text. 4 | 5 | Check out a demo [here](https://bvaughn.github.io/react-highlight-words). 6 | 7 | ## Usage 8 | 9 | To use it, just provide it with an array of search terms and a body of text to highlight. 10 | 11 | [Try this example in Code Sandbox.](https://codesandbox.io/s/5v8yqoxv7k) 12 | 13 | ```jsx 14 | import React from "react"; 15 | import { createRoot } from "react-dom/client"; 16 | import Highlighter from "react-highlight-words"; 17 | 18 | const root = createRoot(document.getElementById("root")); 19 | root.render( 20 | 26 | ); 27 | ``` 28 | 29 | And the `Highlighter` will mark all occurrences of search terms within the text: 30 | 31 | screen shot 2015-12-19 at 8 23 43 am 32 | 33 | ## Props 34 | 35 | | Property | Type | Required? | Description | 36 | |------------------------|-----------------------------|:---------:|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 37 | | `activeClassName` | String | | The class name to be applied to an active match. Use along with `activeIndex` | 38 | | `activeIndex` | Number | | Specify the match index that should be actively highlighted. Use along with `activeClassName` | 39 | | `activeStyle` | Object | | The inline style to be applied to an active match. Use along with `activeIndex` | 40 | | `autoEscape` | Boolean | | Escape characters in `searchWords` which are meaningful in regular expressions | 41 | | `className` | String | | CSS class name applied to the outer/wrapper `` | 42 | | `caseSensitive` | Boolean | | Search should be case sensitive; defaults to `false` | 43 | | `findChunks` | Function | | Use a custom function to search for matching chunks. This makes it possible to use arbitrary logic when looking for matches. See the default `findChunks` function in [highlight-words-core](https://github.com/bvaughn/highlight-words-core) for signature. Have a look at the [custom findChunks example](https://codesandbox.io/s/k20x3ox31o) on how to use it. | 44 | | `highlightClassName` | String or Object | | CSS class name applied to highlighted text or object mapping search term matches to class names. | 45 | | `highlightStyle` | Object | | Inline styles applied to highlighted text | 46 | | `highlightTag` | Node or String | | Type of tag to wrap around highlighted matches. Defaults to `mark` but can also be a React component (class or functional) | 47 | | `sanitize` | Function | | Process each search word and text to highlight before comparing (eg remove accents); signature `(text: string): string` | 48 | | `searchWords` | Array | ✓ | Array of search words. String search terms are automatically cast to RegExps unless `autoEscape` is true. | 49 | | `textToHighlight` | String | ✓ | Text to highlight matches in | 50 | | `unhighlightClassName` | String | | CSS class name applied to unhighlighted text | 51 | | `unhighlightStyle` | Object | | Inline styles applied to unhighlighted text | 52 | | `unhighlightTag` | Node or String | | Type of tag applied to unhighlighted parts. Defaults to `span` but can also be a React component (class or functional) | 53 | | * | any | | Any other props (such as `title` or `data-*`) are applied to the outer/wrapper `` | 54 | 55 | ## Custom highlight tag 56 | 57 | By default, this component uses an HTML Mark Text element (``) to wrap matched text, but you can inject a custom 58 | tag using the `highlightTag` property. This tag should be a React component that accepts the following properties: 59 | 60 | | Property | Type | Description | 61 | |------------------|--------|------------------------| 62 | | `children` | String | Text to be highlighted | 63 | | `highlightIndex` | Number | Index of matched text | 64 | 65 | For example: 66 | 67 | ```js 68 | const Highlight = ({ children, highlightIndex }) => ( 69 | {children} 70 | ); 71 | ``` 72 | 73 | ## Installation 74 | 75 | ``` 76 | yarn add react-highlight-words 77 | ``` 78 | 79 | ``` 80 | npm i react-highlight-words 81 | ``` 82 | 83 | ## License 84 | 85 | MIT License - fork, modify and use however you want. 86 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | general: 2 | branches: 3 | ignore: 4 | - gh-pages 5 | machine: 6 | node: 7 | version: v5.1.0 8 | 9 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function (config) { 2 | config.set({ 3 | browsers: ['PhantomJS2'], 4 | frameworks: ['mocha'], 5 | files: ['src/tests.js'], 6 | preprocessors: { 7 | 'src/tests.js': ['webpack', 'sourcemap'] 8 | }, 9 | junitReporter: { 10 | outputDir: (process.env.CIRCLE_TEST_REPORTS || 'public') + '/karma', 11 | suite: 'karma' 12 | }, 13 | singleRun: true, 14 | plugins: [ 15 | require('karma-mocha'), 16 | require('karma-webpack'), 17 | require('karma-spec-reporter'), 18 | require('karma-junit-reporter'), 19 | require('karma-sourcemap-loader'), 20 | require('karma-phantomjs2-launcher') 21 | ], 22 | webpack: require('./webpack.config.test') 23 | }) 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-highlight-words", 3 | "version": "0.21.0", 4 | "description": "React component to highlight words within a larger body of text", 5 | "main": "dist/main.js", 6 | "scripts": { 7 | "build": "npm run build:website && npm run build:dist", 8 | "build:website": "npm run clean:website && cross-env NODE_ENV=production webpack --config webpack.config.website.js -p --bail", 9 | "build:dist": "npm run clean:dist && cross-env NODE_ENV=production webpack --config webpack.config.dist.cjs.js --bail && cross-env NODE_ENV=production webpack --config webpack.config.dist.umd.js --bail", 10 | "clean": "npm run clean:website && npm run clean:dist", 11 | "clean:website": "rimraf build", 12 | "clean:dist": "rimraf dist", 13 | "deploy": "gh-pages -d build", 14 | "lint": "standard", 15 | "prebuild": "npm run lint", 16 | "prepublishOnly": "npm run build", 17 | "postpublish": "npm run deploy", 18 | "start": "webpack-dev-server --hot --inline --config webpack.config.dev.js", 19 | "test": "cross-env NODE_ENV=test karma start", 20 | "watch": "watch 'clear && npm run lint -s && npm run test -s' src" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/bvaughn/react-highlight-words.git" 25 | }, 26 | "author": "Brian Vaughn", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/bvaughn/react-highlight-words/issues" 30 | }, 31 | "homepage": "https://github.com/bvaughn/react-highlight-words#readme", 32 | "keywords": [ 33 | "react", 34 | "reactjs", 35 | "react-component", 36 | "highlighter", 37 | "highlight", 38 | "text", 39 | "words", 40 | "matches", 41 | "substring", 42 | "occurrences", 43 | "search" 44 | ], 45 | "standard": { 46 | "parser": "babel-eslint", 47 | "ignore": [ 48 | "build", 49 | "dist", 50 | "node_modules" 51 | ], 52 | "global": [ 53 | "afterAll", 54 | "afterEach", 55 | "beforeAll", 56 | "beforeEach", 57 | "describe", 58 | "it", 59 | "jasmine" 60 | ] 61 | }, 62 | "devDependencies": { 63 | "babel": "^5.8.34", 64 | "babel-core": "^5.8.34", 65 | "babel-eslint": "^4.1.6", 66 | "babel-loader": "^5.4.0", 67 | "babel-plugin-react-transform": "^1.1.1", 68 | "cross-env": "^5.1.3", 69 | "css-loader": "^0.23.0", 70 | "cssnext": "^1.8.4", 71 | "cssnext-loader": "^1.0.1", 72 | "expect.js": "^0.3.1", 73 | "gh-pages": "^0.8.0", 74 | "html-webpack-plugin": "^1.7.0", 75 | "karma": "^0.13.15", 76 | "karma-junit-reporter": "^0.3.8", 77 | "karma-mocha": "^0.2.1", 78 | "karma-phantomjs2-launcher": "^0.3.2", 79 | "karma-sourcemap-loader": "^0.3.6", 80 | "karma-spec-reporter": "0.0.23", 81 | "karma-webpack": "^1.7.0", 82 | "latinize": "^0.2.0", 83 | "lodash": "^4.17.10", 84 | "mocha": "^2.3.4", 85 | "phantomjs2": "^2.0.2", 86 | "react": "^19.0.0", 87 | "react-dom": "^19.0.0", 88 | "react-transform-hmr": "^1.0.1", 89 | "rimraf": "^2.4.4", 90 | "standard": "^5.4.1", 91 | "style-loader": "^0.13.0", 92 | "watch": "^0.16.0", 93 | "webpack": "^1.12.9", 94 | "webpack-dev-server": "^1.14.0", 95 | "worker-loader": "^0.7.0" 96 | }, 97 | "dependencies": { 98 | "highlight-words-core": "^1.2.0", 99 | "memoize-one": "^4.0.0" 100 | }, 101 | "peerDependencies": { 102 | "react": "^0.14.0 || ^15.0.0 || ^16.0.0-0 || ^17.0.0-0 || ^18.0.0-0 || ^19.0.0-0" 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/Highlighter.example.css: -------------------------------------------------------------------------------- 1 | .Header { 2 | margin: 0 0 .5rem; 3 | font-size: 1.25em; 4 | font-weight: normal; 5 | } 6 | 7 | .Input { 8 | width: 100%; 9 | border: 1px solid #78909c; 10 | line-height: 1.4; 11 | padding: .3em .5em; 12 | border-radius: .3em; 13 | margin-bottom: 1em; 14 | font-size: 1em; 15 | } 16 | 17 | .Highlight { 18 | background-color: #ffd54f; 19 | } 20 | 21 | .Active { 22 | background-color: #f48f42; 23 | } 24 | 25 | .Footer { 26 | margin-bottom: 0; 27 | } 28 | 29 | .Row { 30 | display: flex; 31 | flex-direction: row; 32 | align-items: flex-start; 33 | justify-content: space-between; 34 | } 35 | 36 | .FirstColumn, 37 | .SecondColumn { 38 | flex: 1 1 auto; 39 | } 40 | 41 | .FirstColumn { 42 | margin-right: 0.5rem; 43 | } 44 | 45 | .SecondColumn { 46 | margin-left: 0.5rem; 47 | } 48 | -------------------------------------------------------------------------------- /src/Highlighter.example.js: -------------------------------------------------------------------------------- 1 | import latinize from 'latinize' 2 | import React, { Component } from 'react' 3 | import Highlighter from './Highlighter' 4 | import styles from './Highlighter.example.css' 5 | 6 | export default class HighlighterExample extends Component { 7 | constructor (props) { 8 | super(props) 9 | 10 | this.state = { 11 | searchText: 'and or the', 12 | textToHighlight: `When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.`, 13 | activeIndex: -1, 14 | caseSensitive: false 15 | } 16 | } 17 | render () { 18 | const { ...props } = this.props 19 | const { activeIndex, caseSensitive, searchText, textToHighlight } = this.state 20 | const searchWords = searchText.split(/\s/).filter(word => word) 21 | 22 | return ( 23 |
24 |
25 |
26 |

27 | Search terms 28 |

29 | this.setState({ searchText: event.target.value })} 34 | /> 35 |
36 |
37 |

38 | Active Index 39 |

40 | this.setState({ activeIndex: parseInt(event.target.value, 10) })} 45 | type='number' 46 | /> 47 |
48 |
49 |

50 | Case Sensitive? 51 |

52 | this.setState({ caseSensitive: event.target.checked })} 57 | type='checkbox' 58 | /> 59 |
60 |
61 | 62 |

63 | Body of Text 64 |

65 |