-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from davelsan/feat-use-router
Extend useRouter to also use transitions
- Loading branch information
Showing
4 changed files
with
141 additions
and
25 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,66 @@ | ||
import { useRouter as useNextRouter } from 'next/navigation' | ||
import {startTransition, useCallback, useMemo} from "react"; | ||
import { useSetFinishViewTransition } from "./transition-context"; | ||
import { | ||
AppRouterInstance, | ||
NavigateOptions | ||
} from "next/dist/shared/lib/app-router-context.shared-runtime"; | ||
|
||
export type TransitionOptions = { | ||
onTransitionReady?: () => void; | ||
}; | ||
|
||
type NavigateOptionsWithTransition = NavigateOptions & TransitionOptions; | ||
|
||
export type TransitionRouter = AppRouterInstance & { | ||
push: (href: string, options?: NavigateOptionsWithTransition) => void; | ||
replace: (href: string, options?: NavigateOptionsWithTransition) => void; | ||
}; | ||
|
||
export function useTransitionRouter() { | ||
const router = useNextRouter() | ||
const finishViewTransition = useSetFinishViewTransition() | ||
|
||
const triggerTransition = useCallback((cb: () => void, { onTransitionReady }: TransitionOptions = {}) => { | ||
// @ts-ignore | ||
const transition = document.startViewTransition( | ||
() => | ||
new Promise<void>((resolve) => { | ||
startTransition(() => { | ||
cb(); | ||
finishViewTransition(() => resolve) | ||
}) | ||
}) | ||
) | ||
|
||
if (onTransitionReady) { | ||
transition.ready.then(onTransitionReady); | ||
} | ||
}, []) | ||
|
||
const push = useCallback(( | ||
href: string, | ||
{ onTransitionReady, ...options }: NavigateOptionsWithTransition = {} | ||
) => { | ||
triggerTransition(() => router.push(href, options), { | ||
onTransitionReady | ||
}) | ||
}, [triggerTransition, router]) | ||
|
||
const replace = useCallback(( | ||
href: string, | ||
{ onTransitionReady, ...options }: NavigateOptionsWithTransition = {} | ||
) => { | ||
triggerTransition(() => router.replace(href, options), { | ||
onTransitionReady | ||
}); | ||
}, [triggerTransition, router]); | ||
|
||
return useMemo<TransitionRouter>( | ||
() => ({ | ||
...router, | ||
push, | ||
replace, | ||
}), | ||
[push, replace, router]); | ||
} |