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 26 additions and 17 deletions
Showing only changes of commit bb8fb038ca - Show all commits

View file

@ -8,31 +8,40 @@ type Props = {
children: any,
skipWait?: boolean,
placeholder?: any,
yOffset?: number,
};
export default function WaitUntilOnPage(props: Props) {
const { yOffset } = props;
const ref = React.useRef();
const [shouldRender, setShouldRender] = React.useState(false);
const shouldElementRender = React.useCallback((ref) => {
const element = ref && ref.current;
if (element) {
const bounding = element.getBoundingClientRect();
if (
bounding.width > 0 &&
bounding.height > 0 &&
bounding.bottom >= 0 &&
bounding.right >= 0 &&
const shouldElementRender = React.useCallback(
(ref) => {
const element = ref && ref.current;
if (element) {
const bounding = element.getBoundingClientRect();
// $FlowFixMe
bounding.top <= (window.innerHeight || document.documentElement.clientHeight) &&
const windowH = window.innerHeight || document.documentElement.clientHeight;
// $FlowFixMe
bounding.left <= (window.innerWidth || document.documentElement.clientWidth)
) {
return true;
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;
}
}
return false;
}, []);
return false;
},
[yOffset]
);
// Handles "element is already in viewport when mounted".
React.useEffect(() => {

View file

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