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

@simbathesailor/use-intersection-observer

v0.1.6

Published

An intersection observer hook for all your purposes

Downloads

20

Readme

use-intersection-observer 🚦🚦

An intersection observer hook for all your purposes

✅ Built on new flashy Reactjs hooks.

✅ No assumption on the elements you want to observe.

✅ Completely configurable.

✅ < 1kb gzipped

Installing

If using npm:




npm i @simbathesailor/use-intersection-observer --save


If using yarn:




yarn add @simbathesailor/use-intersection-observer


Demo

Demo App

Full Codesandbox Code

Usage

import { useIntersectionObserver } from '@simbathesailor/use-intersection-observer';

const defaultVisibilityCondition = (entry: IntersectionObserverEntry) => {
  if (entry.intersectionRatio >= 1) {
    return true;
  }

  return false;
};

const defaultOptions = {
  rootMargin: '0px 0px 0px 0px',

  threshold: '0, 1',

  when: true,

  visibilityCondition: defaultVisibilityCondition,
};

const App = () => {
  const [isVisible, boxElemCallback, rootCallbackRef] = useIntersectionObserver(
    defaultOptions
  );

  return (
    <div className="App">
      <h1>See for the visibility of box at bottom of page</h1>

      <h2>Start scroling down to the visibility change!</h2>

      <div ref={boxElemCallback} className="box">
        {isVisible ? 'Box is visible' : 'Box is not visible'}
      </div>

      {isVisible ? 'Box is visible' : 'Box is not visible'}
    </div>
  );
};

Props

| Props | IsMandatory | Type | Default | Description | | --------------------------- | ----------- | -------- | --------------------------------------- | --------------------------------------------------------------------------- | | options.rootMargin | No | string | '0px 0px 0px 0px' | rootMargin top, left, bottom, right | | options.threshold | No | string | '0, 1' | proportion of element intersecting required to trigger the callback | | options.when | No | boolean | true | The flag which which make the observer active or inactive. | | options.visibilityCondition | No | Function | (entry) => entry.intersectionRatio >= 1 | Return boolean. It sets visibility to true when this function returns true. |

Return Data Types and Description

The hook returns an array. Let's say that array name is Arr.

| Index | Name | Type | Description | | ------ | ---------------------- | ---------------------------- | ----------------------------------------------------------------------------------------- | | Arr[0] | isVisible | boolean | Tells whether the target element is visible or not | | Arr[1] | targetElementRef | Function | The target element ref, add it to target element | | Arr[2] | rootElementCallbackRef | Function | The root element ref, add it to root element or can just leave it if document is the root | | Arr[3] | observer | Intersection observer Object | Can be used to un-observe the target. |

Concept

Intersection Observer API has a very good support across browsers . Here is the link for MDN Intersection observer. You can read about it and understand why it is performant. And the best part is it has a polyfill also : intersection observer polyfill

Work to do

  • TestCases.
  • Other examples

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

simbathesailor

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Contributors

Thanks goes to these wonderful people (emoji key):