Skip to content

Commit

Permalink
feat(link): add view-transition prop
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Sep 11, 2024
1 parent be68148 commit bd2dd8c
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/router/src/RouterLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export interface RouterLinkProps extends RouterLinkOptions {
| 'time'
| 'true'
| 'false'

/**
* Pass the returned promise of `router.push()` to `document.startViewTransition()` if supported.
*/
viewTransition?: boolean
}

/**
Expand All @@ -106,7 +111,13 @@ export interface UseLinkOptions<Name extends keyof RouteMap = keyof RouteMap> {
| RouteLocationAsPath
| RouteLocationRaw
>

replace?: MaybeRef<boolean | undefined>

/**
* Pass the returned promise of `router.push()` to `document.startViewTransition()` if supported.
*/
viewTransition?: boolean
}

/**
Expand Down Expand Up @@ -214,10 +225,19 @@ export function useLink<Name extends keyof RouteMap = keyof RouteMap>(
e: MouseEvent = {} as MouseEvent
): Promise<void | NavigationFailure> {
if (guardEvent(e)) {
return router[unref(props.replace) ? 'replace' : 'push'](
const p = router[unref(props.replace) ? 'replace' : 'push'](
unref(props.to)
// avoid uncaught errors are they are logged anyway
).catch(noop)
if (
props.viewTransition &&
typeof document !== 'undefined' &&
'startViewTransition' in document
) {
// @ts-expect-error: not added to types yet
document.startViewTransition(() => p)
}
return p
}
return Promise.resolve()
}
Expand Down

0 comments on commit bd2dd8c

Please sign in to comment.