feat: implement auto scroll

This commit is contained in:
shadcn 2023-06-15 15:27:41 +04:00
parent a9993640ff
commit b8d3ba85f5
6 changed files with 71 additions and 17 deletions

View file

@ -0,0 +1,23 @@
import * as React from 'react'
export function useAtBottom(offset = 0) {
const [isAtBottom, setIsAtBottom] = React.useState(false)
React.useEffect(() => {
const handleScroll = () => {
setIsAtBottom(
window.innerHeight + window.scrollY ===
document.body.offsetHeight - offset
)
}
window.addEventListener('scroll', handleScroll, { passive: true })
handleScroll()
return () => {
window.removeEventListener('scroll', handleScroll)
}
}, [offset])
return isAtBottom
}