(callback, ioOptions)
| 10 | * @return {function(Element):void} |
| 11 | */ |
| 12 | export function useIntersectionObserver(callback, ioOptions) { |
| 13 | /** @type {import('preact/hooks').MutableRef<import('#core/types/function/types').UnlistenCallback?>} */ |
| 14 | const unobserveRef = useRef(null); |
| 15 | const refCb = useCallback( |
| 16 | /** @param {Element} node */ |
| 17 | (node) => { |
| 18 | const cleanup = unobserveRef.current; |
| 19 | if (cleanup) { |
| 20 | cleanup(); |
| 21 | unobserveRef.current = null; |
| 22 | } |
| 23 | |
| 24 | if (!node) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | unobserveRef.current = observeIntersections(node, callback, ioOptions); |
| 29 | }, |
| 30 | [callback, ioOptions] |
| 31 | ); |
| 32 | |
| 33 | return refCb; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns whether an element is within the viewport. |
no test coverage detected