TanStack / TanStack/virtual

`useVirtualizer({paddingEnd: 800})` creates overscroll issue with `scrollToIndex(last)`

Open
#1,257 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.1k
Forks
466
Avg merge
2d 31m
Merged PRs (30d)
13

Description

Describe the bug

The paddingEnd option for the useVirtualizer callback is not correctly taken into account when using rowVirtualizer .scrollToIndex(last). Instead the function call scrolls to the end of the scroll container. For every other index this works as intended.

const rowVirtualizer = useVirtualizer({
  count: 100,
  getScrollElement: () => parentRef.current,
  estimateSize: () => 35,
  overscan: 5,
  paddingEnd: 800
})

// ...

<button onClick={() => {
  rowVirtualizer.scrollToIndex(98)
  }}>
  Scroll to 98 (works)
</button>
<button onClick={() => {
  rowVirtualizer.scrollToIndex(99)
  }}>
  Scroll to 99 (overscrolls)
</button>
Your minimal, reproducible example

https://github.com/Freymaurer/tanstack-virtual-padding-overscroll-bug-example

Steps to reproduce
  1. Clone and npm i -> npm run dev the example (fresh vite-react-ts with tanstack virtual dependency) -> open the link printed to console
  2. Click the buttons i added on top of the view
  • "Back to Start" uses scrollToIndex(0)
  • "Scroll to 98 (works)" scrolls to 98, so not last index
  • "Scroll to 99 (overscrolls)" scrolls to the last index
  1. Click on "Back to Start", then "Scroll to 98 (works)". It scrolls with pretty good alignment. The field is shown on the bottom of the scrollContainer
  2. Click on "Back to Start", then "Scroll to 99 (overscrolls)". It overscrolls, the element is not visible on screen. 🐛 I think this is due the library calling more or less "scrollToEnd" instead of "scrollTo" in this scenario.
Expected behavior

I expected the behavior to be consistent. The element at last index should be fully visible after calling the scroll function, instead of scrolling to the scroll container end

How often does this bug happen?

Every time

Screenshots or Videos

After clicking on "Scroll to 98 (works)"

Image

After clicking on "Scroll to 99 (overscrolls)"

Image
Platform

OS: Windows 11
Browser: Tested on Chrome, Edge, Firefox, Opera

tanstack-virtual version

v3.14.9

TypeScript version

v6.0.3

Additional context

No response

Terms & Code of Conduct
  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked minimal reproduction and trace the useVirtualizer and scrollToIndex entry points, focusing on the paddingEnd and last-index behavior. Reproduce the issue with the provided buttons, then verify that scrolling to the last index keeps the item fully visible without overscrolling the container.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.