-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routeprogressbar): implement progressbar when loading chunks
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
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,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> | ||
); | ||
}; |
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