JSPM

use-in-viewport

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q12303F
  • License MIT

React hook for check if the current element in viewport

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-in-viewport) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    use-in-viewport

    React hook for check if the current element in viewport.

    Author

    Getting started

    Installation

    npm install use-in-viewport or yarn add use-in-viewport

    Example

    import React, { useRef } from 'react';
    import useInViewport from 'use-in-viewport';
    
    function Example() {
      const elementRef = useRef(null);
      const inViewport = useInViewport(elementRef);
    
      return (
        <div ref={elementRef}>
          Element is {inViewport ? 'in viewport' : 'not in viewport'}
        </div>
      );
    }
    
    export default Example;

    Options

    • root: element scroll area (by default document.body)
    • rootMargin: element margin from root (by default 0px)
    • threshold: element visibility threshold (by default 0)
    import React, { useRef } from 'react';
    import useInViewport from 'use-in-viewport';
    
    function Example() {
      const containerRef = useRef(null);
      const elementRef = useRef(null);
      const inViewport = useInViewport(elementRef, {
        root: containerRef,
        rootMargin: '20px',
        threshold: 1,
      });
    
      return (
        <div ref={containerRef}>
          <div ref={elementRef}>
            Element is {inViewport ? 'in viewport' : 'not in viewport'}
          </div>
        </div>
      );
    }
    
    export default Example;

    Copyright (c) 2022 Martik Avagyan