Skip to content

Commit 60719f6

Browse files
authored
fix(virtual-core): loosen approxEqual to allow 1px difference (#995)
1 parent 8bb2c24 commit 60719f6

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

.changeset/fluffy-books-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/virtual-core': patch
3+
---
4+
5+
fix(virtual-core): loosen approxEqual to allow 1px difference

packages/virtual-core/src/index.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -904,16 +904,7 @@ export class Virtualizer<
904904
toOffset -= size
905905
}
906906

907-
const scrollSizeProp = this.options.horizontal
908-
? 'scrollWidth'
909-
: 'scrollHeight'
910-
const scrollSize = this.scrollElement
911-
? 'document' in this.scrollElement
912-
? this.scrollElement.document.documentElement[scrollSizeProp]
913-
: this.scrollElement[scrollSizeProp]
914-
: 0
915-
916-
const maxOffset = scrollSize - size
907+
const maxOffset = this.getTotalSize() - size
917908

918909
return Math.max(Math.min(maxOffset, toOffset), 0)
919910
}
@@ -1010,8 +1001,8 @@ export class Virtualizer<
10101001
const [latestOffset] = notUndefined(
10111002
this.getOffsetForIndex(index, align),
10121003
)
1013-
1014-
if (!approxEqual(latestOffset, this.getScrollOffset())) {
1004+
const currentScrollOffset = this.getScrollOffset()
1005+
if (!approxEqual(latestOffset, currentScrollOffset)) {
10151006
this.scrollToIndex(index, { align, behavior })
10161007
}
10171008
} else {

packages/virtual-core/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function notUndefined<T>(value: T | undefined, msg?: string): T {
8383
}
8484
}
8585

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

8888
export const debounce = (
8989
targetWindow: Window & typeof globalThis,

0 commit comments

Comments
 (0)