2022-09-26 09:12:20 +00:00
|
|
|
import { defineComponent, unref, nextTick, onMounted, Ref, Transition, VNode } from 'vue'
|
2022-03-14 10:47:24 +00:00
|
|
|
import { _wrapIf } from './utils'
|
|
|
|
import { useRoute } from '#app'
|
2021-10-21 19:16:52 +00:00
|
|
|
// @ts-ignore
|
2021-06-30 16:32:22 +00:00
|
|
|
import layouts from '#build/layouts'
|
2022-08-23 14:24:20 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import { appLayoutTransition as defaultLayoutTransition } from '#build/nuxt.config.mjs'
|
2022-01-31 18:58:19 +00:00
|
|
|
|
2021-06-30 16:32:22 +00:00
|
|
|
export default defineComponent({
|
|
|
|
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) {
|
2022-01-31 18:58:19 +00:00
|
|
|
const route = useRoute()
|
|
|
|
|
2022-08-23 10:25:48 +00:00
|
|
|
let vnode: VNode
|
|
|
|
let _layout: string | false
|
|
|
|
if (process.dev && process.client) {
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
2022-09-22 13:54:55 +00:00
|
|
|
if (_layout && _layout in layouts && ['#comment', '#text'].includes(vnode?.el?.nodeName)) {
|
2022-08-23 10:25:48 +00:00
|
|
|
console.warn(`[nuxt] \`${_layout}\` layout does not have a single root node and will cause errors when navigating between routes.`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:32:22 +00:00
|
|
|
return () => {
|
2022-09-26 09:12:20 +00:00
|
|
|
const layout = unref(props.name) ?? route.meta.layout as string ?? 'default'
|
2022-01-31 18:58:19 +00:00
|
|
|
|
|
|
|
const hasLayout = layout && layout in layouts
|
|
|
|
if (process.dev && layout && !hasLayout && layout !== 'default') {
|
|
|
|
console.warn(`Invalid layout \`${layout}\` 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, {
|
|
|
|
default: () => {
|
|
|
|
if (process.dev && process.client && transitionProps) {
|
|
|
|
_layout = layout
|
|
|
|
vnode = _wrapIf(layouts[layout], hasLayout, context.slots).default()
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
|
|
|
|
return _wrapIf(layouts[layout], hasLayout, context.slots).default()
|
|
|
|
}
|
|
|
|
}).default()
|
2021-06-30 16:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|