Created
March 9, 2022 09:12
-
-
Save arshkkk/b3555c65441ff170d942c4e5309360a2 to your computer and use it in GitHub Desktop.
NextJS Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MyApp({Component, pageProps}: any) { | |
return ( | |
<> | |
<Loader /> | |
<Component {...pageProps} /> | |
</> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import { SvgLoader } from "./SvgLoader"; | |
export const Loader = () => { | |
const router = useRouter(); | |
const [isLoaderVisible, setIsLoaderVisible] = useState(false); | |
React.useEffect(() => { | |
const handleRouteChange = (url, { shallow }) => { | |
setIsLoaderVisible(true); | |
}; | |
const handleRouteComplete = (url, { shallow }) => { | |
setIsLoaderVisible(false); | |
setDirty(false); | |
}; | |
router.events.on("routeChangeStart", handleRouteChange); | |
router.events.on("routeChangeComplete", handleRouteComplete); | |
return () => { | |
router.events.off("routeChangeStart", handleRouteChange); | |
router.events.off("routeChangeComplete", handleRouteComplete); | |
}; | |
}, []); | |
if (isLoaderVisible) { | |
return ( | |
<div | |
style={{ | |
position: "fixed", | |
inset: 0, | |
display: "flex", | |
justifyContent: "center", | |
alignItems: "center", | |
}} | |
> | |
<SvgLoader /> | |
</div> | |
); | |
} else return null; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const SvgLoader = () => ( | |
<div style={{width: '100px'}}> | |
<svg | |
version="1.1" | |
id="L4" | |
xmlns="http://www.w3.org/2000/svg" | |
x="0px" | |
y="0px" | |
viewBox="0 0 100 100" | |
enable-background="new 0 0 0 0" | |
> | |
<circle fill="#fff" stroke="none" cx="6" cy="50" r="6"> | |
<animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.1" /> | |
</circle> | |
<circle fill="#fff" stroke="none" cx="26" cy="50" r="6"> | |
<animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.2" /> | |
</circle> | |
<circle fill="#fff" stroke="none" cx="46" cy="50" r="6"> | |
<animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.3" /> | |
</circle> | |
</svg> | |
</div> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment