npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kohlmannj/react-scroll-percentage

v3.0.0-alpha.13

Published

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

Downloads

16

Readme

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

Props

The <ScrollPercentage /> accepts the following props:

| Name | Type | Default | Required | Description | | --------- | ----------------------------------------------- | ------- | -------- | --------------------------------------------------------------------------------------------- | | tag | Node | 'div' | true | Element tag to use for the wrapping component | | children | ((percentage: number, inView: boolean) => Node) | | true | Children should be either a function or a node | | threshold | Number | 0 | false | Number between 0 and 1 indicating the percentage that should be visible before triggering | | onChange | (percentage: number, inView: boolean) => void | | false | Call this function whenever the in view state changes | | innerRef | (element: ?HTMLElement) => void | | false | Get a reference to the inner DOM node |

Example code

Render prop

The basic usage pass a function as the child. It will be called whenever the state changes, with the current value of percentage and inView.

Note that will still render a wrapping element (default is a <div>). You can change to element by setting tag, and any excess props like className will be passed to the element

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={(percentage, inView) => console.log(percentage, inView)}>
  <h2>
    Plain children are always rendered. Use onChange to monitor state.
  </h2>
</ScrollPercentage>

Polyfills

Intersection Observer

Intersection Observer is the API is used to determine if an element is inside the viewport or not. Browser support is pretty good, but Safari is still missing support.

Can i use intersectionobserver?

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

yarn add intersection-observer

Then import it in your app:

import 'intersection-observer'

If you are using Webpack (or similar) you could use dynamic imports, to load the Polyfill only if needed. A basic implementation could look something like this:

loadPolyfills()
  .then(() => /* Render React application now that your Polyfills are ready */)

/**
* Do feature detection, to figure out which polyfills needs to be imported.
**/
function loadPolyfills() {
  const polyfills = []

  if (!supportsIntersectionObserver()) {
    polyfills.push(import('intersection-observer'))
  }

  return Promise.all(polyfills)
}

function supportsIntersectionObserver() {
  return (
    'IntersectionObserver' in global &&
    'IntersectionObserverEntry' in global &&
    'intersectionRatio' in IntersectionObserverEntry.prototype
  )
}

requestAnimationFrame

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