mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-02 02:17:15 +00:00
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { useRouter } from '#app/composables/router'
|
|
import { defineNuxtPlugin } from '#app/nuxt'
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
if (!document.startViewTransition) { return }
|
|
|
|
let finishTransition: undefined | (() => void)
|
|
let abortTransition: undefined | (() => void)
|
|
|
|
const router = useRouter()
|
|
|
|
router.beforeResolve((to) => {
|
|
if (to.meta.pageTransition === false) { return }
|
|
|
|
const promise = new Promise<void>((resolve, reject) => {
|
|
finishTransition = resolve
|
|
abortTransition = reject
|
|
})
|
|
|
|
let changeRoute: () => void
|
|
const ready = new Promise<void>(resolve => (changeRoute = resolve))
|
|
|
|
const transition = document.startViewTransition!(() => {
|
|
changeRoute()
|
|
return promise
|
|
})
|
|
|
|
transition.finished.then(() => {
|
|
abortTransition = undefined
|
|
finishTransition = undefined
|
|
})
|
|
|
|
return ready
|
|
})
|
|
|
|
nuxtApp.hook('vue:error', () => {
|
|
abortTransition?.()
|
|
abortTransition = undefined
|
|
})
|
|
|
|
nuxtApp.hook('page:finish', () => {
|
|
finishTransition?.()
|
|
finishTransition = undefined
|
|
})
|
|
})
|
|
|
|
declare global {
|
|
interface Document {
|
|
startViewTransition?: (callback: () => Promise<void> | void) => {
|
|
finished: Promise<void>
|
|
updateCallbackDone: Promise<void>
|
|
ready: Promise<void>
|
|
}
|
|
}
|
|
}
|