New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-scroll-percentage

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-scroll-percentage

Monitor the scroll percentage of a component inside the viewport, using the IntersectionObserver API.

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.5K
increased by8.26%
Maintainers
1
Weekly downloads
 
Created
Source

react-scroll-percentage

Greenkeeper badge Travis styled with prettier npm

React component that reports the current scroll percentage of a element inside the viewport. It uses React Intersection Observer to only report the percentage when the element is inside the viewport.

import ScrollPercentage from 'react-scroll-percentage'

<ScrollPercentage>
  {({ percentage }) => (
    <h2>{`Percentage scrolled: ${percentage.toPrecision(2)}%.`}</h2>
  )}
</ScrollPercentage>

Demo

See https://thebuilder.github.io/react-scroll-percentage/ for a demo.

Installation

Install using Yarn:

yarn add react-scroll-percentage

or NPM:

npm install react-scroll-percentage --save

Polyfills

intersection-observer

The component requires the intersection-observer API to be available on the global namespace. At the moment you include the polyfill to support all browsers

You can import the polyfill directly or use a service like polyfill.io that can add it when needed.

yarn add intersection-observer

Then import it in your app

import 'intersection-observer'

requestAnimationFrame

To optimize scroll updates, requestAnimationFrame is used. Make sure your target browsers support it, or include the required polyfill.

Props

The <Observer /> accepts the following props:

NameTypeDefaultRequiredDescription
tagStringfalseElement tag to use for the wrapping component
childrenfunc/nodefalseChildren should be either a function or a node
thresholdNumber0falseNumber between 0 and 1 indicating the the percentage that should be visible before triggering
onChangeFuncfalseCall this function whenever the in view state changes

Example code

Child as function

The basic usage pass a function as the child. It will be called whenever the state changes, with an object containing inView and percentage.

import ScrollPercentage from 'react-scroll-percentage'

<ScrollPercentage>
  {({ percentage, inView }) => (
    <h2>{`Percentage scrolled: ${percentage.toPrecision(2)}%.`}</h2>
  )}
</ScrollPercentage>

OnChange callback

You can monitor the onChange method, and control the state in your own component. The child node will always be rendered.

import ScrollPercentage from 'react-scroll-percentage'

<ScrollPercentage onChange={state => console.log(state)}>
  <h2>
    Plain children are always rendered. Use onChange to monitor state.
  </h2>
</Observer>

Keywords

FAQs

Package last updated on 27 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc