diff --git a/.changeset/fluffy-books-attend.md b/.changeset/fluffy-books-attend.md new file mode 100644 index 000000000..0aa286308 --- /dev/null +++ b/.changeset/fluffy-books-attend.md @@ -0,0 +1,5 @@ +--- +'@tanstack/virtual-core': patch +--- + +fix(virtual-core): loosen approxEqual to allow 1px difference diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 6aad9d6d1..3c704059c 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -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) } @@ -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)) { this.scrollToIndex(index, { align, behavior }) } } else { diff --git a/packages/virtual-core/src/utils.ts b/packages/virtual-core/src/utils.ts index 9ebf8e76b..1bb4615c2 100644 --- a/packages/virtual-core/src/utils.ts +++ b/packages/virtual-core/src/utils.ts @@ -83,7 +83,7 @@ export function notUndefined(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,