Front-page lazy-load fixes and improvements #6388
1 changed files with 37 additions and 22 deletions
|
@ -2,7 +2,7 @@
|
||||||
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,
|
||||||
|
@ -14,33 +14,48 @@ export default function WaitUntilOnPage(props: 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((ref) => {
|
||||||
const handleDisplayingRef = debounce((e) => {
|
const element = ref && ref.current;
|
||||||
const element = ref && ref.current;
|
if (element) {
|
||||||
if (element) {
|
const bounding = element.getBoundingClientRect();
|
||||||
const bounding = element.getBoundingClientRect();
|
if (
|
||||||
if (
|
bounding.width > 0 &&
|
||||||
bounding.bottom >= 0 &&
|
bounding.height > 0 &&
|
||||||
bounding.right >= 0 &&
|
bounding.bottom >= 0 &&
|
||||||
// $FlowFixMe
|
bounding.right >= 0 &&
|
||||||
bounding.top <= (window.innerHeight || document.documentElement.clientHeight) &&
|
// $FlowFixMe
|
||||||
// $FlowFixMe
|
bounding.top <= (window.innerHeight || document.documentElement.clientHeight) &&
|
||||||
bounding.left <= (window.innerWidth || document.documentElement.clientWidth)
|
// $FlowFixMe
|
||||||
) {
|
bounding.left <= (window.innerWidth || document.documentElement.clientWidth)
|
||||||
setShouldRender(true);
|
) {
|
||||||
}
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}, []);
|
||||||
|
|
||||||
if (element && !shouldRender) {
|
// Handles "element is already in viewport when mounted".
|
||||||
window.addEventListener('scroll', handleDisplayingRef);
|
React.useEffect(() => {
|
||||||
return () => window.removeEventListener('scroll', handleDisplayingRef);
|
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);
|
||||||
|
return () => window.removeEventListener('scroll', handleDisplayingRef);
|
||||||
}
|
}
|
||||||
}, [ref, setShouldRender, shouldRender]);
|
}, [ref, setShouldRender, shouldRender, shouldElementRender]);
|
||||||
|
|
||||||
const render = props.skipWait || shouldRender;
|
const render = props.skipWait || shouldRender;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue