Skip to content

Commit

Permalink
Suppress rerendering of WindowVirtualizer outside of viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Mar 5, 2024
1 parent 2b64b26 commit bca7113
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
ItemResize,
ItemsRange,
} from "./types";
import { abs, clamp, max, min } from "./utils";
import { abs, max, min } from "./utils";

// Scroll offset and sizes can have sub-pixel value if window.devicePixelRatio has decimal value
const SUBPIXEL_THRESHOLD = 1.5; // 0.5 * 3
Expand Down Expand Up @@ -173,11 +173,9 @@ export const createVirtualStore = (
return computeRange(cache, offset, _prevRange[0], viewportSize);
};
const getTotalSize = (): number => computeTotalSize(cache);
const getScrollableSize = (): number =>
getTotalSize() + startSpacerSize + endSpacerSize;
const getMaxScrollOffset = (): number =>
// total size can become smaller than viewport size
max(0, getScrollableSize() - viewportSize);
max(0, getTotalSize() + startSpacerSize - viewportSize);
const getItemOffset = (index: number): number => {
return computeStartOffset(cache, index) - pendingJump;
};
Expand Down Expand Up @@ -210,7 +208,7 @@ export const createVirtualStore = (
return _prevRange;
}
_prevRange = getRange(
scrollOffset - startSpacerSize + pendingJump + jump
max(0, scrollOffset - startSpacerSize + pendingJump + jump)
);

if (_frozenRange) {
Expand Down Expand Up @@ -259,7 +257,7 @@ export const createVirtualStore = (
const flushedIsJumpByShift = isJumpByShift;
jump = 0;
isJumpByShift = false;
if (viewportSize > getScrollableSize()) {
if (viewportSize > getTotalSize() + startSpacerSize + endSpacerSize) {
// In this case applying jump will not cause scroll.
// Current logic expects scroll event occurs after applying jump so discard it.
return [0, false];
Expand All @@ -281,16 +279,10 @@ export const createVirtualStore = (

switch (type) {
case ACTION_SCROLL: {
// Scroll offset may exceed min or max especially in Safari's elastic scrolling.
const nextScrollOffset = clamp(payload, 0, getMaxScrollOffset());
const flushedJump = _flushedJump;
_flushedJump = 0;
// Skip if offset is not changed
if (nextScrollOffset === scrollOffset) {
break;
}

const delta = nextScrollOffset - scrollOffset;
const delta = payload - scrollOffset;
const distance = abs(delta);

// Scroll event after jump compensation is not reliable because it may result in the opposite direction.
Expand Down Expand Up @@ -327,8 +319,16 @@ export const createVirtualStore = (
// Update synchronously if scrolled a lot
shouldSync = distance > viewportSize;

scrollOffset = nextScrollOffset;
mutated = UPDATE_VIRTUAL_STATE + UPDATE_SCROLL_EVENT;
scrollOffset = payload;
mutated = UPDATE_SCROLL_EVENT;
// Skip if offset is not changed
// Scroll offset may exceed min or max especially in Safari's elastic scrolling.
if (
scrollOffset >= startSpacerSize &&
scrollOffset <= getMaxScrollOffset()
) {
mutated += UPDATE_VIRTUAL_STATE;
}
break;
}
case ACTION_SCROLL_END: {
Expand Down

0 comments on commit bca7113

Please sign in to comment.