1.1.1 • Published 9 months ago

@kylelwm/react-ui-scrollspy v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

ScrollSpy Demo

React UI ScrollSpy

npm npm License MIT PRs Welcome

React TypeScript NPM

Make sure you ⭐️ this repository if you find it helpful or interesting :)

✨ Installation

React 18

npm install --save react-ui-scrollspy

OR

yarn add react-ui-scrollspy

React 17 and below

npm install --save react-ui-scrollspy@2.2.0

OR

yarn add react-ui-scrollspy@2.2.0

🎞 Demo

Try it your self here!

Demo 1Demo 2
ScrollSpy DemoScrollSpy Demo

⚙️ Usage

  1. In your navigation component
<div>
  <p data-to-scrollspy-id="first">Section 1</p>
  <p data-to-scrollspy-id="second">Section 2</p>
</div>
  1. Wrap the elements you want to spy on in the <ScrollSpy> component.
import ScrollSpy from "react-ui-scrollspy";

<ScrollSpy>
  <div id="first">
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Aut dolores
    veritatis doloremque fugit. Soluta aperiam atque inventore deleniti,
    voluptatibus non fuga eos magni natus vel, rerum excepturi expedita.
    Tempore, vero!
  </div>
  <div id="second">
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Aut dolores
    veritatis doloremque fugit. Soluta aperiam atque inventore deleniti,
    voluptatibus non fuga eos magni natus vel, rerum excepturi expedita.
    Tempore, vero!
  </div>
</ScrollSpy>
  1. Write styles for when the navigation element which is active in your index.css
.active-scroll-spy {
  background-color: yellowgreen;
  border-radius: 15px;
}

📝 Notes

Incase the ScrollSpy is not working the way you expected, you can try the following:

  • Reduce the value of scrollThrottle.

  • If your page contains a navbar a header consider adding the following CSS

html {
  scroll-padding-top: 120px; /* height of your navbar */
}

💡 Props

🔧 Children

AttributesTypeDescriptionDefaultRequired
childrenReactNodeEach direct child Element should contain an id-yes

🔧 Refs

AttributesTypeDescriptionDefaultRequired
navContainerRefMutableRefObject<HTMLDivElement | null>ref to your navigation container containing items with data-to-scrollspy-id attributes-no
parentScrollContainerRefMutableRefObject<HTMLDivElement | null>If you want to spy only on a particular scrollable container (Element) then pass its ref to this prop-no

🔧 Throttle

AttributesTypeDescriptionDefaultRequired
scrollThrottlenumberIn milliseconds to throttle the onscroll event. Lower the number, better the response time, higher the performance cost300no

🔧 Callback

AttributesTypeDescriptionDefaultRequired
onUpdateCallback(id: string) => voidExecutes this function whenever you scroll to a new ScrollSpy child Element, callback returns the id of that Element as well-no

🔧 Offsets

AttributesTypeDescriptionDefaultRequired
offsetTopnumberSpy will be fired when it has been scrolled offsetTop beyond 50% to the top of the containing element0no
offsetBottomnumberSpy will be fired when it has been scrolled offsetBottom beyond 50% to the bottom of the containing element0no

🔧 Customize Attributes

AttributesTypeDescriptionDefaultRequired
useDataAttributestringTo customize the string after data-"to-scrollspy-id"no
activeClassstringTo customize the class added when the Element in view"active-scroll-spy"no
useBoxMethodbooleanSet to false if you want your spy to be active only if more than 50% of that div is in the viewporttrueno
updateHistoryStackbooleanSet to false to disable the URL getting automatically updated when scrollingtrueno

📝 Authors