-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuseIntersectionObserver.ts
More file actions
118 lines (93 loc) · 3.35 KB
/
Copy pathuseIntersectionObserver.ts
File metadata and controls
118 lines (93 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { useEffect, useRef, useState } from "react";
/**
* @
* @see https://usehooks-ts.com/react-hook/use-intersection-observer
*/
type State = {
isIntersecting: boolean;
entry?: IntersectionObserverEntry;
};
type UseIntersectionObserverOptions = {
root?: Element | Document | null;
rootMargin?: string;
threshold?: number | number[];
freezeOnceVisible?: boolean;
onChange?: (isIntersecting: boolean, entry: IntersectionObserverEntry) => void;
initialIsIntersecting?: boolean;
};
type IntersectionReturn = [(node?: Element | null) => void, boolean, IntersectionObserverEntry | undefined] & {
ref: (node?: Element | null) => void;
isIntersecting: boolean;
entry?: IntersectionObserverEntry;
};
export function useIntersectionObserver({
threshold = 0,
root = null,
rootMargin = "0%",
freezeOnceVisible = false,
initialIsIntersecting = false,
onChange,
}: UseIntersectionObserverOptions = {}): IntersectionReturn {
const [ref, setRef] = useState<Element | null>(null);
const [state, setState] = useState<State>(() => ({
isIntersecting: initialIsIntersecting,
entry: undefined,
}));
const callbackRef = useRef<UseIntersectionObserverOptions["onChange"]>(undefined);
callbackRef.current = onChange;
const frozen = state.entry?.isIntersecting && freezeOnceVisible;
useEffect(() => {
// Ensure we have a ref to observe
if (!ref) return;
// Ensure the browser supports the Intersection Observer API
if (!("IntersectionObserver" in window)) return;
// Skip if frozen
if (frozen) return;
let unobserve: (() => void) | undefined;
const observer = new IntersectionObserver(
(entries: IntersectionObserverEntry[]): void => {
const thresholds = Array.isArray(observer.thresholds) ? observer.thresholds : [observer.thresholds];
entries.forEach((entry) => {
const isIntersecting =
entry.isIntersecting && thresholds.some((threshold) => entry.intersectionRatio >= threshold);
setState({ isIntersecting, entry });
if (callbackRef.current) {
callbackRef.current(isIntersecting, entry);
}
if (isIntersecting && freezeOnceVisible && unobserve) {
unobserve();
unobserve = undefined;
}
});
},
{ threshold, root, rootMargin },
);
observer.observe(ref);
return () => {
observer.disconnect();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
ref,
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(threshold),
root,
rootMargin,
frozen,
freezeOnceVisible,
]);
// ensures that if the observed element changes, the intersection observer is reinitialized
const prevRef = useRef<Element | null>(null);
useEffect(() => {
if (!ref && state.entry?.target && !freezeOnceVisible && !frozen && prevRef.current !== state.entry.target) {
prevRef.current = state.entry.target;
setState({ isIntersecting: initialIsIntersecting, entry: undefined });
}
}, [ref, state.entry, freezeOnceVisible, frozen, initialIsIntersecting]);
const result = [setRef, !!state.isIntersecting, state.entry] as IntersectionReturn;
// Support object destructuring, by adding the specific values.
result.ref = result[0];
result.isIntersecting = result[1];
result.entry = result[2];
return result;
}