1.0.1 • Published 2 years ago

react-scroll-info v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-scroll-info(useScrollInfo)

NPM

npm version

A custom hook for creating to access the scroll info while user scrolling on page in react components.

Features

  • You control the UI - useScrollInfo provides the necessary info like isPageTop, isPageBottom, isScrolledUp, isScrolledDown, scrolledPosition, documentHeight

  • Written in Vanilla Javascript

  • Cross-browser compatibility

Demo

CodeSandbox Demo

Installation

$ npm install react-scroll-info
# or
$ yarn add react-scroll-info

Usage

import React, { useEffect } from 'react'
import useScrollInfo from 'react-scroll-info'
function Demo() {
 const {
    isPageBottom,
    isPageTop,
    isScrolledDown,
    isScrolledUp,
    scrolledPosition,
    documentHeight
  } = useScrollInfo();

  useEffect(() => {
    if (isPageBottom) {
      console.log("page reached to bottom");
    }
  }, [isPageBottom]);

  return (
    <div>
    <button>
        {isScrolledDown ? 'Up' : 'Down'}
    </button>
    {
        `isPageBottom: ${isPageBottom}
        isPageTop: ${isPageTop}
        isScrolledDown: ${isScrolledDown}
        isScrolledUp: ${isScrolledUp}
        scrolledPosition: ${scrolledPosition}
        documentHeight: ${documentHeight}`
    }
    </div>
  )
}

Detect Page Bottom Before Some Pixels

import React, { useEffect } from 'react'
import useScrollInfo from 'react-scroll-info'
function Demo() {
 const {
    isPageBottom,
    isPageTop,
    isScrolledDown,
    isScrolledUp,
    scrolledPosition,
    documentHeight
  } = useScrollInfo(50); // isPageBottom return true before 50 pixels of page bootm.

  useEffect(() => {
    if (isPageBottom) {
      console.log("page reached to bottom");
    }
  }, [isPageBottom]);

  return (
    <div>
    <button>
        {isScrolledDown ? 'Up' : 'Down'}
    </button>
    </div>
  )
}

useScrollInfo Config

The following are optional properties passed into useScrollInfo():

ParamTypeDefaultDescription
param1number0If you want detect pagebottom before some pixels, pass param1 by default it 0.

What you get

NameDescription
isPageBottomReached bottom of the page it returns true - Boolean
isPageTopReached top of the page it returns true - Boolean
isScrolledUpScrolling Up it returns true - Boolean
isScrolledDownScrolling Down it returns true - Boolean
scrolledPositionCurrent scrolled position - Number
documentHeightDocument(page) Height - Number