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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nimibyte/intersection-hook

v1.0.1-beta.6

Published

React Hook for Tracking Section Visibility in the Viewport

Readme

Intersection Hook for React

This package provides a set of tools to easily implement Intersection Observer in React applications. It allows you to register elements and track their visibility in the viewport. This is ideal for creating dynamic navigation menus that highlight the active section as the user scrolls.

example

Installation

You can install the package using npm or yarn.

Using npm:

npm install @nimibyte/intersection-hook

Using yarn:

yarn add @nimibyte/intersection-hook

Usage

Step 1: Set up the Provider (IntersectionProvider)

Wrap your application or component with the IntersectionProvider component to enable the useIntersection hook in any part of the component hierarchy. The provider handles the intersection state and configuration.

import { IntersectionProvider } from '@nimibyte/intersection-hook';

const App = () => {
  return (
    <IntersectionProvider config={{ threshold: 0.8, scrollBehavior: 'smooth' }}>
      <YourComponent />
    </IntersectionProvider>
  );
};

Step 2: Use useIntersection in Your Component

Once your application is wrapped by the IntersectionProvider, you can use the useIntersection hook to register elements and track intersections.

Menu Component

This component renders a navigation menu with links to the registered sections. It highlights the active link based on the section currently visible.

import { useIntersection } from '@nimibyte/intersection-hook';

const Menu = () => {
  const { sections, activeSection, scrollTo } = useIntersection();

  return (
    <>
      {sections?.map(({ id, label }) => (
        <a
          key={id}
          onClick={scrollTo(id)}
          style={{ color: id === activeSection ? 'red' : 'yellow', cursor: 'pointer' }}
          data-testid={`menu-${id}`}
        >
          {label}
        </a>
      ))}
    </>
  );
};

Content Component

The Content component registers the sections and displays them on the page. The sections are automatically detected as they scroll into the visible area of the browser.

import { useIntersection } from '@nimibyte/intersection-hook';

const Content = () => {
  const { register } = useIntersection();

  return (
    <div>
      {Object.entries(SECTIONS).map(([id, label]) => (
        <div ref={register({ id, label })} data-testid={`content-${id}`} key={id}>
          {label}
        </div>
      ))}
    </div>
  );
};

API

IntersectionProvider

The context provider that enables the useIntersection hook in child components.

Props:

•	children: The components to be wrapped.
•	config: Optional configuration to customize the intersection observer behavior:
•	threshold (default: 0.5): Percentage of the section visible in the viewport to consider it “intersected”.
•	scrollBehavior (default: 'smooth'): The scroll behavior when clicking on a menu link (can be 'auto', 'smooth', or 'instant').

useIntersection

A hook that provides access to the intersection functionality within components.

Returns:

•	register: A function to register an element for observation. It accepts an object with the properties id and label.
•	activeSection: The id of the currently visible section in the viewport.
•	sections: An array of registered sections, each containing the id and label properties.
•	scrollTo: A function to scroll the view to a given section by its id.

Contributing

If you would like to contribute to this project, feel free to fork it and submit pull requests. Please ensure to test your changes before submitting them.

License

MIT License. See the LICENSE file for more details.