Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: view port observer on lost visibility #17417

Merged
merged 6 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/script/components/utils/InViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {viewportObserver} from 'Util/DOM/viewportObserver';

interface InViewportParams {
onVisible: () => void;
onVisibilityLost?: () => void;
requireFullyInView?: boolean;
allowBiggerThanViewport?: boolean;
/** Will check if the element is overlayed by something else. Can be used to be sure the user could actually see the element. Should not be used to do lazy loading as the overlayObserver has quite a long debounce time */
Expand All @@ -33,6 +34,7 @@ interface InViewportParams {
const InViewport: React.FC<InViewportParams & React.HTMLProps<HTMLDivElement>> = ({
children,
onVisible,
onVisibilityLost,
requireFullyInView = false,
checkOverlay = false,
allowBiggerThanViewport = false,
Expand All @@ -58,15 +60,23 @@ const InViewport: React.FC<InViewportParams & React.HTMLProps<HTMLDivElement>> =
const triggerCallbackIfVisible = () => {
if (inViewport && visible) {
onVisible();
releaseTrackers();

if (!onVisibilityLost) {
releaseTrackers();
}
Comment on lines +64 to +66
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to release the trackers after detecting element's visibility (we want to detect its visibility lost later).

}
};

viewportObserver.trackElement(
element,
(isInViewport: boolean) => {
(isInViewport: boolean, isPartiallyVisible: boolean) => {
inViewport = isInViewport;
triggerCallbackIfVisible();

// If the element is not intersecting at all, we can trigger the onVisibilityLost callback
if (!isPartiallyVisible) {
onVisibilityLost?.();
}
},
requireFullyInView,
allowBiggerThanViewport,
Expand All @@ -78,7 +88,7 @@ const InViewport: React.FC<InViewportParams & React.HTMLProps<HTMLDivElement>> =
});
}
return () => releaseTrackers();
}, [allowBiggerThanViewport, requireFullyInView, checkOverlay, onVisible]);
}, [allowBiggerThanViewport, requireFullyInView, checkOverlay, onVisible, onVisibilityLost]);

return (
<div ref={domNode} {...props} css={{minHeight: '1px'}}>
Expand Down
29 changes: 21 additions & 8 deletions src/script/util/DOM/viewportObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
*
*/

const observedElements = new Map();
const observedElements = new Map<
Element,
{
allowBiggerThanViewport?: boolean;
requireFullyInView?: boolean;
onVisible?: Function;
onVisibilityChange?: (isVisible: boolean, isPartiallyVisible: boolean) => void;
Comment on lines +21 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added types here and renamed onChange to onVisibilityChange for more clarity.

}
>();
const tolerance = 0.8;

const onIntersect: IntersectionObserverCallback = entries => {
entries.forEach(({intersectionRatio, isIntersecting, target: element, rootBounds}) => {
const {onVisible, onChange, requireFullyInView, allowBiggerThanViewport} = observedElements.get(element) || {};
const {onVisible, onVisibilityChange, requireFullyInView, allowBiggerThanViewport} =
observedElements.get(element) || {};
const isFullyInView = intersectionRatio >= tolerance;

const isBiggerThanRoot = () => {
Expand All @@ -35,11 +44,11 @@ const onIntersect: IntersectionObserverCallback = entries => {

const isVisible = isIntersecting && (!requireFullyInView || isFullyInView || isBiggerThanRoot());

if (onChange) {
onChange(isVisible);
if (onVisibilityChange) {
onVisibilityChange(!!isVisible, isIntersecting);
} else if (isVisible) {
removeElement(element);
return onVisible && onVisible();
return onVisible?.();
}
});
};
Expand Down Expand Up @@ -72,18 +81,22 @@ const onElementInViewport = (
* Will track an element and trigger the callback whenever the intersecting state changes
*
* @param element the element to observe
* @param onChange the callback to call when the element intersects or not
* @param onVisibilityChange the callback to call when the element intersects or not
* @param requireFullyInView should the element be fully in view
* @param allowBiggerThanViewport should fire when element is bigger than viewport
*/
const trackElement = (
element: HTMLElement,
onChange: Function,
onVisibilityChange: (isVisible: boolean, isPartiallyVisible: boolean) => void,
requireFullyInView = false,
allowBiggerThanViewport = false,
): void => {
if (element) {
observedElements.set(element, {allowBiggerThanViewport, onChange, requireFullyInView});
observedElements.set(element, {
allowBiggerThanViewport,
onVisibilityChange,
requireFullyInView,
});
return observer.observe(element);
}
};
Expand Down
Loading