@ilokesto/utilinent 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,6 +6,7 @@ export function useIntersectionObserver({ threshold = 0, root = null, rootMargin
|
|
|
6
6
|
const onChangeRef = useRef(onChange);
|
|
7
7
|
const isFirstCallbackRef = useRef(true);
|
|
8
8
|
const isFrozen = useRef(false);
|
|
9
|
+
const prevIsIntersectingRef = useRef(initialIsIntersecting);
|
|
9
10
|
// Keep callback ref updated
|
|
10
11
|
useEffect(() => {
|
|
11
12
|
onChangeRef.current = onChange;
|
|
@@ -34,6 +35,8 @@ export function useIntersectionObserver({ threshold = 0, root = null, rootMargin
|
|
|
34
35
|
: [observer.thresholds];
|
|
35
36
|
const isCurrentlyIntersecting = intersectionEntry.isIntersecting &&
|
|
36
37
|
thresholds.some((t) => intersectionEntry.intersectionRatio >= t);
|
|
38
|
+
const wasIntersecting = prevIsIntersectingRef.current;
|
|
39
|
+
prevIsIntersectingRef.current = isCurrentlyIntersecting;
|
|
37
40
|
// Update state
|
|
38
41
|
setIsIntersecting(isCurrentlyIntersecting);
|
|
39
42
|
setEntry(intersectionEntry);
|
|
@@ -43,7 +46,9 @@ export function useIntersectionObserver({ threshold = 0, root = null, rootMargin
|
|
|
43
46
|
return;
|
|
44
47
|
}
|
|
45
48
|
// Call onChange callback
|
|
46
|
-
|
|
49
|
+
if (!wasIntersecting && isCurrentlyIntersecting) {
|
|
50
|
+
onChangeRef.current?.(isCurrentlyIntersecting, intersectionEntry);
|
|
51
|
+
}
|
|
47
52
|
// Freeze if triggerOnce and now intersecting
|
|
48
53
|
if (freezeOnceVisible && isCurrentlyIntersecting) {
|
|
49
54
|
isFrozen.current = true;
|
|
@@ -62,6 +67,7 @@ export function useIntersectionObserver({ threshold = 0, root = null, rootMargin
|
|
|
62
67
|
setIsIntersecting(initialIsIntersecting);
|
|
63
68
|
setEntry(undefined);
|
|
64
69
|
isFirstCallbackRef.current = true;
|
|
70
|
+
prevIsIntersectingRef.current = initialIsIntersecting;
|
|
65
71
|
if (!freezeOnceVisible) {
|
|
66
72
|
isFrozen.current = false;
|
|
67
73
|
}
|