0.0.2 • Published 11 months ago

scrollen v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Scrollen

A performant utility scroll hook for React.

Install

npm install scrollen

Getting Started

To start using the scrollen you need import and call the useScroller hook

import { useScroller } from 'scrollen'

const scroller = useScroller()

And that's it! By default the hook will listen to scroll on document.

If you want to use a different scroll container, you can use the setScrollerElement function

import { useScroller } from 'scrollen'

const scroller = useScroller()

<div ref={scroller.setScrollerElement}>
  ...
</div>

Example

See this small example of how you can use the hook.

Documentation

First of all, a huge thanks to @diegohaz as this library uses the Ariakit Component Stores API, so refer to the Ariakit Component Stores Guide to learn the core of the hook.

Props

PropDescription
initialScrollTopThe initial scrollTop value to the scroller element.
initialScrollLeftThe initial scrollLeft value to the scroller element.
startScrollAtWhere the scroll should start. You can pass bottom \| right.

State

StateDescription
elementThe scroller element.
scrollTopThe scrollTop value for the scroller element.
scrollLeftThe scrollLeft value for the scroller element.
scrollWidthThe scrollWidth value for the scroller element.
scrollHeightThe scrollHeight value for the scroller element.
scrollYThe progress of the vertical scroll. (between 0 and 1)
scrollXThe progress of the horizontal scroll. (between 0 and 1)
scrollLengthYThe length of the vertical scroll.
scrollLengthXThe length of the horizontal scroll.
scrollDirectionYThe direction of the vertical scroll. (static \| up \| down)
scrollDirectionXThe direction of the horizontal scroll. (static \| left \| right)
isAtTopA boolean to check if the scroll reached the top.
isAtBottomA boolean to check if the scroll reached the bottom.
isAtLeftA boolean to check if the scroll reached the left.
isAtRightA boolean to check if the scroll reached the right.

Functions

FunctionDescription
setScrollerElementFunction to set the scroller element.
scrollToTopFunction to scroll programatically to top.
scrollToLeftFunction to scroll programatically to left.
scrollToBottomFunction to scroll programatically to bottom.
scrollToRightFunction to scroll programtically to right.
useOnScrollTopA custom hook to register a callback that is called when the scroll reached the top.
useOnScrollBottomA custom hook to register a callback that is called when the scroll reached the bottom.

Acknowledgements

  • Ariakit - Thanks to @diegohaz for the Component Stores API that made it possible to create a performant hook.
  • react-virtuoso - Where did I get ideas for utilities.