-
-
Save brandonpittman/d58ab0d8791867b02c50650489d26423 to your computer and use it in GitHub Desktop.
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
function useIsPendingPathname(to) { | |
let { location } = useTransition(); | |
let { pathname } = useResolvedPath(to); | |
return location?.pathname === pathname; | |
} | |
function useIsSlowTransition(ms = 300) { | |
let transition = useTransition(); | |
let [isSlow, setIsSlow] = useState(false); | |
useEffect(() => { | |
if (transition.state === "loading") { | |
let id = setTimeout(() => setIsSlow(true), ms); | |
return () => clearTimeout(id); | |
} else { | |
setIsSlow(false); | |
} | |
}, [transition, ms]); | |
return isSlow; | |
} | |
function PendingLink({ to, style, ...props }) { | |
let isSlow = useIsSlowTransition(300); | |
let isPending = useIsPendingPathname(to); | |
return ( | |
<Link | |
style={{ ...style, opacity: isPending && isSlow ? 0.33 : 1 }} | |
to={to} | |
{...props} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment