Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-visible-observer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-visible-observer

![npm version](https://img.shields.io/npm/v/react-visible-observer)

  • 1.0.4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-42.86%
Maintainers
1
Weekly downloads
 
Created
Source

React Visible Observer

npm version

简体中文

React Visible Observer is a React component library based on the Intersection Observer API, designed to monitor when elements enter or leave the viewport (visible area). This library can trigger callback functions when elements become visible or invisible, making it ideal for implementing lazy loading, animation triggering, and other interactive features dependent on element visibility.

Installation

You can install this library via npm or yarn:

npm install react-visible-observer

or

yarn add react-visible-observer

Quick Start

1. Monitor a Single Element

import React, { useRef } from 'react';
import { useIntersectionObserver } from 'react-visible-observer';

const MyComponent = () => {
  const ref = useRef(null);
  const onVisibilityChange = (isVisible) => {
    console.log(`The element is now ${isVisible ? 'visible' : 'hidden'}`);
  };

  useIntersectionObserver(ref, onVisibilityChange);

  return (
    <div ref={ref} style={{ height: '100px', backgroundColor: 'blue' }}>
      This is a div that will be observed.
    </div>
  );
};

export default MyComponent;

2. Monitor a List of Elements, Typically Used for Infinite Scrolling

import React, { useRef } from 'react';
import useIntersectionObserver from 'react-visible-observer';

const MyComponent = () => {
  const listRef = useRef([]);

  const onVisibilityChange = (isVisible, entry) => {
    console.log(`The element is now ${isVisible ? 'visible' : 'hidden'}`);
    const id = entry.target.id; // Get the ID of the element
    console.log(`${id} is now visible`);
  };

  useIntersectionObserver(listRef, onVisibilityChange);

  return (
    <div>
      <div ref={el => listRef.current[0] = el} id="item1">Item 1</div>
      <div ref={el => listRef.current[1] = el} id="item2">Item 2</div>
      <div ref={el => listRef.current[2] = el} id="item3">Item 3</div>
    </div>
  );
};

export default MyComponent;

API

useIntersectionObserver(ref, onVisibilityChange, onEntryUpdate, options)

OptionDescriptionTypeRequiredDefault
refThe ref of the DOM element(s) to observe.React.RefObject or Array[React.RefObject]RequiredNone
onVisibilityChangeCallback function called when the observed element goes from invisible to visible or from visible to invisible. Receives two parameters: isVisible (boolean) indicating whether the element is visible, and entry (IntersectionObserverEntry) providing detailed information.FunctionRequiredundefined
onEntryUpdateCallback function called when there is any update, whether the element is visible or not. Receives one parameter: entry (IntersectionObserverEntry).FunctionOptionalundefined
optionsConfiguration options for IntersectionObserver.ObjectOptional{ root: null, rootMargin: '0px', threshold: 0.1 }

options Configuration Options

OptionDescriptionTypeRequiredDefault
rootThe root element of the observer. Default is null, which uses the viewport as the root element.ElementOptionalnull
rootMarginA string specifying the margins of the root element. Used to increase or decrease the area considered as the viewport.StringOptional'0px'
thresholdA number or an array of numbers defining the percentage of target visibility that triggers the callback.NumberorArrayOptional0.1

IntersectionObserverEntry Properties

  • boundingClientRect: A DOMRectReadOnly object representing the current size and position of the target element's bounding box in viewport coordinates.
  • intersectionRatio: A floating-point number indicating the ratio of the intersection area to the target's bounding box or to the root's bounding box, depending on the value of the root property.
  • intersectionRect: A DOMRectReadOnly object representing the size and position of the intersection rectangle's intersection area.
  • isIntersecting: A Boolean value indicating whether the target element intersects with the root element.
  • rootBounds: A DOMRectReadOnly object representing the bounding box of the root element in viewport coordinates. If there is no root element, this value is null.
  • target: An Element object representing the target element being observed.
  • time: A floating-point number representing the time at which the intersection occurred, in milliseconds.

License

React Visible Observer is released under the MIT License. See the LICENSE file for details.

FAQs

Package last updated on 28 May 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc