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

@bogachenkov/react-scrolling-progress

v1.0.1

Published

React component (based on HTML `<progress>` element) that shows the progress of the scroll and listens to the target element height changes.

Downloads

34

Readme

React component (based on HTML <progress> element) that shows the progress of the scroll and listens to the target element height changes. It also returns useScrollProgress hook in case you want to use your own progress bar.

Installation

npm install @bogachenkov/react-scrolling-progress

or

yarn add @bogachenkov/react-scrolling-progress

Table of contents

Basic Usage

ReactScrollProgress with no configuration

By default it will be tracking for document.documentElement scroll progress.

import React from "react";
import ReactScrollProgress from "@bogachenkov/react-scrolling-progress";

const MyComponent = () => {
  ...
  return (
    <>
      <ReactScrollProgress />
      ...
    </>
  )
}

ReactScrollProgress with some configuration

import React, { useRef } from "react";
import ReactScrollProgress from "@bogachenkov/react-scrolling-progress";

const MyComponent = () => {
    const targetElementRef = useRef();
    return (
        <div ref={targetElementRef}>
          <ReactScrollProgress
            styles={{
              position: 'sticky',
              top: '5px',
              colors: [ '#845EC2', '#D65DB1', '#FF6F91', '#FF9671', '#FFC75F','#F9F871']
            }}
            scrollOptions={{
              targetElement: ref,
              detectByElementBottom: true
            }}
          />
          ...
        </div>
    )
}

useScrollProgress

import React from "react";
import { useScrollProgress } from "@bogachenkov/react-scrolling-progress";

const MyComponent = () => {
    const { targetElement, progressNumber, progressString } = useScrollProgress({
      precision: 4,
      detectByElementBottom: true,
      useTargetElement: true
    });
    return (
      <div ref={targetElement}>
        <CustomScrollBar value={progressNumber} />
        ... // OR
        <div style={{width: progressString}}></div>
      </div>
    )
}

Also you can use targetElement instead of useTargetElement and passed your own ref to it:

import React, { useRef } from "react";
import { useScrollProgress } from "@bogachenkov/react-scrolling-progress";

const MyComponent = () => {
    const targetElementRef = useRef();
    const { targetElement, progressNumber, progressString } = useScrollProgress({
      precision: 4,
      detectByElementBottom: true,
      targetElement: targetElementRef
    });
    return (
      <div ref={targetElementRef}>
        <CustomScrollBar value={progressNumber} />
        ... // OR
        <div style={{width: progressString}}></div>
      </div>
    )
}

targetElement and useTargetElement fields are mutually exclusive, so you should use only one of them.

Properties

ReactScrollProgress

  • scrollOptions:

    | Key | Type | Default | Description | | :---------------------: | :---------------------------: | :-----: | :----------------------------------------------------------: | | targetElement | React.MutableRefObject<any> | null | A ref that will be assigned to the element you want to track . By default it will be tracking scroll for document.documentElement. | | detectByElementBottom | boolean | false | If set to true, then the progress of scrolling will be equal to 100% at the moment when the bottom border of the element appears in the viewport.If set to false, then the progress will be equal to 100% only when you will scroll the whole element. |

  • styles:

    | Key | Type | Default | Description | | :---------------: | :-------------------------------: | :----------------------: | :----------------------------------------------------------: | | height | React.CSSProperties['height'] | '5px' | Height of the scrollbar | | width | React.CSSProperties['width'] | '100%' | Width of the scrollbar | | position | React.CSSProperties['position'] | fixed | Valid CSS-position | | top | React.CSSProperties['top'] | 0 | CSS-position top property | | left | React.CSSProperties['left'] | 0 | CSS-position left property | | backgroundColor | React.CSSProperties['color'] | #EEEEEE | Background color of the progress bar container. | | colors | React.CSSProperties['color'][] | ['#319197', '#fb7319'] | Array of colors used to create the gradient-color of the progress bar. If you want only one color, just pass the array with that color. | | showStripes | boolean | true | Show/hide gradient stripes. | | showBarShadow | boolean | true | Show/hide shadows in the progress bar container |

useScrollProgress

  • options:

    | Key | Type | Default | Description | | :---------------------: | :---------------------------: | :-----: | :----------------------------------------------------------: | | targetElement | React.MutableRefObject<any> | null | A ref that will be assigned to the element you want to track . By default it will be tracking scroll for document.documentElement.Cannot be used with useTargetElement | | useTargetElement | boolean | false | If set to true, the hook will return a ref that needs to be assigned to the element you want to track.Cannot be used with targetElement | | precision | number | 3 | Decimal places of the percentage that will be returned from hook | | detectByElementBottom | boolean | false | If set to true, then the progress of scrolling will be equal to 100% at the moment when the bottom border of the element appears in the viewport.If set to false, then the progress will be equal to 100% only when you will scroll the whole element. |

State

const { progressString, progressNumber, targetElement? } = useScrollProgress(options?)

| Name | Type | Description | | :--------------: | :---------------------------------: | :----------------------------------------------------------: | | progressString | string | The progress value represented by a string like '100%' | | progressNumber | number | The progress value | | targetElement | MutableRefObject<any> \| undefined | A ref that needs to be assigned to the element you want to track. It will returns only if you passed useTargetElement key to options object. |