1.2.1 • Published 6 months ago

react-highlight-search v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

React-Highlight-Search

React Highlight Search is an out-of-the-box solution for deep text search through the DOM tree. It provides a lightweight and fast ReactJS component (19.9 kB, reduced to 7.0 kB gzip), built with Vanilla JS and featuring zero dependencies.

Installation

The easiest way to install react-highlight-search is by using either npm or yarn commands:

npm install react-highlight-search

or

yarn add react-highlight-search

Basic Usage in React App

Live Demo

Implementing deep search through a nested DOM is incredibly simple with the react-highlight-search package.

import React, { useState, useCallback } from "react";
import { HighlightSearchWrapper } from "react-highlight-search";

const ExampleWithSearch = () => {
  const [searchString, setSearchString] = useState(""); // State variable to hold the search string
  const [matchData, setMatchData] = useState({
    wrapperIndex: 0,
    matchesFound: 0,
    matchParentElement: null,
  }); // Search data returned by the component

  const handleInputChange = useCallback((e) => {
    setSearchString(e.target.value);
  }, []);

  return (
    <>
      <input onInput={handleInputChange} />
      <HighlightSearchWrapper
        searchString={searchString} // Pass the search string
        onMatchData={setMatchData} // Pass the function to update search data on a successful search
      >
        <div className={"example-of-nesting-1"}>
            Hello World!
            <div className={"example-of-nesting-2}>
                Other text example
                <div className={"example-of-nesting-3}>
                    Search Me!
                    <ul>
                        <li>Search Me..</li>
                        <li>
                            Search Me Again!
                            <span>Search Me!</span>
                        </li>
                        <li>Hello World!</li>
                        <li>
                            <div>
                                <h4>Other text example</h4>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
      </HighlightSearchWrapper>

      // Block shows example of the search result data
      <div style={{ width: "350px" }}>
        <div>Wrapper Index: {matchData.wrapperIndex}</div>
        <div>Matches Found: {matchData.matchesFound}</div>
        <div>
          Match Parent Element:
          {matchData.matchParentElement
            ? matchData.matchParentElement.toString()
            : ""}
        </div>
      </div>
    </>
  );
};

export default ExampleWithSearch;

Props

NameRequiredTypeDefaultDescription
searchStringYesstringundefinedThe text to search for.
setTriggerSearchNoFunctionundefinedThe function to trigger search manually.
ignoreCaseNobooleantrueIgnore case sensitive of the search string.
searchMinLengthNonumber1The minimum length of text required to start the search.
onMatchDataNoFunctionundefinedCallback function triggered on a successful search.
spanClassNameNostring"hlsearch-span-el"Class name applied to the elements added to the DOM for highlighting text.
indexNonumber0Index value returned in the onMatchData callback. Useful for managing multiple components.

// TODO: issues with dots

License

MIT Licensed. Copyright (c) Vladyslav Dotsenko 2025.


1.2.0

7 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

1.0.4

8 months ago

1.2.1

6 months ago

1.0.3

8 months ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago