Skip to content

Instantly share code, notes, and snippets.

@arshkkk
Created March 9, 2022 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arshkkk/b3555c65441ff170d942c4e5309360a2 to your computer and use it in GitHub Desktop.
Save arshkkk/b3555c65441ff170d942c4e5309360a2 to your computer and use it in GitHub Desktop.
NextJS Router
function MyApp({Component, pageProps}: any) {
return (
<>
<Loader />
<Component {...pageProps} />
</>
);
}
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;
};
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