-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add global loading indicator to top nav
- Loading branch information
1 parent
f54dbcd
commit dad1da4
Showing
2 changed files
with
44 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as React from "react"; | ||
import { useNavigation } from "@remix-run/react"; | ||
import cx from "clsx"; | ||
|
||
export function GlobalLoading() { | ||
const transition = useNavigation(); | ||
const active = transition.state !== "idle"; | ||
|
||
const ref = React.useRef<HTMLDivElement>(null); | ||
const [animationComplete, setAnimationComplete] = React.useState(true); | ||
|
||
React.useEffect(() => { | ||
if (!ref.current) return; | ||
if (active) setAnimationComplete(false); | ||
|
||
Promise.allSettled( | ||
ref.current.getAnimations().map(({ finished }) => finished), | ||
).then(() => !active && setAnimationComplete(true)); | ||
}, [active]); | ||
|
||
return ( | ||
<div | ||
role="progressbar" | ||
aria-hidden={!active} | ||
aria-valuetext={active ? "Loading" : undefined} | ||
className="fixed inset-x-0 left-0 top-0 z-50 h-1 animate-pulse" | ||
> | ||
<div | ||
ref={ref} | ||
className={cx( | ||
"h-full bg-gradient-to-r from-blue-brand to-aqua-brand transition-all duration-500 ease-in-out", | ||
transition.state === "idle" && | ||
animationComplete && | ||
"w-0 opacity-0 transition-none", | ||
transition.state === "submitting" && "w-4/12", | ||
transition.state === "loading" && "w-10/12", | ||
transition.state === "idle" && !animationComplete && "w-full", | ||
)} | ||
/> | ||
</div> | ||
); | ||
} |