Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(routeprogressbar): add progressbar when loading new chunks #330

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"@dhis2/app-runtime": "^3.8.0",
"react-router-dom": "^6.11.2"
"react-router-dom": "^6.11.2",
"@tanem/react-nprogress": "^5.0.38"
}
}
2 changes: 2 additions & 0 deletions 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 Down Expand Up @@ -47,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;
}
58 changes: 58 additions & 0 deletions src/app/layout/progressbar/RouteProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 { key: locationKey } = useLocation();
const isLoading = navigation.state === LOADING_STATE;
// key is used to reset the state of the progress when location changes.
// Cannot use navigation.location.key directly because navigation.location
// will be undefined when navigation is not in a loading state, causing the animation
// to cancel early because the key will change at the same time as isLoading.
// Once navigation has finished, locationKey will be the same as previous (but now undefined)
// so fallback to that once navigation has finished.
const resetKey = navigation.location?.key || locationKey;

return <RouteProgressBar key={resetKey} 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 }) => {
return (
<div
className={css.progressBar}
style={
{
["--progress"]: `${(-1 + progress) * 100}%`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the -1? Does progress go from 1 to 100? (I tried looking this up in the docs for the package, but didn't find info there 🤷 )

Copy link
Member Author

@Birkbjo Birkbjo May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question - I just followed the example code for the css stuff.
But it seems to be a "trick" because it uses width: 100%. So it actually goes from -100% to 0% . So eg. 0% margin is 100% and -10% is visually 90% of progress.

} 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 @@ -2873,6 +2880,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 @@ -7020,6 +7035,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 @@ -10441,7 +10463,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