Skip to content

Commit

Permalink
Merge pull request #31 from Shadowh4k/fix/router-transition
Browse files Browse the repository at this point in the history
fix when using router if the browser doesn't support view transitions
  • Loading branch information
shuding authored Sep 22, 2024
2 parents c2ddb1e + 686ea76 commit 6195aed
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/use-transition-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ export function useTransitionRouter() {
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 ('startViewTransition' in document) {
// @ts-ignore
const transition = document.startViewTransition(
() =>
new Promise<void>((resolve) => {
startTransition(() => {
cb();
finishViewTransition(() => resolve)
})
})
})
)
)

if (onTransitionReady) {
transition.ready.then(onTransitionReady);
if (onTransitionReady) {
transition.ready.then(onTransitionReady);
}
}
else {
return cb()
}
}, [])

const push = useCallback((
Expand Down

0 comments on commit 6195aed

Please sign in to comment.