Snap the header scroll on the web

This commit is contained in:
Dan Abramov
2023-12-23 18:16:44 +00:00
parent d9c44c373a
commit e582713e23
3 changed files with 64 additions and 19 deletions
+17 -3
View File
@@ -109,7 +109,7 @@ function ListImpl<ItemT>(
const containerRef = useRef(null)
useResizeObserver(containerRef, onContentSizeChange)
// --- onScroll ---
// --- onScroll & onScrollEndWeb ---
const [isInsideVisibleTree, setIsInsideVisibleTree] = React.useState(false)
const handleWindowScroll = useNonReactiveCallback(() => {
if (isInsideVisibleTree) {
@@ -124,6 +124,16 @@ function ListImpl<ItemT>(
)
}
})
const handleWindowScrollEnd = useNonReactiveCallback(() => {
if (isInsideVisibleTree) {
contextScrollHandlers.onScrollEndWeb?.({
contentOffset: {
x: window.scrollX,
y: window.scrollY,
},
})
}
})
React.useEffect(() => {
if (!isInsideVisibleTree) {
// Prevents hidden tabs from firing scroll events.
@@ -131,8 +141,12 @@ function ListImpl<ItemT>(
return
}
window.addEventListener('scroll', handleWindowScroll)
return () => window.removeEventListener('scroll', handleWindowScroll)
}, [isInsideVisibleTree, handleWindowScroll])
window.addEventListener('scrollend', handleWindowScrollEnd)
return () => {
window.removeEventListener('scroll', handleWindowScroll)
window.removeEventListener('scrollend', handleWindowScrollEnd)
}
}, [isInsideVisibleTree, handleWindowScroll, handleWindowScrollEnd])
// --- onScrolledDownChange ---
const isScrolledDown = useRef(false)