0.0.5 • Published 4 years ago

scroll-provider v0.0.5

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

CircleCI GuardRails badge Depfu

code style: prettier

React Scroll Provider

React component that passes scrolling data through the component tree.

Available information are:

  • scrollPosition (number): current scrollY,
  • isScrolling (boolean): user is scrolling the page

DEMO (CodeSandbox)

Installation

yarn add scroll-provider

or

npm install --save scroll-provider

Usage

  • Import component
import ScrollProvider from 'scroll-provider'
  • Wrap the main component
<ScrollProvider>
  <div className="App">
   ...app content...
  </div>
</ScrollProvider>
  • Now all components have access to the scroll information
  1. You can use useContext hook
import React, { useContext } from "react";
import { ScrollContext } from "../ScrollProvider";

const Component = () => {
  const { scrollPosition, isScrolling } = useContext(ScrollContext);

  return (
    <div>
      {scrollPosition} - {isScrolling ? "scrolling" : "idle"}
    </div>
  );
};

export default Component;
  1. or the Context API
import React from "react";
import { ScrollContext } from "../ScrollProvider";

const Component = () => (
  <ScrollsContext.Consumer>
    {({ scrollPosition, isScrolling }) => (
      <div>
        {scrollPosition} - {isScrolling ? "scrolling" : "idle"}
      </div>
    )}
  </ScrollsContext.Consumer>
);

export default ScrollPosition;