mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
fix: set DOM scrollTop when programmatically scroll to item
This commit is contained in:
parent
d6e9fcc163
commit
ce3dffb84d
1 changed files with 5 additions and 8 deletions
|
|
@ -31,7 +31,6 @@ export function useVirtualScroll<E extends HTMLElement>({
|
|||
|
||||
const ref = React.useRef<E | null>(null) // TODO: compare DOM native event listener
|
||||
const [scrollTop, setScrollTop] = React.useState(0)
|
||||
const [controlledScrollTop, setControlledScrollTop] = React.useState(0)
|
||||
|
||||
const onScroll = React.useCallback((e: React.UIEvent<E, UIEvent>) => {
|
||||
setScrollTop(e.currentTarget.scrollTop)
|
||||
|
|
@ -75,12 +74,6 @@ export function useVirtualScroll<E extends HTMLElement>({
|
|||
[indexes, memoizedStyler],
|
||||
)
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
if (ref.current) {
|
||||
ref.current.scrollTop = controlledScrollTop
|
||||
}
|
||||
}, [controlledScrollTop])
|
||||
|
||||
const containerStyle: React.CSSProperties = React.useMemo(
|
||||
() => ({
|
||||
height: totalHeight,
|
||||
|
|
@ -95,7 +88,11 @@ export function useVirtualScroll<E extends HTMLElement>({
|
|||
|
||||
const updateScrollPosition = (scrollTop: number) => {
|
||||
setScrollTop(scrollTop)
|
||||
setControlledScrollTop(scrollTop)
|
||||
|
||||
// Note: storing the scrollTop into a state and update DOM element scrollTop inside a layout effect would not work.
|
||||
if (ref.current) {
|
||||
ref.current.scrollTop = scrollTop
|
||||
}
|
||||
}
|
||||
|
||||
switch (mode) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue