Skip to content

Commit

Permalink
feat(routeprogressbar): implement progressbar when loading chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed May 19, 2023
1 parent fb5ebe6 commit afa8602
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@dhis2/app-runtime": "^3.8.0",
"@tanem/react-nprogress": "^5.0.38",
"react-router-dom": "^6.10.0"
}
}
8 changes: 7 additions & 1 deletion src/app/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Outlet, useMatches } from "react-router-dom";
import { MatchRouteHandle } from "../routes/types";
import { Sidebar } from "../sidebar";
import css from "./Layout.module.css";
import { RouteProgress } from "./progressbar/RouteProgressBar";

interface BaseLayoutProps {
children: React.ReactNode;
Expand All @@ -28,7 +29,11 @@ export const SidebarLayout = ({
}) => {
return (
<BaseSidebarLayout
sidebar={<Sidebar className={cx(css.sidebar, {[css.hide]: hideSidebar})} />}
sidebar={
<Sidebar
className={cx(css.sidebar, { [css.hide]: hideSidebar })}
/>
}
>
{children}
</BaseSidebarLayout>
Expand All @@ -43,6 +48,7 @@ export const Layout = () => {

return (
<SidebarLayout hideSidebar={hideSidebar}>
<RouteProgress />
<Outlet />
</SidebarLayout>
);
Expand Down
23 changes: 23 additions & 0 deletions src/app/layout/progressbar/RouteProgressBar.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.progressBarWrapper {
--animationDuration: 200ms;
transition: opacity var(--animationDuration) linear;
pointer-events: none;
opacity: 1;
}

.progressBarWrapper.finished {
opacity: 0;
}

.progressBar {
--progress: 0%;
margin-left: var(--progress);
transition: margin-left var(--animationDuration) linear;
background: #29d;
height: 2px;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 1000;
}
63 changes: 63 additions & 0 deletions src/app/layout/progressbar/RouteProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useNProgress } from "@tanem/react-nprogress";
import cx from "classnames";
import React from "react";
import { useNavigation, useLocation } from "react-router-dom";
import css from "./RouteProgressBar.module.css";

const LOADING_STATE = "loading";
const ANIMATION_DURATION = 200;

export const RouteProgress = () => {
const navigation = useNavigation();
const location = useLocation();
const isLoading = navigation.state === LOADING_STATE;
// key is used to reset the state of the progress when location changes
const [keyIncrement, setKeyIncrement] = React.useState(0);

React.useEffect(() => {
// use timeout so animation has a chance to complete before resetting
const timeout = setTimeout(
() => setKeyIncrement((inc) => inc + 1),
ANIMATION_DURATION
);
() => clearTimeout(timeout);
}, [location.key]);

return <RouteProgressBar key={keyIncrement} isLoading={isLoading} />;
};

export const RouteProgressBar = ({ isLoading }) => {
const { isFinished, progress } = useNProgress({
animationDuration: ANIMATION_DURATION,
isAnimating: isLoading,
});

return (
<div
className={cx(css.progressBarWrapper, {
[css.finished]: isFinished,
})}
style={
{
["--animationDuration"]: `${ANIMATION_DURATION}ms`,
} as React.CSSProperties
}
>
<ProgressBar progress={progress} />
</div>
);
};

const ProgressBar = ({ progress }: { progress: number }) => {
console.log({ progress });
return (
<div
className={css.progressBar}
style={
{
["--progress"]: `${(-1 + progress) * 100}%`,
} as React.CSSProperties
}
></div>
);
};
24 changes: 23 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,13 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.21.5":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200"
integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==
dependencies:
regenerator-runtime "^0.13.11"

"@babel/template@^7.18.10", "@babel/template@^7.20.7", "@babel/template@^7.3.3":
version "7.20.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8"
Expand Down Expand Up @@ -2842,6 +2849,14 @@
dependencies:
defer-to-connect "^1.0.1"

"@tanem/react-nprogress@^5.0.38":
version "5.0.38"
resolved "https://registry.yarnpkg.com/@tanem/react-nprogress/-/react-nprogress-5.0.38.tgz#348ced46a86a7d5bdc3f42afce394a2a51d74923"
integrity sha512-iyUzLfqiXa/EZ558mV8Wp/iqy7lKT9K+6sy5SF76sYD7mgZQ67VA8nUnsQXKFdEXoMdmfIUj7dbyDHN2OOI6BQ==
dependencies:
"@babel/runtime" "^7.21.5"
hoist-non-react-statics "^3.3.2"

"@testing-library/dom@^8.0.0":
version "8.20.0"
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.20.0.tgz#914aa862cef0f5e89b98cc48e3445c4c921010f6"
Expand Down Expand Up @@ -6926,6 +6941,13 @@ he@^1.2.0:
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==

hoist-non-react-statics@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
dependencies:
react-is "^16.7.0"

hoopy@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/hoopy/-/hoopy-0.1.4.tgz#609207d661100033a9a9402ad3dea677381c1b1d"
Expand Down Expand Up @@ -10281,7 +10303,7 @@ react-final-form@^6.5.3:
dependencies:
"@babel/runtime" "^7.15.4"

react-is@^16.13.1:
react-is@^16.13.1, react-is@^16.7.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
Expand Down

0 comments on commit afa8602

Please sign in to comment.