1.0.1 • Published 5 years ago
@bogachenkov/react-scrolling-progress v1.0.1
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-progressor
yarn add @bogachenkov/react-scrolling-progressTable 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 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 tofalse, then the progress will be equal to 100% only when you will scroll the whole element.styles:Key Type Default Description 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']0 CSS-position toppropertyleftReact.CSSProperties['left']0 CSS-position leftpropertybackgroundColorReact.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:Key Type Default Description 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 withuseTargetElementuseTargetElementbooleanfalseIf set to true, the hook will return a ref that needs to be assigned to the element you want to track.Cannot be used withtargetElementprecisionnumber3 Decimal 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 tofalse, 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. |