-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
55 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
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,80 @@ | ||
'use client' | ||
|
||
import { AnimatePresence, motion } from 'framer-motion' | ||
import { useRouter } from 'next-nprogress-bar' | ||
import { | ||
createContext, | ||
MouseEventHandler, | ||
PropsWithChildren, | ||
use, | ||
useTransition, | ||
} from 'react' | ||
|
||
export const DELAY = 200; | ||
|
||
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); | ||
|
||
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> | ||
); | ||
} |
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