Skip to content

Commit e2d93c2

Browse files
authored
fix(virtual-core): prevent measurement jitter when scale is applied (#986)
1 parent 990db34 commit e2d93c2

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

.changeset/tough-walls-accept.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/virtual-core': patch
3+
---
4+
5+
fix(virtual-core): prevent measurement jitter when scale is applied

packages/virtual-core/src/index.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export interface Rect {
4444

4545
//
4646

47+
const getRect = (element: HTMLElement): Rect => {
48+
const { offsetWidth, offsetHeight } = element
49+
return { width: offsetWidth, height: offsetHeight }
50+
}
51+
4752
export const defaultKeyExtractor = (index: number) => index
4853

4954
export const defaultRangeExtractor = (range: Range) => {
@@ -77,7 +82,7 @@ export const observeElementRect = <T extends Element>(
7782
cb({ width: Math.round(width), height: Math.round(height) })
7883
}
7984

80-
handler(element.getBoundingClientRect())
85+
handler(getRect(element as unknown as HTMLElement))
8186

8287
if (!targetWindow.ResizeObserver) {
8388
return () => {}
@@ -93,7 +98,7 @@ export const observeElementRect = <T extends Element>(
9398
return
9499
}
95100
}
96-
handler(element.getBoundingClientRect())
101+
handler(getRect(element as unknown as HTMLElement))
97102
}
98103

99104
instance.options.useAnimationFrameWithResizeObserver
@@ -251,11 +256,10 @@ export const measureElement = <TItemElement extends Element>(
251256
return size
252257
}
253258
}
254-
return Math.round(
255-
element.getBoundingClientRect()[
256-
instance.options.horizontal ? 'width' : 'height'
257-
],
258-
)
259+
260+
return (element as unknown as HTMLElement)[
261+
instance.options.horizontal ? 'offsetWidth' : 'offsetHeight'
262+
]
259263
}
260264

261265
export const windowScroll = <T extends Window>(

0 commit comments

Comments
 (0)