npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-highlight-search

v1.0.4

Published

Text highlight with deep search through DOM elements.

Downloads

369

Readme

React-Highlight-Search

React Highlight Search is a lightweight and fast ReactJS component (17.7 kB > 6.3 kB gzip), built with Vanilla JS and featuring 0 dependencies, designed for deep text search through the DOM tree.

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

In the current version, all search text must be at the same nesting level. Refer to the example below.

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

const ExampleWithSearch = () => {
  const [searchString, setSearchString] = useState(""); // Controlled string to search for
  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} />
      <div>
        <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"}>
            <div className={"example-of-nesting-2"}>
              <div className={"example-of-nesting-3"}>
                <ul>
                  // Search text must be at the same nesting level
                  <li>Search Me!</li>
                  <li>Search Me Again!</li>
                  <li>Hello World!</li>
                  <li>Other text example</li>
                </ul>
              </div>
            </div>
          </div>
        </HighlightSearchWrapper>
      </div>

      // 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

| Name | Required | Type | Default | Description | | ------------- | ------------- | ------------- | ------------- | ------------- | | searchString | Yes | string | undefined | The text to search for. | searchMinLength | No | number | 1 | The minimum length of text required to start the search. | onMatchData | No | Function | undefined | Callback function triggered on a successful search. | spanClassName | No | string | "highlightsearch-selected-element" | Class name applied to the elements added to the DOM for highlighting text. | index | No | number | 0 | Index value returned in the onMatchData callback. Useful for managing multiple components.

License

MIT Licensed. Copyright (c) Vladyslav Dotsenko 2024.