YesPlayMusic/packages/web/hooks/useIntersectionObserver.ts
2022-07-11 11:06:41 +08:00

26 lines
565 B
TypeScript

import { useState, useEffect, RefObject } from 'react'
const useIntersectionObserver = (
element: RefObject<Element>
): { onScreen: boolean } => {
const [onScreen, setOnScreen] = useState(false)
useEffect(() => {
if (element.current) {
const observer = new IntersectionObserver(([entry]) =>
setOnScreen(entry.isIntersecting)
)
observer.observe(element.current)
return () => {
observer.disconnect()
}
}
}, [element, setOnScreen])
return {
onScreen,
}
}
export default useIntersectionObserver