Skip to content

Commit

Permalink
fix: dynamic size scroll jumping
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed May 31, 2021
1 parent a88702c commit 48ab76f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/witty-boxes-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

fix: dynamic size scroll jumping
5 changes: 4 additions & 1 deletion src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default <
const correctScrollCountRef = useRef(0);
const rosRef = useRef<Map<Element, ResizeObserver>>(new Map());
const scrollOffsetRef = useRef(0);
const prevMeasureIdxRef = useRef(-1);
const prevVStopRef = useRef<number>();
const outerRef = useRef<O>(null);
const innerRef = useRef<I>(null);
Expand Down Expand Up @@ -350,7 +351,7 @@ export default <
const prevEnd = msData[i - 1]?.end || 0;

if (measuredSize !== size || start !== prevEnd) {
if (isScrolling && start < scrollOffset)
if (i < prevMeasureIdxRef.current && start < scrollOffset)
scrollTo(scrollOffset + measuredSize - size);

msDataRef.current[i] = getMeasure(i, measuredSize);
Expand All @@ -359,6 +360,8 @@ export default <
hasDynamicSizeRef.current = true;
}

prevMeasureIdxRef.current = i;

rosRef.current.get(target)?.disconnect();
rosRef.current.set(target, ro);
}).observe(el);
Expand Down

0 comments on commit 48ab76f

Please sign in to comment.