2020-05-29 22:34:54 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
2020-07-25 09:06:25 +02:00
|
|
|
import debounce from 'util/debounce';
|
|
|
|
|
2021-07-05 05:27:22 +02:00
|
|
|
const DEBOUNCE_SCROLL_HANDLER_MS = 50;
|
2020-05-29 22:34:54 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: any,
|
2020-08-24 19:35:21 +02:00
|
|
|
skipWait?: boolean,
|
2021-06-15 08:41:03 +02:00
|
|
|
placeholder?: any,
|
2021-07-05 05:54:55 +02:00
|
|
|
yOffset?: number,
|
2020-05-29 22:34:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function WaitUntilOnPage(props: Props) {
|
2021-07-05 05:54:55 +02:00
|
|
|
const { yOffset } = props;
|
2020-05-29 22:34:54 +02:00
|
|
|
const ref = React.useRef();
|
|
|
|
const [shouldRender, setShouldRender] = React.useState(false);
|
|
|
|
|
2021-07-05 05:54:55 +02:00
|
|
|
const shouldElementRender = React.useCallback(
|
|
|
|
(ref) => {
|
|
|
|
const element = ref && ref.current;
|
|
|
|
if (element) {
|
|
|
|
const bounding = element.getBoundingClientRect();
|
2021-07-05 05:27:22 +02:00
|
|
|
// $FlowFixMe
|
2021-07-05 05:54:55 +02:00
|
|
|
const windowH = window.innerHeight || document.documentElement.clientHeight;
|
2021-07-05 05:27:22 +02:00
|
|
|
// $FlowFixMe
|
2021-07-05 05:54:55 +02:00
|
|
|
const windowW = window.innerWidth || document.documentElement.clientWidth;
|
|
|
|
|
|
|
|
const isApproachingViewport = yOffset && bounding.top < windowH + yOffset;
|
|
|
|
const isInViewport = // also covers "element is larger than viewport".
|
|
|
|
bounding.width > 0 &&
|
|
|
|
bounding.height > 0 &&
|
|
|
|
bounding.bottom >= 0 &&
|
|
|
|
bounding.right >= 0 &&
|
|
|
|
bounding.top <= windowH &&
|
|
|
|
bounding.left <= windowW;
|
|
|
|
|
|
|
|
return isInViewport || isApproachingViewport;
|
2021-07-05 05:27:22 +02:00
|
|
|
}
|
2021-07-05 05:54:55 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[yOffset]
|
|
|
|
);
|
2021-07-05 05:27:22 +02:00
|
|
|
|
|
|
|
// Handles "element is already in viewport when mounted".
|
2020-05-29 22:34:54 +02:00
|
|
|
React.useEffect(() => {
|
2021-07-05 05:27:22 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
if (!shouldRender && shouldElementRender(ref)) {
|
|
|
|
setShouldRender(true);
|
2020-05-29 22:34:54 +02:00
|
|
|
}
|
2021-07-05 05:27:22 +02:00
|
|
|
}, 500);
|
|
|
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
2020-05-29 22:34:54 +02:00
|
|
|
|
2021-07-05 05:27:22 +02:00
|
|
|
// Handles "element scrolled into viewport".
|
|
|
|
React.useEffect(() => {
|
|
|
|
const handleDisplayingRef = debounce(() => {
|
|
|
|
if (shouldElementRender(ref)) {
|
|
|
|
setShouldRender(true);
|
2020-05-29 22:34:54 +02:00
|
|
|
}
|
2020-07-25 09:06:25 +02:00
|
|
|
}, DEBOUNCE_SCROLL_HANDLER_MS);
|
2020-05-29 22:34:54 +02:00
|
|
|
|
2021-07-05 05:27:22 +02:00
|
|
|
if (ref && ref.current && !shouldRender) {
|
|
|
|
window.addEventListener('scroll', handleDisplayingRef);
|
2021-07-05 05:56:36 +02:00
|
|
|
window.addEventListener('resize', handleDisplayingRef);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('scroll', handleDisplayingRef);
|
|
|
|
window.removeEventListener('resize', handleDisplayingRef);
|
|
|
|
};
|
2020-05-29 22:34:54 +02:00
|
|
|
}
|
2021-07-05 05:27:22 +02:00
|
|
|
}, [ref, setShouldRender, shouldRender, shouldElementRender]);
|
2020-05-29 22:34:54 +02:00
|
|
|
|
2021-06-15 08:41:03 +02:00
|
|
|
const render = props.skipWait || shouldRender;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={ref}>
|
|
|
|
{render && props.children}
|
|
|
|
{!render && props.placeholder !== undefined && props.placeholder}
|
|
|
|
</div>
|
|
|
|
);
|
2020-05-29 22:34:54 +02:00
|
|
|
}
|