elk/composables/page-transiton.ts
2023-02-11 16:20:12 +01:00

29 lines
653 B
TypeScript

export const usePageTransition = (options: {
beforeEach?: typeof noop
afterHydrated?: typeof noop
onTransitionError?: typeof noop
}) => {
const nuxtApp = useNuxtApp()
const router = useRouter()
if (options.beforeEach) {
router.beforeEach(() => {
options.beforeEach?.()
})
}
if (options.onTransitionError) {
router.onError(() => {
options.onTransitionError?.()
})
}
if (options.afterHydrated) {
const nuxtHook = () => {
if (isHydrated.value)
options.afterHydrated?.()
}
nuxtApp.hooks.hook('app:suspense:resolve', nuxtHook)
nuxtApp.hooks.hook('page:finish', nuxtHook)
}
}