Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): add page:view-transition:start hook #26045

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/3.api/6.advanced/1.hooks.md
Expand Up @@ -28,6 +28,7 @@ Hook | Arguments | Environment | Description
`page:loading:start` | - | Client | Called when the `setup()` of the new page is running.
`page:loading:end` | - | Client | Called after `page:finish`
`page:transition:finish`| `pageComponent?` | Client | After page transition [onAfterLeave](https://vuejs.org/guide/built-ins/transition.html#javascript-hooks) event.
`page:view-transition:start` | `transition` | Client | Called after document.startViewTransition is called by the [experimental nuxt plugin](https://nuxt.com/docs/getting-started/transitions#view-transitions-api-experimental).
danielroe marked this conversation as resolved.
Show resolved Hide resolved

## Nuxt Hooks (build time)

Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt/src/app/nuxt.ts
Expand Up @@ -18,6 +18,7 @@ import type { NuxtError } from '../app/composables/error'
import type { AsyncDataRequestStatus } from '../app/composables/asyncData'
import type { NuxtAppManifestMeta } from '../app/composables/manifest'
import type { LoadingIndicator } from '../app/composables/loading-indicator'
import type { ViewTransition } from './plugins/view-transitions.client'

import type { NuxtAppLiterals } from '#app'

Expand Down Expand Up @@ -45,6 +46,7 @@ export interface RuntimeNuxtHooks {
'page:finish': (Component?: VNode) => HookResult
'page:transition:start': () => HookResult
'page:transition:finish': (Component?: VNode) => HookResult
'page:view-transition:start': (transition: ViewTransition) => HookResult
horvbalint marked this conversation as resolved.
Show resolved Hide resolved
'page:loading:start': () => HookResult
'page:loading:end': () => HookResult
'vue:setup': () => void
Expand Down
15 changes: 9 additions & 6 deletions packages/nuxt/src/app/plugins/view-transitions.client.ts
Expand Up @@ -14,7 +14,7 @@ export default defineNuxtPlugin((nuxtApp) => {

const router = useRouter()

router.beforeResolve((to, from) => {
router.beforeResolve(async (to, from) => {
const viewTransitionMode = to.meta.viewTransition ?? defaultViewTransition
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
const prefersNoTransition = prefersReducedMotion && viewTransitionMode !== 'always'
Expand All @@ -35,6 +35,7 @@ export default defineNuxtPlugin((nuxtApp) => {
changeRoute()
return promise
})
await nuxtApp.callHook('page:view-transition:start', transition)

transition.finished.then(() => {
abortTransition = undefined
Expand All @@ -55,12 +56,14 @@ export default defineNuxtPlugin((nuxtApp) => {
})
})

export interface ViewTransition {
ready: Promise<void>
finished: Promise<void>
updateCallbackDone: Promise<void>
}

declare global {
interface Document {
startViewTransition?: (callback: () => Promise<void> | void) => {
finished: Promise<void>
updateCallbackDone: Promise<void>
ready: Promise<void>
}
startViewTransition?: (callback: () => Promise<void> | void) => ViewTransition
}
}