1.0.1 • Published 3 years ago

@bogachenkov/react-scrolling-progress v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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:

    KeyTypeDefaultDescription
    targetElementReact.MutableRefObject<any>nullA ref that will be assigned to the element you want to track . By default it will be tracking scroll for document.documentElement.
    detectByElementBottombooleanfalseIf 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:

    KeyTypeDefaultDescription
    heightReact.CSSProperties['height']'5px'Height of the scrollbar
    widthReact.CSSProperties['width']'100%'Width of the scrollbar
    positionReact.CSSProperties['position']fixedValid CSS-position
    topReact.CSSProperties['top']0CSS-position top property
    leftReact.CSSProperties['left']0CSS-position left property
    backgroundColorReact.CSSProperties['color']#EEEEEEBackground color of the progress bar container.
    colorsReact.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.
    showStripesbooleantrueShow/hide gradient stripes.
    showBarShadowbooleantrueShow/hide shadows in the progress bar container

useScrollProgress

  • options:

    KeyTypeDefaultDescription
    targetElementReact.MutableRefObject<any>nullA 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
    useTargetElementbooleanfalseIf 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
    precisionnumber3Decimal places of the percentage that will be returned from hook
    detectByElementBottombooleanfalseIf 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?)
NameTypeDescription
progressStringstringThe progress value represented by a string like '100%'
progressNumbernumberThe progress value
targetElementMutableRefObject<any> \| undefinedA 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.