2020-06-28 13:37:04 +10:00
|
|
|
import React, { useContext, useEffect, useRef } from "react";
|
|
|
|
|
import { Box, Progress } from "theme-ui";
|
|
|
|
|
|
|
|
|
|
import MapLoadingContext from "../../contexts/MapLoadingContext";
|
|
|
|
|
|
|
|
|
|
function MapLoadingOverlay() {
|
|
|
|
|
const { isLoading, loadingProgressRef } = useContext(MapLoadingContext);
|
|
|
|
|
|
|
|
|
|
const requestRef = useRef();
|
|
|
|
|
const progressBarRef = useRef();
|
|
|
|
|
|
|
|
|
|
// Use an animation frame to update the progress bar
|
|
|
|
|
// This bypasses react allowing the animation to be smooth
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
function animate() {
|
|
|
|
|
if (!isLoading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
requestRef.current = requestAnimationFrame(animate);
|
2020-06-28 16:51:56 +10:00
|
|
|
if (progressBarRef.current) {
|
|
|
|
|
progressBarRef.current.value = loadingProgressRef.current;
|
|
|
|
|
}
|
2020-06-28 13:37:04 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestRef.current = requestAnimationFrame(animate);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelAnimationFrame(requestRef.current);
|
|
|
|
|
};
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [isLoading]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
isLoading && (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
2020-07-13 21:53:40 +10:00
|
|
|
left: "8px",
|
|
|
|
|
bottom: "8px",
|
2020-06-28 13:37:04 +10:00
|
|
|
flexDirection: "column",
|
2020-07-13 21:53:40 +10:00
|
|
|
borderRadius: "28px",
|
2020-10-27 18:26:09 +11:00
|
|
|
zIndex: 2,
|
2020-06-28 13:37:04 +10:00
|
|
|
}}
|
2020-07-13 21:53:40 +10:00
|
|
|
bg="overlay"
|
2020-06-28 13:37:04 +10:00
|
|
|
>
|
|
|
|
|
<Progress
|
|
|
|
|
ref={progressBarRef}
|
|
|
|
|
max={1}
|
|
|
|
|
value={0}
|
|
|
|
|
m={2}
|
|
|
|
|
sx={{ width: "32px" }}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapLoadingOverlay;
|