-
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.
refactored approach to single Transition wrapper
- Loading branch information
Showing
6 changed files
with
92 additions
and
89 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Link from "./link"; | ||
import Link from "next/link"; | ||
|
||
export default function Nav() { | ||
return ( | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,87 @@ | ||
"use client"; | ||
|
||
import { AnimatePresence, motion } from "framer-motion"; | ||
import { useRouter } from "next/navigation"; | ||
import { | ||
createContext, | ||
MouseEventHandler, | ||
PropsWithChildren, | ||
use, | ||
useTransition, | ||
} from "react"; | ||
|
||
export const DELAY = 1000; | ||
|
||
const sleep = (ms: number) => | ||
new Promise<void>((resolve) => setTimeout(() => resolve(), ms)); | ||
const noop = () => {}; | ||
|
||
type TransitionContext = { | ||
pending: boolean; | ||
navigate: (url: string) => void; | ||
}; | ||
const Context = createContext<TransitionContext>({ | ||
pending: false, | ||
navigate: noop, | ||
}); | ||
export const usePageTransition = () => use(Context); | ||
export const usePageTransitionHandler = () => { | ||
const { navigate } = usePageTransition(); | ||
const onClick: MouseEventHandler<HTMLAnchorElement> = (e) => { | ||
e.preventDefault(); | ||
const href = e.currentTarget.getAttribute("href"); | ||
if (href) navigate(href); | ||
}; | ||
|
||
return onClick; | ||
}; | ||
|
||
type Props = PropsWithChildren<{ | ||
className?: string; | ||
}>; | ||
|
||
export default function Transitions({ children, className }: Props) { | ||
const [pending, start] = useTransition(); | ||
const router = useRouter(); | ||
const navigate = (href: string) => { | ||
start(async () => { | ||
router.push(href); | ||
await sleep(DELAY); | ||
}); | ||
}; | ||
|
||
const onClick: MouseEventHandler<HTMLDivElement> = (e) => { | ||
const a = (e.target as Element).closest("a"); | ||
if (a) { | ||
e.preventDefault(); | ||
const href = a.getAttribute("href"); | ||
if (href) navigate(href); | ||
} | ||
}; | ||
|
||
return ( | ||
<Context.Provider value={{ pending, navigate }}> | ||
<div onClickCapture={onClick} className={className}> | ||
{children} | ||
</div> | ||
</Context.Provider> | ||
); | ||
} | ||
|
||
export function Animate({ children, className }: Props) { | ||
const { pending } = usePageTransition(); | ||
return ( | ||
<AnimatePresence> | ||
{!pending && ( | ||
<motion.div | ||
initial={{ opacity: 0 }} | ||
animate={{ opacity: 1 }} | ||
exit={{ opacity: 0 }} | ||
className={className} | ||
> | ||
{children} | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
); | ||
} |