1.0.9 • Published 4 years ago

rscroll-position v1.0.9

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

rscroll-position

rscroll-position is a react component that returns the browser's x and y viewport position. It is highly optimized :rocket: using the special techniques to avoid unnecessary rerenders!

NPM contributions welcome

Install

npm install --save rscroll-position

Usage

PropDescription
elementreference to DOM element/node
delayThe delay (to throttle events) in ms.
useWindowUse window.scroll if true otherwise document.body.getBoundingClientRect() to detect scroll position.
depsDependency array

useScrollPosition()

import { useState, useRef } from 'react';
import { useScrollPosition } from 'rscroll-position';

function App(){
  
  const [ trackPosition, setTrackPosition ] = useScrollPosition (false);
  
  // refference to DOM element
  const element = useRef();
  
  useScrollPosition(({previous, current})=> {
    console.log({previous, current})
  },
    element,  // element
    true,     // useWindow
    700,      // delay ms
    [trackPosition]        // deps
  );
  
  return (
    <div ref={element}>
      <button 
        onClick={() => setTrackPosition(prev => !prev)}>
        Toggle
      </button>
    </div>
  )
}

License

MIT © Chitova263

1.0.9

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago