Giter Site home page Giter Site logo

Comments (3)

MOHIDQ avatar MOHIDQ commented on June 7, 2024 1

Thanks, that works very well! didn't think about using getBoundingClientRect().top for the clicked on element! Definitely making adding a method to capture the current scroll position would be great, but this solution works for now

from react-viewport-list.

oleggrishechkin avatar oleggrishechkin commented on June 7, 2024

Hi, @MOHIDQ.

You soultion with scrollTop can work correctly only for fixed item height.
The best solution at this moment is manually compute index and offset on item's click (if you open some "item view" from "list" by clicking at item). offset should be the difference between viewport getBoundingClientRect().top and clicked item getBoundingClientRect().top. offset is just a value which will be added to scrollTop (or scrollLeft) after scroll to element by index.

I think I can add getScrollPosition() method in the next minor release to get index and offset for scroll restoration without pain.

from react-viewport-list.

oleggrishechkin avatar oleggrishechkin commented on June 7, 2024

@MOHIDQ Anyway I added getScrollPosition method in 7.1.0 release. You can use it for computing index and offset for initialIndex/initialOffset or scrollToIndex on component unmount/route change/scroll etc.

something like this:

// save last scroll position somewhere. It can be a session storage or some variable outside your component for example. 
lastScrollPosition = viewportListRef.current.getScrollPosition();

// now we can use index and offset in out ViewportList for scroll restoration
<ViewportList>
    ...
    initialIndex={lastScrollPosition.index}
    initialOffset={lastScrollPosition.offset}
</VIewportList>

from react-viewport-list.

Related Issues (20)

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.