Skip to content

Commit

Permalink
fix: show loading state on route changing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo committed May 9, 2023
1 parent 62baae0 commit 0f6631d
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion apps/client/src/components/Container/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useContext } from "react";
import { useContext, useEffect, useState } from "react";
import Router from "next/router";

import { Backdrop } from "@app/components/Backdrop";
import { Main } from "@app/components/Container/Main";
import { FullPageLoader } from "@app/components/FullPageLoader";
import { Header } from "@app/components/Header";
import { Sidebar } from "@app/components/Sidebar";
import { SidebarContext, SidebarProvider } from "@app/context/SidebarContext";
Expand All @@ -15,9 +18,30 @@ export function Layout({ children }: ILayout) {
useSignOut();
const { isSidebarOpen } = useContext(SidebarContext);
const { theme } = useTheme();
const [isChangingRoute, setIsChangingRoute] = useState(false);

useEffect(() => {
const start = () => setIsChangingRoute(true);
const done = () => setIsChangingRoute(false);

Router.events.on("routeChangeStart", start);
Router.events.on("routeChangeComplete", done);
Router.events.on("routeChangeError", done);

return () => {
Router.events.off("routeChangeStart", start);
Router.events.off("routeChangeComplete", done);
Router.events.off("routeChangeError", done);
};
}, []);

return (
<SidebarProvider>
{isChangingRoute ? (
<Backdrop className="z-[100] transition-all duration-150">
<FullPageLoader />
</Backdrop>
) : null}
<div
className={`h-screen ${theme} ${isSidebarOpen && "overflow-hidden"}`}
>
Expand Down

0 comments on commit 0f6631d

Please sign in to comment.