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

v2.0.2

Published

Custom React Hook for initializing IntersectionObserver on any node ref

Downloads

4

Readme

react-useintersection

Description

react-useintersection is a custom React Hook that allows for initializing IntersectionObserver on any node ref. With the IntersectionanchorObserver returned from the hook, components can be styled on the fly or related JavaScript behavior can be triggered.

Be aware of the browser compatiblity of IntersectionObserver, namely that IE11 does not support it. If necessary, consider using the intersection-observer package for a polyfill.

Installation

npm i 'react-useintersection'

Usage

Here's an example of how you can use IntersectionObserver to track when the user has scrolled from the top of the page, but this is just a simple example of how powerful IntersectionObserver is. Visit the MDN docs for ideas, including lazy loading of images, infinite scrolling, and others.

This specific example is how the author of this package achieves a menu bar that is transparent while at the top of the page and transitions to opaque when the user scrolls down. Check out the source of xer site to get the full context including CSS.

import React, { useEffect, useRef } from 'react'
import useIntersection from 'react-useintersection'

import MenuBar from './MenuBar'

const Layout = ({ children }) => {
  const anchor = useRef(null)
  const [setNode, anchorObserver] = useIntersection()

  useEffect(() => {
    setNode(anchor.current)
  })

  return (
    <>
      <div id="top-of-page-anchor" ref={anchor} />
      <MenuBar isAtTop={anchorObserver.isIntersecting} />
      {children}
    </>
  )
}

Astute readers may be concerned about memory leaks; efforts have been made to nip potential leaks in the bud, namely by using [node, options.root, options.rootMargin, options.threshold] as the second argument within the useEffect of useIntersection.

License

Implementation based on code samples by Justin Travis Waith-Mair from his Medium article and used with permission. All original code is released under the Blue Oak Model License, a copyleft written with the intent to be a more legally defensible version of the MIT license.

Contributing

Contributions are welcome in the form of reporting issues and submitting pull requests. Contributors agree to follow the Code of Conduct outlined in CODE_OF_CONDUCT.md.