MCPcopy Create free account
hub / github.com/ampproject/amphtml / useIntersectionObserver

Function useIntersectionObserver

src/preact/component/intersection-observer.js:12–34  ·  view source on GitHub ↗
(callback, ioOptions)

Source from the content-addressed store, hash-verified

10 * @return {function(Element):void}
11 */
12export 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.

Callers 3

ComponentFunction · 0.90
BentoTimeagoFunction · 0.90
BentoIframeFunction · 0.90

Calls 1

observeIntersectionsFunction · 0.90

Tested by

no test coverage detected