Skip to content

Commit

Permalink
Merge pull request #569 from inokawa/fix-window-scroll-to-index
Browse files Browse the repository at this point in the history
Fix scrollbar size calculation in scrollToIndex of horizontal WindowVirtualizer
  • Loading branch information
inokawa authored Dec 5, 2024
2 parents 12af5e5 + ee09b25 commit 0a4513b
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/core/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down

0 comments on commit 0a4513b

Please sign in to comment.