Skip to content
Merged
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/fluffy-books-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/virtual-core': patch
---

fix(virtual-core): loosen approxEqual to allow 1px difference
15 changes: 3 additions & 12 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,16 +904,7 @@ export class Virtualizer<
toOffset -= size
}

const scrollSizeProp = this.options.horizontal
? 'scrollWidth'
: 'scrollHeight'
const scrollSize = this.scrollElement
? 'document' in this.scrollElement
? this.scrollElement.document.documentElement[scrollSizeProp]
: this.scrollElement[scrollSizeProp]
: 0

const maxOffset = scrollSize - size
const maxOffset = this.getTotalSize() - size

return Math.max(Math.min(maxOffset, toOffset), 0)
}
Expand Down Expand Up @@ -1010,8 +1001,8 @@ export class Virtualizer<
const [latestOffset] = notUndefined(
this.getOffsetForIndex(index, align),
)

if (!approxEqual(latestOffset, this.getScrollOffset())) {
const currentScrollOffset = this.getScrollOffset()
if (!approxEqual(latestOffset, currentScrollOffset)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there still a chance to get infinite loop

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have something specific in mind?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess for any given scrollToIndex we want to set some kind of counter, and break recursion when this counter is reach some limit, kinda dead loop detector

this.scrollToIndex(index, { align, behavior })
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/virtual-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function notUndefined<T>(value: T | undefined, msg?: string): T {
}
}

export const approxEqual = (a: number, b: number) => Math.abs(a - b) < 1
export const approxEqual = (a: number, b: number) => Math.abs(a - b) <= 1

export const debounce = (
targetWindow: Window & typeof globalThis,
Expand Down