-
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 (#176)
* feat: Add global loading indicator to top nav * chore: Apply some of Brook's suggestions to global-loading code
- Loading branch information
1 parent
d22a79f
commit f0c65a2
Showing
2 changed files
with
48 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,46 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useNavigation } from "@remix-run/react"; | ||
import cx from "clsx"; | ||
|
||
export function GlobalLoading() { | ||
let transition = useNavigation(); | ||
let active = transition.state !== "idle"; | ||
|
||
let ref = useRef<HTMLDivElement>(null); | ||
let [animating, setAnimating] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!ref.current) return; | ||
|
||
Promise.allSettled( | ||
ref.current.getAnimations().map(({ finished }) => finished), | ||
).then(() => { | ||
if (!active) setAnimating(false); | ||
}); | ||
|
||
if (active) { | ||
let id = setTimeout(() => setAnimating(true), 100); | ||
return () => clearTimeout(id); | ||
} | ||
}, [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" && | ||
(animating ? "w-full" : "w-0 opacity-0 transition-none"), | ||
transition.state === "submitting" && "w-4/12", | ||
transition.state === "loading" && "w-10/12", | ||
)} | ||
/> | ||
</div> | ||
); | ||
} |