Front-page lazy-load fixes and improvements #6388

Merged
infinite-persistence merged 3 commits from ip/front.page.lazy.load.2 into master 2021-07-05 10:04:34 +02:00
2 changed files with 46 additions and 18 deletions

View file

@ -2,45 +2,73 @@
import React from 'react'; import React from 'react';
import debounce from 'util/debounce'; import debounce from 'util/debounce';
const DEBOUNCE_SCROLL_HANDLER_MS = 25; const DEBOUNCE_SCROLL_HANDLER_MS = 50;
type Props = { type Props = {
children: any, children: any,
skipWait?: boolean, skipWait?: boolean,
placeholder?: any, placeholder?: any,
yOffset?: number,
}; };
export default function WaitUntilOnPage(props: Props) { export default function WaitUntilOnPage(props: Props) {
const { yOffset } = props;
const ref = React.useRef(); const ref = React.useRef();
const [shouldRender, setShouldRender] = React.useState(false); const [shouldRender, setShouldRender] = React.useState(false);
React.useEffect(() => { const shouldElementRender = React.useCallback(
const handleDisplayingRef = debounce((e) => { (ref) => {
const element = ref && ref.current; const element = ref && ref.current;
if (element) { if (element) {
const bounding = element.getBoundingClientRect(); const bounding = element.getBoundingClientRect();
if ( // $FlowFixMe
const windowH = window.innerHeight || document.documentElement.clientHeight;
// $FlowFixMe
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.bottom >= 0 &&
bounding.right >= 0 && bounding.right >= 0 &&
// $FlowFixMe bounding.top <= windowH &&
bounding.top <= (window.innerHeight || document.documentElement.clientHeight) && bounding.left <= windowW;
// $FlowFixMe
bounding.left <= (window.innerWidth || document.documentElement.clientWidth) return isInViewport || isApproachingViewport;
) {
setShouldRender(true);
}
} }
if (element && !shouldRender) { return false;
window.addEventListener('scroll', handleDisplayingRef); },
return () => window.removeEventListener('scroll', handleDisplayingRef); [yOffset]
);
// Handles "element is already in viewport when mounted".
React.useEffect(() => {
setTimeout(() => {
if (!shouldRender && shouldElementRender(ref)) {
setShouldRender(true);
}
}, 500);
}, []); // eslint-disable-line react-hooks/exhaustive-deps
// Handles "element scrolled into viewport".
React.useEffect(() => {
const handleDisplayingRef = debounce(() => {
if (shouldElementRender(ref)) {
setShouldRender(true);
} }
}, DEBOUNCE_SCROLL_HANDLER_MS); }, DEBOUNCE_SCROLL_HANDLER_MS);
if (ref) { if (ref && ref.current && !shouldRender) {
handleDisplayingRef(); window.addEventListener('scroll', handleDisplayingRef);
window.addEventListener('resize', handleDisplayingRef);
return () => {
window.removeEventListener('scroll', handleDisplayingRef);
window.removeEventListener('resize', handleDisplayingRef);
};
} }
}, [ref, setShouldRender, shouldRender]); }, [ref, setShouldRender, shouldRender, shouldElementRender]);
const render = props.skipWait || shouldRender; const render = props.skipWait || shouldRender;

View file

@ -63,7 +63,7 @@ function HomePage(props: Props) {
{index === 0 && <>{claimTiles}</>} {index === 0 && <>{claimTiles}</>}
{index !== 0 && ( {index !== 0 && (
<WaitUntilOnPage name={title} placeholder={tilePlaceholder}> <WaitUntilOnPage name={title} placeholder={tilePlaceholder} yOffset={800}>
{claimTiles} {claimTiles}
</WaitUntilOnPage> </WaitUntilOnPage>
)} )}