Socket
Socket
Sign inDemoInstall

@simbathesailor/use-intersection-observer

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @simbathesailor/use-intersection-observer

An intersection observer hook for all your purposes


Version published
Maintainers
1
Install size
0.980 MB
Created

Readme

Source

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

PropsIsMandatoryTypeDefaultDescription
options.rootMarginNostring'0px 0px 0px 0px'rootMargin top, left, bottom, right
options.thresholdNostring'0, 1'proportion of element intersecting required to trigger the callback
options.whenNobooleantrueThe flag which which make the observer active or inactive.
options.visibilityConditionNoFunction(entry) => entry.intersectionRatio >= 1Return 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.

IndexNameTypeDescription
Arr[0]isVisiblebooleanTells whether the target element is visible or not
Arr[1]targetElementRefFunctionThe target element ref, add it to target element
Arr[2]rootElementCallbackRefFunctionThe root element ref, add it to root element or can just leave it if document is the root
Arr[3]observerIntersection observer ObjectCan 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):

Anil kumar chaudhary
Anil kumar Chaudhary

💻 🤔 🎨 📖 🐛

Keywords

FAQs

Last updated on 22 Jan 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc