WaitUntilOnPage: add option to load when approaching viewport
This commit is contained in:
parent
9745d6df3e
commit
bb8fb038ca
2 changed files with 26 additions and 17 deletions
|
@ -8,31 +8,40 @@ 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);
|
||||||
|
|
||||||
const shouldElementRender = React.useCallback((ref) => {
|
const shouldElementRender = React.useCallback(
|
||||||
const element = ref && ref.current;
|
(ref) => {
|
||||||
if (element) {
|
const element = ref && ref.current;
|
||||||
const bounding = element.getBoundingClientRect();
|
if (element) {
|
||||||
if (
|
const bounding = element.getBoundingClientRect();
|
||||||
bounding.width > 0 &&
|
|
||||||
bounding.height > 0 &&
|
|
||||||
bounding.bottom >= 0 &&
|
|
||||||
bounding.right >= 0 &&
|
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
bounding.top <= (window.innerHeight || document.documentElement.clientHeight) &&
|
const windowH = window.innerHeight || document.documentElement.clientHeight;
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
bounding.left <= (window.innerWidth || document.documentElement.clientWidth)
|
const windowW = window.innerWidth || document.documentElement.clientWidth;
|
||||||
) {
|
|
||||||
return true;
|
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".
|
// Handles "element is already in viewport when mounted".
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
|
|
@ -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>
|
||||||
)}
|
)}
|
||||||
|
|
Loading…
Add table
Reference in a new issue