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

@alexvcasillas/use-observer

v1.1.2

Published

React Intersection Observer implementation done right!

Downloads

446

Readme

Use Observer

The Use Observer package aims to provide a proper implementation of the Intersection Observer API for your React components with ease.

Motivation

I've used a few libraries that implemented the Intersection Observer API but they were all doing something unwated and that was that my components where all getting re-rendered constantly due to a wrong implementation of the state within the hook. I was working trying to fix this issue because it was really bothering me and was causing some performance issues due to re-rendering (lots of) components on my screen at the same time so I started writing my own Intersection Observer API implementation to remove this other libraries from my dependencies so I finally came up with a solution that would also be a great fit for your projects if you need this Intersection Observer API to work with your React components to animate something when within the viewport.

Installation

yarn add @alexvcasillas/use-observer
npm i -s @alexvcasillas/use-observer

Usage

import { useObserver } from "@alexvcasillas/use-observer";

function MyComponent() {
  const { inView, ref } = useObserver({ threshold: 0.5 });

  return (
    <div ref={ref}>
      Is in view? {inView}
    </div>
  )
}

API

The API of useObserver is pretty straightforward, I aimed to keep a consistent and simple API so you would only need to pass an object with the following properties:

type ObserverType {
  threshold: number,
  rootMargin?: string,
}

The threshold property indicates how much of the component needs to be displayed within the viewport to trigger the animation. It will take a number from 0 to 1 to indicate the percentage of the component that's on the viewport, meaning that 0.5 will be equals to the 50% of the component.

The rootMargin property is a string with syntax similar to that of the CSS margin property. Each side of the rectangle represented by rootMargin is added to the corresponding side in the root element's bounding box before the intersection test is performed. This lets you, for example, adjust the bounds outward so that the target element is considered 100% visible even if a certain number of pixels worth of width or height is clipped away, or treat the target as partially hidden if an edge is too close to the edge of the root's bounding box. Reference.

useObserver will return an object with two properties:

{
  inView: boolean,
  ref: React.MutableRefObject
}

With the inView property you will be able to determine that the given element is in viewport within the given threshold.

You will have to use the ref property to create a reference to the element within your React component that you'd like to track (as seen in the example above).

Used by

  • React Spring POP!: Animate React elements when they enter the viewport with physics based animations.

If you'd like to add your library that uses useObserver feel free to add a PR modifying the above line and I will gladly merge it :)