Skip to content

enhance: Scroll top should not shaking #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,26 @@
rangeRef.current.start = start;
rangeRef.current.end = end;

// When scroll up, first visible item get real height may not same as `itemHeight`,
// Which will make scroll jump.
// Let's sync scroll top to avoid jump
React.useLayoutEffect(() => {
const changedRecord = heights.getRecord();
if (changedRecord.size === 1) {
const recordKey = Array.from(changedRecord)[0];
const startIndexKey = getKey(mergedData[start]);

Check warning on line 281 in src/List.tsx

View check run for this annotation

Codecov / codecov/patch

src/List.tsx#L280-L281

Added lines #L280 - L281 were not covered by tests
if (startIndexKey === recordKey) {
const realStartHeight = heights.get(recordKey);
const diffHeight = realStartHeight - itemHeight;
syncScrollTop((ori) => {
return ori + diffHeight;

Check warning on line 286 in src/List.tsx

View check run for this annotation

Codecov / codecov/patch

src/List.tsx#L283-L286

Added lines #L283 - L286 were not covered by tests
});
}
}

heights.resetRecord();
}, [scrollHeight]);

// ================================= Size =================================
const [size, setSize] = React.useState({ width: 0, height });

Expand Down
14 changes: 10 additions & 4 deletions src/hooks/useHeights.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import raf from 'rc-util/lib/raf';
import * as React from 'react';
import { useEffect, useRef } from 'react';
import type { GetKey } from '../interface';
Expand All @@ -23,10 +22,11 @@ export default function useHeights<T>(
const [updatedMark, setUpdatedMark] = React.useState(0);
const instanceRef = useRef(new Map<React.Key, HTMLElement>());
const heightsRef = useRef(new CacheMap());
const collectRafRef = useRef<number>();

const promiseIdRef = useRef<number>(0);

function cancelRaf() {
raf.cancel(collectRafRef.current);
promiseIdRef.current += 1;
}

function collectHeight(sync = false) {
Expand Down Expand Up @@ -56,7 +56,13 @@ export default function useHeights<T>(
if (sync) {
doCollect();
} else {
collectRafRef.current = raf(doCollect);
promiseIdRef.current += 1;
const id = promiseIdRef.current;
Promise.resolve().then(() => {
if (id === promiseIdRef.current) {
doCollect();
}
});
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/utils/CacheMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ class CacheMap {
// `useMemo` no need to update if `id` not change
id: number = 0;

diffKeys = new Set<React.Key>();

constructor() {
this.maps = Object.create(null);
}

set(key: React.Key, value: number) {
this.maps[key as string] = value;
this.id += 1;
this.diffKeys.add(key as string);
}

get(key: React.Key) {
return this.maps[key as string];
}

/**
* CacheMap will record the key changed.
* To help to know what's update in the next render.
*/
resetRecord() {
this.diffKeys.clear();
}

getRecord() {
return this.diffKeys;
}
}

export default CacheMap;
Loading