fix(virtual-core): read shouldAdjustScrollPositionOnItemSizeChange from options by xianjianlf2 · Pull Request #1232 · TanStack/virtual · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wire-should-adjust-scroll-position-option.md
20 changes: 18 additions & 2 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ export interface VirtualizerOptions<
useAnimationFrameWithResizeObserver?: boolean
laneAssignmentMode?: LaneAssignmentMode
useCachedMeasurements?: boolean
shouldAdjustScrollPositionOnItemSizeChange?: (
item: VirtualItem,
delta: number,
instance: Virtualizer<TScrollElement, TItemElement>,
) => boolean
}

type ScrollState = {
Expand Down Expand Up @@ -1547,10 +1552,21 @@ export class Virtualizer<
this.scrollState?.behavior !== 'smooth' &&
this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
// The predicate can be supplied via options (#1227) or, for
// back-compat, by assigning it directly on the instance.
const shouldAdjustScrollPositionOnItemSizeChange:
| ((
item: VirtualItem,
delta: number,
instance: Virtualizer<TScrollElement, TItemElement>,
) => boolean)
| undefined =
this.options.shouldAdjustScrollPositionOnItemSizeChange ??
this.shouldAdjustScrollPositionOnItemSizeChange
const shouldAdjustScroll =
this.scrollState?.behavior !== 'smooth' &&
(this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
? this.shouldAdjustScrollPositionOnItemSizeChange(
(shouldAdjustScrollPositionOnItemSizeChange !== undefined
? shouldAdjustScrollPositionOnItemSizeChange(
// The callback expects a VirtualItem; build one lazily only
// when the consumer actually supplied a custom predicate.
this.measurementsCache[index] ?? {
Expand Down
113 changes: 113 additions & 0 deletions packages/virtual-core/tests/index.test.ts