-
Notifications
You must be signed in to change notification settings - Fork 0
Fade transitions between pages #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
08766f5
9b7a2a9
9004f2a
abad9a8
6561f7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.container { | ||
height: 100%; | ||
} | ||
|
||
.nav { | ||
display: flex; | ||
gap: 8px; | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,8 @@ | ||||||||||||||||||||||
import { useEffect } from "react" | ||||||||||||||||||||||
import { useNavigate, usePath } from "@/lib/router" | ||||||||||||||||||||||
import { useTransition, animated } from "@react-spring/web" | ||||||||||||||||||||||
import { useNavigate, usePath, usePreviousPath } from "@/lib/router" | ||||||||||||||||||||||
import { useModal } from "./ModalContext" | ||||||||||||||||||||||
import { useAddress } from "../data/hooks" | ||||||||||||||||||||||
import Connect from "@/pages/connect/Connect" | ||||||||||||||||||||||
import Home from "@/pages/wallet/tabs/Home" | ||||||||||||||||||||||
import Send from "@/pages/wallet/txs/send/Send" | ||||||||||||||||||||||
|
@@ -12,71 +15,95 @@ import Withdrawals from "@/pages/bridge/op/Withdrawals" | |||||||||||||||||||||
import BridgePreview from "@/pages/bridge/BridgePreview" | ||||||||||||||||||||||
import BridgeHistory from "@/pages/bridge/BridgeHistory" | ||||||||||||||||||||||
import TxRequest from "@/pages/tx/TxRequest" | ||||||||||||||||||||||
import { useAddress } from "../data/hooks" | ||||||||||||||||||||||
import { useModal } from "./ModalContext" | ||||||||||||||||||||||
|
||||||||||||||||||||||
const routes = [ | ||||||||||||||||||||||
{ path: "/connect", Component: Connect }, | ||||||||||||||||||||||
{ path: "/", Component: Home }, | ||||||||||||||||||||||
{ path: "/send", Component: Send, rerender: true }, | ||||||||||||||||||||||
{ path: "/collection", Component: CollectionDetails }, | ||||||||||||||||||||||
{ path: "/nft", Component: NftDetails }, | ||||||||||||||||||||||
{ path: "/nft/send", Component: SendNft, rerender: true }, | ||||||||||||||||||||||
{ path: "/rollups", Component: ManageChains }, | ||||||||||||||||||||||
{ path: "/bridge", Component: BridgeForm, renderWithoutAddress: true, rerender: true }, | ||||||||||||||||||||||
{ path: "/bridge/preview", Component: BridgePreview }, | ||||||||||||||||||||||
{ path: "/bridge/history", Component: BridgeHistory, renderWithoutAddress: true }, | ||||||||||||||||||||||
{ path: "/op/withdrawals", Component: Withdrawals }, | ||||||||||||||||||||||
{ path: "/tx", Component: TxRequest }, | ||||||||||||||||||||||
] | ||||||||||||||||||||||
|
||||||||||||||||||||||
const Routes = () => { | ||||||||||||||||||||||
const rawPath = usePath() | ||||||||||||||||||||||
const rawPrevPath = usePreviousPath() | ||||||||||||||||||||||
const navigate = useNavigate() | ||||||||||||||||||||||
const path = usePath() | ||||||||||||||||||||||
const address = useAddress() | ||||||||||||||||||||||
const { closeModal } = useModal() | ||||||||||||||||||||||
|
||||||||||||||||||||||
const path = ["/nfts", "/activity"].includes(rawPath) ? "/" : rawPath | ||||||||||||||||||||||
const prevPath = ["/nfts", "/activity"].includes(rawPrevPath || "") ? "/" : rawPrevPath | ||||||||||||||||||||||
|
||||||||||||||||||||||
// whenever address changes, navigate to the appropriate path | ||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||
closeModal() | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (path.startsWith("/bridge/")) { | ||||||||||||||||||||||
navigate("/bridge") | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (path === "/collection" || path.startsWith("/nft")) { | ||||||||||||||||||||||
navigate("/nfts") | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Run only on address changes, preventing navigation from triggering on path updates. | ||||||||||||||||||||||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||||||||||||||||||||||
}, [address]) | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (path === "/connect") { | ||||||||||||||||||||||
if (address) return null | ||||||||||||||||||||||
return <Connect /> | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// Compute transition direction | ||||||||||||||||||||||
const currentIndex = routes.findIndex((r) => r.path === path) | ||||||||||||||||||||||
const prevIndex = routes.findIndex((r) => r.path === prevPath) | ||||||||||||||||||||||
const direction = currentIndex >= prevIndex ? 1 : -1 | ||||||||||||||||||||||
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle edge cases when routes are not found in the array. The // Compute transition direction
const currentIndex = routes.findIndex((r) => r.path === path)
const prevIndex = routes.findIndex((r) => r.path === prevPath)
- const direction = currentIndex >= prevIndex ? 1 : -1
+ const direction = currentIndex === -1 || prevIndex === -1 ? 1 : (currentIndex >= prevIndex ? 1 : -1) 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||
|
||||||||||||||||||||||
const transitions = useTransition(path, { | ||||||||||||||||||||||
from: | ||||||||||||||||||||||
direction > 0 | ||||||||||||||||||||||
? { opacity: 1, transform: `translateX(100%)` } | ||||||||||||||||||||||
: { opacity: 0, transform: `translateX(0%)` }, | ||||||||||||||||||||||
enter: { opacity: 1, transform: "translateX(0%)" }, | ||||||||||||||||||||||
leave: | ||||||||||||||||||||||
direction < 0 | ||||||||||||||||||||||
? { opacity: 1, transform: `translateX(100%)` } | ||||||||||||||||||||||
: { opacity: 0, transform: `translateX(0%)` }, | ||||||||||||||||||||||
immediate: !prevPath, | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (path === "/connect" && !address) return <Connect /> | ||||||||||||||||||||||
|
||||||||||||||||||||||
// FIXME: do we need to block all routes when address is not connected? | ||||||||||||||||||||||
if (!address) return null | ||||||||||||||||||||||
|
||||||||||||||||||||||
switch (path) { | ||||||||||||||||||||||
case "/bridge": | ||||||||||||||||||||||
return <BridgeForm key={address} /> | ||||||||||||||||||||||
case "/bridge/history": | ||||||||||||||||||||||
return <BridgeHistory /> | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return ( | ||||||||||||||||||||||
<div style={{ position: "relative", height: "100%", overflow: "hidden" }}> | ||||||||||||||||||||||
{transitions((style, animatedPath) => { | ||||||||||||||||||||||
const route = routes.find((r) => r.path === animatedPath) | ||||||||||||||||||||||
if (!route) return null | ||||||||||||||||||||||
if (!address && !route.renderWithoutAddress) return null | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (!address) { | ||||||||||||||||||||||
return null | ||||||||||||||||||||||
} | ||||||||||||||||||||||
const { Component, rerender } = route | ||||||||||||||||||||||
|
||||||||||||||||||||||
switch (path) { | ||||||||||||||||||||||
case "/": | ||||||||||||||||||||||
case "/nfts": | ||||||||||||||||||||||
case "/activity": | ||||||||||||||||||||||
return <Home /> | ||||||||||||||||||||||
case "/send": | ||||||||||||||||||||||
return <Send key={address} /> | ||||||||||||||||||||||
case "/collection": | ||||||||||||||||||||||
return <CollectionDetails /> | ||||||||||||||||||||||
case "/nft": | ||||||||||||||||||||||
return <NftDetails /> | ||||||||||||||||||||||
case "/nft/send": | ||||||||||||||||||||||
return <SendNft key={address} /> | ||||||||||||||||||||||
case "/rollups": | ||||||||||||||||||||||
return <ManageChains /> | ||||||||||||||||||||||
case "/bridge/preview": | ||||||||||||||||||||||
return <BridgePreview /> | ||||||||||||||||||||||
case "/op/withdrawals": | ||||||||||||||||||||||
return <Withdrawals /> | ||||||||||||||||||||||
case "/tx": | ||||||||||||||||||||||
return <TxRequest /> | ||||||||||||||||||||||
case "/blank": | ||||||||||||||||||||||
return null | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return ( | ||||||||||||||||||||||
<animated.div | ||||||||||||||||||||||
key={animatedPath} | ||||||||||||||||||||||
style={{ | ||||||||||||||||||||||
...style, | ||||||||||||||||||||||
position: "absolute", | ||||||||||||||||||||||
width: "100%", | ||||||||||||||||||||||
height: "100%", | ||||||||||||||||||||||
backgroundColor: "var(--bg)", | ||||||||||||||||||||||
}} | ||||||||||||||||||||||
> | ||||||||||||||||||||||
{rerender ? <Component key={address} /> : <Component />} | ||||||||||||||||||||||
</animated.div> | ||||||||||||||||||||||
) | ||||||||||||||||||||||
})} | ||||||||||||||||||||||
</div> | ||||||||||||||||||||||
) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export default Routes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle edge case when tab indices are not found.
The direction calculation doesn't handle the case where
findIndex
returns -1 (when a path is not found in the tabs array). This could lead to incorrect animation direction.📝 Committable suggestion
🤖 Prompt for AI Agents