-
Notifications
You must be signed in to change notification settings - Fork 167
fix: scrollbar is shaking when dropdown scroll to bottom #226
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
996f6e8
fix: scrollbar is shaking when dropdown scroll to bottom
bbb169 2cb4a6e
refactor: rename maxScrollHeight to maxScrollHeightRef
bbb169 aa8ceff
chore: fix typo
zombieJ 550cfad
Merge remote-tracking branch 'origin/master' into fixScrollShake
zombieJ 9ced492
chore: fix typo
zombieJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import type CacheMap from '../utils/CacheMap'; | ||
import type { ScrollToCacheState } from '../utils/scrollToCacheState'; | ||
import { MEASURE } from '../utils/scrollToCacheState'; | ||
import React, { useRef } from 'react'; | ||
|
||
export default function useCalcPosition<T>( | ||
scrollToCacheState: ScrollToCacheState, | ||
fillerInnerRef: React.MutableRefObject<HTMLDivElement>, | ||
getKey: (item: T) => React.Key, | ||
inVirtual: boolean, | ||
heights: CacheMap, | ||
itemHeight: number, | ||
useVirtual: boolean, | ||
offsetTop: number, | ||
mergedData: T[], | ||
heightUpdatedMark: number, | ||
height: number, | ||
): [number, number, number, number, number, number, React.MutableRefObject<number>] { | ||
const lastScrollInfos = useRef<[number, number, number, number]>([0, 0, 0, 0]); | ||
const [lastScrollHeight, lastFillerOffset, lastStartIndex, lastEndIndex] = | ||
lastScrollInfos.current; | ||
|
||
const maxScrollHeightRef = useRef(-1); | ||
|
||
const { | ||
scrollHeight, | ||
start, | ||
end, | ||
offset: fillerOffset, | ||
} = React.useMemo(() => { | ||
if (!useVirtual) { | ||
return { | ||
scrollHeight: undefined, | ||
start: 0, | ||
end: mergedData.length - 1, | ||
offset: undefined, | ||
}; | ||
} | ||
|
||
// Always use virtual scroll bar in avoid shaking | ||
if (!inVirtual) { | ||
return { | ||
scrollHeight: fillerInnerRef.current?.offsetHeight || 0, | ||
start: 0, | ||
end: mergedData.length - 1, | ||
offset: undefined, | ||
}; | ||
} | ||
|
||
let itemTop = 0; | ||
let startIndex: number; | ||
let startOffset: number; | ||
let endIndex: number; | ||
|
||
const dataLen = mergedData.length; | ||
for (let i = 0; i < dataLen; i += 1) { | ||
const item = mergedData[i]; | ||
const key = getKey(item); | ||
|
||
const cacheHeight = heights.get(key); | ||
const currentItemBottom = itemTop + (cacheHeight === undefined ? itemHeight : cacheHeight); | ||
|
||
// Check item top in the range | ||
if (currentItemBottom >= offsetTop && startIndex === undefined) { | ||
startIndex = i; | ||
startOffset = itemTop; | ||
} | ||
|
||
// Check item bottom in the range. We will render additional one item for motion usage | ||
if (currentItemBottom > offsetTop + height && endIndex === undefined) { | ||
endIndex = i; | ||
} | ||
|
||
itemTop = currentItemBottom; | ||
} | ||
|
||
// When scrollTop at the end but data cut to small count will reach this | ||
if (startIndex === undefined) { | ||
startIndex = 0; | ||
startOffset = 0; | ||
|
||
endIndex = Math.ceil(height / itemHeight); | ||
} | ||
if (endIndex === undefined) { | ||
endIndex = mergedData.length - 1; | ||
} | ||
|
||
// Give cache to improve scroll experience | ||
endIndex = Math.min(endIndex + 1, mergedData.length - 1); | ||
|
||
const result = { | ||
scrollHeight: itemTop, | ||
start: startIndex, | ||
end: endIndex, | ||
offset: startOffset, | ||
}; | ||
|
||
// scrollToCacheState means listChildren is not correct, just use last offset to avoid seeing nothing. | ||
if (scrollToCacheState === MEASURE) { | ||
// makes `keepInRange` pass new `offsetTop` | ||
maxScrollHeightRef.current = Math.max(lastScrollHeight, itemTop) - height; | ||
|
||
return { | ||
...result, | ||
offset: lastFillerOffset, | ||
scrollHeight: lastScrollHeight, | ||
}; | ||
} | ||
|
||
lastScrollInfos.current = [itemTop, startOffset, startIndex, endIndex]; | ||
|
||
return result; | ||
}, [inVirtual, useVirtual, offsetTop, mergedData, heightUpdatedMark, height, scrollToCacheState]); | ||
|
||
// =============================== maxScrollHeight for `In Range` =============================== | ||
const maxScrollHeight = scrollHeight - height; | ||
// init | ||
if (maxScrollHeightRef.current === -1) { | ||
maxScrollHeightRef.current = maxScrollHeight; | ||
} | ||
if (scrollToCacheState !== MEASURE) { | ||
maxScrollHeightRef.current = maxScrollHeight; | ||
} | ||
|
||
return [scrollHeight, start, end, lastStartIndex, lastEndIndex, fillerOffset, maxScrollHeightRef]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.