-
Notifications
You must be signed in to change notification settings - Fork 2
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
Birkbjo
wants to merge
7
commits into
master
Choose a base branch
from
feat/routeprogressbar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−2
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
afa8602
feat(routeprogressbar): implement progressbar when loading chunks
Birkbjo 3e80c2a
docs(routeprogressbar): clarify key usage
Birkbjo ff260f2
refactor: simplify key reset
Birkbjo d03e70f
refactor(routeprogress): some cleanup
Birkbjo b2a809d
refactor(routeprogress): further simplify resetkey
Birkbjo c1a035e
docs: cleanup comment
Birkbjo e2ac444
Merge branch 'master' into feat/routeprogressbar
Birkbjo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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; | ||
} |
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
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}%`, | ||
} as React.CSSProperties | ||
} | ||
></div> | ||
); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🤷 )
There was a problem hiding this comment.
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%
to0%
. So eg.0%
margin is 100% and-10%
is visually 90% of progress.