petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

[BUG] Scroll position is not maintained when dynamically adding elements to the top of the list #1137

Closed Jesus-Gonzalez closed 1 month ago

Jesus-Gonzalez commented 1 month ago

Describe the bug

The scroll position is not maintained after adding an element dynamically to the list.

Reproduction

https://codesandbox.io/p/sandbox/fwrtj2

To Reproduce

Steps to reproduce the behavior:

  1. Scroll down the list and new elements are added to the end of the list.
  2. When in the mid/bottom of the scroll, click Add User button
  3. The list is scrolled up as a result of prepending an element to the beginning of the list.

Expected behavior

The list scroller should maintain its position.

Screenshots

recording.webm

Desktop (please complete the following information):

pribeh commented 1 month ago

I'm experiencing this too. Judging from other issues posted here I believe we might be meant to use the message list branch to handle reverse list display properly. I'm trying to get that to work now but am running into some issues

https://virtuoso.dev/virtuoso-message-list/

Jesus-Gonzalez commented 1 month ago

Thanks @pribeh - looking into message list component.

I think I’ll opt to create a list by myself without virtualization, since I don’t need it for my use case.

Anyway it will be interesting to see what others can suggest or comment.

Thanks again, Jesus

petyosi commented 1 month ago

@pribeh if you're evaluating the message list, you can contact me over email. Happy to help you, given it's a commercial license.

petyosi commented 1 month ago

The original version of react-virtuoso exposes a firstItemIndex prop for prepending items, but the API is not great. I strongly recommend the message list for that purpose.