-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a half decent top loading bar
Before we were using a full screen opacity change and that's proving disruptive to the user experience. I think it's better to simply show the loader on the top after ~500 milliseconds of waiting for navigation to go idle
- Loading branch information
Showing
5 changed files
with
47 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useNavigation } from "@remix-run/react"; | ||
import { useEffect, useRef } from "react"; | ||
import LoadingBar, { LoadingBarRef } from "react-top-loading-bar"; | ||
|
||
export function GlobalLoadingBar() { | ||
const navigation = useNavigation(); | ||
|
||
const loadingBarRef = useRef<LoadingBarRef>(null); | ||
const loaderStarted = useRef(false); | ||
|
||
useEffect(() => { | ||
let timeout: ReturnType<typeof setTimeout> | undefined; | ||
if (navigation.state !== "idle") { | ||
timeout = setTimeout(() => { | ||
if (!loadingBarRef.current) return; | ||
loaderStarted.current = true; | ||
loadingBarRef.current.continuousStart(); | ||
}, 400); | ||
} else if (loaderStarted.current == true) { | ||
loaderStarted.current = false; | ||
loadingBarRef.current?.complete(); | ||
} | ||
return () => { | ||
if (timeout) clearTimeout(timeout); | ||
}; | ||
}, [navigation.state]); | ||
return <LoadingBar ref={loadingBarRef} />; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.