MCPcopy Create free account
hub / github.com/api-platform/website / useIntersection

Function useIntersection

pwa/hooks/useIntersection.tsx:4–30  ·  view source on GitHub ↗
(
  ref: RefObject<HTMLElement>,
  options: IntersectionObserverInit
)

Source from the content-addressed store, hash-verified

2import { RefObject, useEffect, useState } from "react";
3
4const useIntersection = (
5 ref: RefObject<HTMLElement>,
6 options: IntersectionObserverInit
7): IntersectionObserverEntry | null => {
8 const [intersectionObserverEntry, setIntersectionObserverEntry] =
9 useState<IntersectionObserverEntry | null>(null);
10
11 useEffect(() => {
12 if (ref.current && typeof IntersectionObserver === "function") {
13 const handler = (entries: IntersectionObserverEntry[]) => {
14 setIntersectionObserverEntry(entries[0]);
15 };
16
17 const observer = new IntersectionObserver(handler, options);
18 observer.observe(ref.current);
19
20 return () => {
21 setIntersectionObserverEntry(null);
22 observer.disconnect();
23 };
24 }
25 // eslint-disable-next-line @typescript-eslint/no-empty-function
26 return () => {};
27 }, [ref.current, options.threshold, options.root, options.rootMargin]);
28
29 return intersectionObserverEntry;
30};
31
32export default useIntersection;

Callers 2

SectionFunction · 0.85
useAnimationFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected