Giter Site home page Giter Site logo

purii / react-use-scrollspy Goto Github PK

View Code? Open in Web Editor NEW
318.0 5.0 17.0 3.4 MB

Flexible React Hook to automatically update navigation based on scroll position

Home Page: https://purii.github.io/react-use-scrollspy/

License: MIT License

HTML 22.30% CSS 14.72% TypeScript 62.98%
reactjs javascript hooks-api-react react scrollspy hooks

react-use-scrollspy's Introduction

react-use-scrollspy

Build Status npm version npm GitHub license Donate

Example

Installation

react-use-scrollspy is a React Hook which requires React 16.8.0 or later.

// yarn
yarn add react-use-scrollspy
// or npm
npm i react-use-scrollspy --S

Usage

import useScrollSpy from 'react-use-scrollspy';
...
const activeSection = useScrollSpy({
  sectionElementRefs: [], // Array of References to DOM elements
});
Parameter Default Type Description
defaultValue 0 int Default value that is returned (optional)
offsetPx 0 int Set offset (optional)
sectionElementRefs [] [Ref] Array of Refs to observe (e.g. via React refs)
scrollingElement window Ref Target of the scrolling (e.g. via React refs)) (optional)

with Refs

Use React refs for section elements like in the provided example.

import React, { useRef } from 'react';
import useScrollSpy from 'react-use-scrollspy';

const App = () => {

  const sectionRefs = [
    useRef(null),
    useRef(null),
    useRef(null),
  ];

  const activeSection = useScrollSpy({
    sectionElementRefs: sectionRefs,
    offsetPx: -80,
  });

  return (
    <nav className="App-navigation">
      <span className={activeSection === 0 ? "App-navigation-item App-navigation-item--active" : "App-navigation-item"}>Section 1</span>
      <span className={activeSection === 1 ? "App-navigation-item App-navigation-item--active" : "App-navigation-item"}>Section 2</span>
      <span className={activeSection === 2 ? "App-navigation-item App-navigation-item--active" : "App-navigation-item"}>Section 3</span>
    </nav>

    <section className="App-section" ref={sectionRefs[0]}>
      <h1>Section 1</h1>
    </section>
    <section className="App-section" ref={sectionRefs[1]}>
      <h1>Section 2</h1>
    </section>
    <section className="App-section" ref={sectionRefs[2]}>
      <h1>Section 3</h1>
    </section>
  )

react-use-scrollspy's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar exil0867 avatar jonascr avatar purii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-use-scrollspy's Issues

Typescript support

If I install the package on a typescript project, I get a warning:
Could not find a declaration file for module 'react-use-scrollspy'.

A declaration file should be added for typescript users

Check scroll position on re-render

Please add a useEffect call to the hook, so that the state is checked on re-renders. Especially on the first render. If you scroll to the last section and refresh the page the value is incorrect.

Variable number of sections

Any thoughts on what I can do if I have a variable number of sections to watch?

I can't run useRef in a loop (it's against the "rules of hooks").

Question: Custom scroll container?

Hi,

thanks for this hook. I was wondering if it would be possible to use it with a custom scroll container, e.g. a fixed height div with overflow: auto containing my list of sections. If so a quick example would be appreciated.

Cheers

Question: How to handle sections with small height at the bottom of the page?

First of all, thanks for the fantastic hook. Works like a charm! ๐Ÿ‘

So, I have a page with dynamically generated sections with editable content, so the last section on the page sometimes has a small height say 100px and doesn't trigger the hook like it should be for other sections on the page
The hook works perfectly, even for the last section on the page, if it's height is large enough to be covered into the threshold value calculated using the offsetPx prop

import useScrollSpy from 'react-use-scrollspy';

// if I have 3 sections, and the height of the 3rd(last) section is small
// then activeSection value is never set to its index value i.e. 2
const activeSection = useScrollSpy({
  sectionElementRefs: sectionRefsArray,
  offsetPx: Y_OFFSET
});

Is there any way to handle this scenario?

Get activeSection by ID instead of number

Could you kindly advice on how to get the activeSection by sectionRefs id? Currently it is returning the index of the sectionRefs - 0, 1, 2 . I would need to return by sectionRefs.getAttribute(id) because the order of the section might differ than the navigation.

import React, { useRef } from 'react';
import useScrollSpy from 'react-use-scrollspy';

const App = () => {

const sectionRefs = [
useRef(null),
useRef(null),
useRef(null),
];

const activeSection = useScrollSpy({
sectionElementRefs: sectionRefs,
offsetPx: -80,
});

console.log(activeSection); ----> currently getting the index of the sectionRefs - 0, 1, 2 . Option to return by sectionRefs.getAttribute(id) because the order of the section might differ than the navigation.

return (


<span className={activeSection === "section-1" ? "active" : ""}>Section 1
<span className={activeSection === "section-2" ? "active" : ""}>Section 2
<span className={activeSection === "section-3"? "active" : ""}>Section 3

<section id="section-1" ref={sectionRefs[0]}>
  <h1>Section 1</h1>
</section>
<section id="section-2" ref={sectionRefs[1]}>
  <h1>Section 2</h1>
</section>
<section id="section-3" ref={sectionRefs[2]}>
  <h1>Section 3</h1>
</section>

)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.