I'm working on an app in Next.js and I'm trying to include a loading animation for the app. The animation is supposed to be three bouncing balls which correctly shows up. If I start the nextjs app with npm run dev
it works correctly and as long as I refresh the same page it loads like its supposed to. But if I navigate to a different route the animation doesn't appear. The elements themselves don't appear. I'm not sure why it's not working, I've put the code below for reference.
import React from "react";
import Image from 'next/image'
import styled from "styled-components";
const Screen = styled.div`
position: relative;
height: 100vh;
width: 100%;
opacity: 0;
animation: fade 0.4s ease-in forwards;
@keyframes fade {
0% {
opacity: 0.4;
}
50% {
opacity: 0.8;
}
100% {
opacity: 1;
}
}
`;
const Balls = styled.div`
display: flex;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
.ball {
height: 30px;
width: 30px;
border-radius: 50%;
background: #E60E33;
margin: 0 60px 0 0;
animation: oscillate 0.7s ease-in forwards infinite;
}
.one {
animation-delay: 0.5s;
}
.two {
animation-delay: 1s;
}
.three {
animation-delay: 1.5s;
}
@keyframes oscillate {
0% {
transform: translateY(0);
}
50% {
transform: translateY(20px);
}
100% {
transform: translateY(0);
}
}
`;
function MyApp({ Component, pageProps }) {
const [loading, setLoading] = React.useState(false);
React.useEffect(() =>{
setTimeout(() => setLoading(true), 5000);
})
return (
<>
{loading ? (
<React.Fragment>
<Component {...pageProps} />
</React.Fragment>
) : (
<Screen>
<div
style={{
display: "flex",
position: "absolute",
top: "35%",
left: "48%",
transform: "translate(-50%, -50%)"
}}
>
</div>
<Balls>
<div className="ball one"></div>
<div className="ball two"></div>
<div className="ball three"></div>
</Balls>
</Screen>
)}
</>
);
}
export default MyApp