2023-07-05 10:39:39 +00:00
|
|
|
import type { Ref, VNode } from 'vue'
|
2023-06-23 10:02:01 +00:00
|
|
|
import { Suspense, Transition, computed, defineComponent, h, inject, mergeProps, nextTick, onMounted, provide, ref, unref } from 'vue'
|
2022-10-24 08:36:49 +00:00
|
|
|
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
2022-03-14 10:47:24 +00:00
|
|
|
import { _wrapIf } from './utils'
|
2023-07-05 10:39:39 +00:00
|
|
|
import { LayoutMetaSymbol, PageRouteSymbol } from './injections'
|
|
|
|
|
2023-02-09 06:26:41 +00:00
|
|
|
import { useRoute } from '#app/composables/router'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2022-10-24 08:36:49 +00:00
|
|
|
import { useRoute as useVueRouterRoute } from '#build/pages'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2021-06-30 16:32:22 +00:00
|
|
|
import layouts from '#build/layouts'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2022-08-23 14:24:20 +00:00
|
|
|
import { appLayoutTransition as defaultLayoutTransition } from '#build/nuxt.config.mjs'
|
2023-06-23 10:02:01 +00:00
|
|
|
import { useNuxtApp } from '#app'
|
2022-01-31 18:58:19 +00:00
|
|
|
|
2021-06-30 16:32:22 +00:00
|
|
|
export default defineComponent({
|
2023-01-14 01:27:38 +00:00
|
|
|
name: 'NuxtLayout',
|
2022-11-29 12:16:41 +00:00
|
|
|
inheritAttrs: false,
|
2021-06-30 16:32:22 +00:00
|
|
|
props: {
|
|
|
|
name: {
|
2022-01-17 18:27:23 +00:00
|
|
|
type: [String, Boolean, Object] as unknown as () => string | false | Ref<string | false>,
|
2022-01-31 18:58:19 +00:00
|
|
|
default: null
|
2021-06-30 16:32:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setup (props, context) {
|
2023-06-23 10:02:01 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
2022-10-08 14:18:57 +00:00
|
|
|
// Need to ensure (if we are not a child of `<NuxtPage>`) that we use synchronous route (not deferred)
|
2023-07-05 10:39:39 +00:00
|
|
|
const injectedRoute = inject(PageRouteSymbol)
|
2022-10-08 14:18:57 +00:00
|
|
|
const route = injectedRoute === useRoute() ? useVueRouterRoute() : injectedRoute
|
2023-06-23 10:02:01 +00:00
|
|
|
|
2022-10-08 14:18:57 +00:00
|
|
|
const layout = computed(() => unref(props.name) ?? route.meta.layout as string ?? 'default')
|
2022-01-31 18:58:19 +00:00
|
|
|
|
2023-06-10 22:17:14 +00:00
|
|
|
const layoutRef = ref()
|
|
|
|
context.expose({ layoutRef })
|
|
|
|
|
2021-06-30 16:32:22 +00:00
|
|
|
return () => {
|
2023-06-23 10:02:01 +00:00
|
|
|
const done = nuxtApp.deferHydration()
|
2022-10-08 14:18:57 +00:00
|
|
|
const hasLayout = layout.value && layout.value in layouts
|
|
|
|
if (process.dev && layout.value && !hasLayout && layout.value !== 'default') {
|
|
|
|
console.warn(`Invalid layout \`${layout.value}\` selected.`)
|
2021-06-30 16:32:22 +00:00
|
|
|
}
|
2022-01-31 18:58:19 +00:00
|
|
|
|
2022-08-23 10:25:48 +00:00
|
|
|
const transitionProps = route.meta.layoutTransition ?? defaultLayoutTransition
|
|
|
|
|
2022-01-31 18:58:19 +00:00
|
|
|
// We avoid rendering layout transition if there is no layout to render
|
2022-08-23 10:25:48 +00:00
|
|
|
return _wrapIf(Transition, hasLayout && transitionProps, {
|
2023-06-23 10:02:01 +00:00
|
|
|
default: () => h(Suspense, { suspensible: true, onResolve: () => { nextTick(done) } }, {
|
2023-07-05 10:39:39 +00:00
|
|
|
// @ts-expect-error seems to be an issue in vue types
|
|
|
|
default: () => h(LayoutProvider, {
|
2023-06-23 10:02:01 +00:00
|
|
|
layoutProps: mergeProps(context.attrs, { ref: layoutRef }),
|
2023-06-10 22:17:14 +00:00
|
|
|
key: layout.value,
|
|
|
|
name: layout.value,
|
2023-06-23 10:02:01 +00:00
|
|
|
shouldProvide: !props.name,
|
|
|
|
hasTransition: !!transitionProps
|
2023-07-05 10:39:39 +00:00
|
|
|
}, context.slots)
|
2023-06-23 10:02:01 +00:00
|
|
|
})
|
2022-08-23 10:25:48 +00:00
|
|
|
}).default()
|
2021-06-30 16:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-06-23 10:02:01 +00:00
|
|
|
|
|
|
|
const LayoutProvider = defineComponent({
|
|
|
|
name: 'NuxtLayoutProvider',
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
|
|
|
name: {
|
2023-07-05 10:39:39 +00:00
|
|
|
type: [String, Boolean]
|
2023-06-23 10:02:01 +00:00
|
|
|
},
|
|
|
|
layoutProps: {
|
|
|
|
type: Object
|
|
|
|
},
|
|
|
|
hasTransition: {
|
|
|
|
type: Boolean
|
|
|
|
},
|
|
|
|
shouldProvide: {
|
|
|
|
type: Boolean
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setup (props, context) {
|
|
|
|
// Prevent reactivity when the page will be rerendered in a different suspense fork
|
2023-07-05 10:39:39 +00:00
|
|
|
// eslint-disable-next-line vue/no-setup-props-destructure
|
|
|
|
const name = props.name
|
2023-06-23 10:02:01 +00:00
|
|
|
if (props.shouldProvide) {
|
|
|
|
provide(LayoutMetaSymbol, {
|
|
|
|
isCurrent: (route: RouteLocationNormalizedLoaded) => name === (route.meta.layout ?? 'default')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-05 10:39:39 +00:00
|
|
|
let vnode: VNode | undefined
|
2023-06-23 10:02:01 +00:00
|
|
|
if (process.dev && process.client) {
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (['#comment', '#text'].includes(vnode?.el?.nodeName)) {
|
2023-07-05 10:39:39 +00:00
|
|
|
if (name) {
|
|
|
|
console.warn(`[nuxt] \`${name}\` layout does not have a single root node and will cause errors when navigating between routes.`)
|
|
|
|
} else {
|
|
|
|
console.warn('[nuxt] `<NuxtLayout>` needs to be passed a single root node in its default slot.')
|
|
|
|
}
|
2023-06-23 10:02:01 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
2023-07-05 10:39:39 +00:00
|
|
|
if (!name || (typeof name === 'string' && !(name in layouts))) {
|
|
|
|
if (process.dev && process.client && props.hasTransition) {
|
|
|
|
vnode = context.slots.default?.() as VNode | undefined
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
return context.slots.default?.()
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:02:01 +00:00
|
|
|
if (process.dev && process.client && props.hasTransition) {
|
2023-07-05 10:39:39 +00:00
|
|
|
vnode = h(layouts[name], props.layoutProps, context.slots)
|
2023-06-23 10:02:01 +00:00
|
|
|
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
|
2023-07-05 10:39:39 +00:00
|
|
|
return h(layouts[name], props.layoutProps, context.slots)
|
2023-06-23 10:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|