Skip to content

Commit

Permalink
Merge pull request #1761 from didi/fix-rn-scroll-into-view
Browse files Browse the repository at this point in the history
Fix rn scroll into view
  • Loading branch information
hiyuki authored Dec 19, 2024
2 parents a013670 + f7a515b commit ab50d03
Showing 1 changed file with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
const initialTimeout = useRef<ReturnType<typeof setTimeout> | null>(null)
const intersectionObservers = useContext(IntersectionObserverContext)

const snapScrollIntoView = useRef<string>('')
const firstScrollIntoViewChange = useRef<boolean>(false)

const {
normalStyle,
Expand Down Expand Up @@ -220,23 +220,28 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
}, [refresherTriggered])

useEffect(() => {
if (scrollIntoView && __selectRef && snapScrollIntoView.current !== scrollIntoView) {
snapScrollIntoView.current = scrollIntoView || ''
setTimeout(() => {
const refs = __selectRef(`#${scrollIntoView}`, 'node')
if (refs) {
const { nodeRef } = refs.getNodeInstance()
nodeRef.current?.measureLayout(
scrollViewRef.current,
(left: number, top:number) => {
scrollToOffset(left, top)
}
)
}
})
if (scrollIntoView && __selectRef) {
if (!firstScrollIntoViewChange.current) {
setTimeout(handleScrollIntoView)
} else {
handleScrollIntoView()
}
}
firstScrollIntoViewChange.current = true
}, [scrollIntoView])

function handleScrollIntoView () {
const refs = __selectRef!(`#${scrollIntoView}`, 'node')
if (!refs) return
const { nodeRef } = refs.getNodeInstance()
nodeRef.current?.measureLayout(
scrollViewRef.current,
(left: number, top:number) => {
scrollToOffset(left, top)
}
)
}

function selectLength (size: { height: number; width: number }) {
return !scrollX ? size.height : size.width
}
Expand Down

0 comments on commit ab50d03

Please sign in to comment.