Skip to content
Source
Sign inSign up
TanStack/virtual

fix(virtual-core): scrollToIndex(last) overshoots when paddingEnd > 0 #1263

Closeddikshit-n(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/1263/headopened

Showing 2 changed files with 191 additions and 3 deletions.

x
0 of 2 viewed, 2 left
21 additions and 3 deletionspackages/virtual-core/src/index.tsModified
View file
@@ -1888,10 +1888,28 @@
- // For the last item with 'end' alignment, use browser's actual max scroll
- // to account for borders/padding that aren't in our measurements
+ // For the last item with 'end' alignment, derive the target from the
+ // selected item's own end rather than from getTotalSize() across all
+ // lanes. getTotalSize() returns the furthest measured end across lanes,
+ // but in a multi-lane layout the last index can end in a shorter lane
+ // and have a smaller item.end than getTotalSize() implies. Targeting the
+ // lane-max would scroll the selected item above the viewport top.
+ //
+ // We also apply scrollPaddingEnd here (matching the normal 'end' path
+ // below) and clamp to the virtual maximum so paddingEnd > 0 still keeps
+ // the last item flush with the bottom of the viewport rather than
+ // overshooting it.
if (align === 'end' && index === this.options.count - 1) {
- return [this.getMaxScrollOffset(), align] as const
+ const virtualMaxOffset = Math.max(
+ this.getTotalSize() - this.options.paddingEnd - this.getSize(),
+ 0,
+ )
+ const itemEndOffset =
+ item.end + this.options.scrollPaddingEnd - this.getSize()
+ return [