1.0.4 • Published 1 year ago

react-visible-observer v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

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.

1.0.4

1 year ago

1.0.2

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago