Giter VIP home page Giter VIP logo

react-use-scrollspy's Introduction

Patricks's github stats

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

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?

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.

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>

)

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

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.