From ee09b2597be86f0705d8e97d770333d4db9cef5d Mon Sep 17 00:00:00 2001 From: inokawa <48897392+inokawa@users.noreply.github.com> Date: Thu, 5 Dec 2024 09:25:52 +0900 Subject: [PATCH] Fix scrollbar size calculation in scrollToIndex of horizontal WindowVirtualizer --- src/core/scroller.ts | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/core/scroller.ts b/src/core/scroller.ts index 2f75d516..8bf72900 100644 --- a/src/core/scroller.ts +++ b/src/core/scroller.ts @@ -562,39 +562,29 @@ export const createWindowScroller = ( const document = getCurrentDocument(containerElement); const window = getCurrentWindow(document); + const html = document.documentElement; + const getScrollbarSize = () => + store.$getViewportSize() - + (isHorizontal ? html.clientWidth : html.clientHeight); scheduleImperativeScroll(() => { - // Calculate target scroll position including container's offset from document - const containerOffset = calcOffsetToViewport( - containerElement!, - document.body, - window, - isHorizontal - ); - - // slight tech debt: this would otherwise need to be accounted for in store._getViewportSize in a way that's flexible for windowScroller - const scrollbarHeight = - window.innerHeight - document.documentElement.clientHeight; - const scrollbarWidth = - window.innerHeight - document.documentElement.clientWidth; - const viewportAdjustment = - align === "end" || align === "center" - ? isHorizontal - ? scrollbarWidth - : scrollbarHeight - : 0; - return ( offset + - containerOffset + + // Calculate target scroll position including container's offset from document + calcOffsetToViewport( + containerElement!, + document.body, + window, + isHorizontal + ) + // store._getStartSpacerSize() + store.$getItemOffset(index) + (align === "end" ? store.$getItemSize(index) - - (store.$getViewportSize() - viewportAdjustment) + (store.$getViewportSize() - getScrollbarSize()) : align === "center" ? (store.$getItemSize(index) - - (store.$getViewportSize() - viewportAdjustment)) / + (store.$getViewportSize() - getScrollbarSize())) / 2 : 0) );