-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Radoslaw Szwajkowski <rszwajko@redhat.com>
- Loading branch information
Showing
5 changed files
with
71 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 31 additions & 25 deletions
56
client/src/app/components/InfiniteScroller/InfiniteScroller.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 22 additions & 32 deletions
54
client/src/app/components/InfiniteScroller/useVisibilityTracker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,40 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
const intersectionCallback = | ||
(stateCallback: () => void) => (entries: IntersectionObserverEntry[]) => { | ||
entries.forEach(({ isIntersecting }) => { | ||
if (isIntersecting) { | ||
stateCallback(); | ||
} | ||
}); | ||
}; | ||
|
||
export function useVisibilityTracker({ enable }: { enable: boolean }) { | ||
const loaderRef = useRef<HTMLDivElement>(null); | ||
const visibleZoneRef = useRef<HTMLDivElement>(null); | ||
const hiddenZoneRef = useRef<HTMLDivElement>(null); | ||
const [isVisible, setIsVisible] = useState(false); | ||
const nodeRef = useRef<HTMLDivElement>(null); | ||
const [visible, setVisible] = useState(false); | ||
const node = nodeRef.current; | ||
|
||
useEffect(() => { | ||
if ( | ||
!enable || | ||
!loaderRef.current || | ||
!visibleZoneRef.current || | ||
!hiddenZoneRef.current | ||
) { | ||
if (!enable || !node) { | ||
console.log("useVisibilityTracker - disabled"); | ||
return undefined; | ||
} | ||
|
||
const visibleZoneObserver = new IntersectionObserver( | ||
intersectionCallback(() => setIsVisible(true)), | ||
{ root: visibleZoneRef.current, rootMargin: "0px", threshold: 1.0 } | ||
); | ||
const hiddenZoneObserver = new IntersectionObserver( | ||
intersectionCallback(() => setIsVisible(false)), | ||
{ root: hiddenZoneRef.current, rootMargin: "0px", threshold: 1.0 } | ||
// observer with default options - the whole view port used | ||
// using a parent is hard | ||
const observer = new IntersectionObserver( | ||
(entries: IntersectionObserverEntry[]) => | ||
entries.forEach(({ isIntersecting, ...rest }) => { | ||
if (isIntersecting) { | ||
setVisible(true); | ||
console.log("useVisibilityTracker - intersection", rest); | ||
} else { | ||
setVisible(false); | ||
console.log("useVisibilityTracker - out-of-box", rest); | ||
} | ||
}) | ||
); | ||
visibleZoneObserver.observe(loaderRef.current); | ||
hiddenZoneObserver.observe(loaderRef.current); | ||
observer.observe(node); | ||
|
||
console.log("useVisibilityTracker - observe"); | ||
|
||
return () => { | ||
visibleZoneObserver.disconnect(); | ||
hiddenZoneObserver.disconnect(); | ||
observer.disconnect(); | ||
setVisible(false); | ||
console.log("useVisibilityTracker - disconnect"); | ||
}; | ||
}, [enable]); | ||
}, [enable, node]); | ||
|
||
return { isVisible, loaderRef, visibleZoneRef, hiddenZoneRef }; | ||
return { visible, nodeRef }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters