mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2024-12-04 06:33:36 +08:00
26 lines
565 B
TypeScript
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
|