Nuxt/packages/nuxt/src/app/components/layout.ts

56 lines
1.8 KiB
TypeScript
Raw Normal View History

import { defineComponent, isRef, nextTick, onMounted, Ref, Transition, VNode } from 'vue'
import { _wrapIf } from './utils'
import { useRoute } from '#app'
// @ts-ignore
2021-06-30 16:32:22 +00:00
import layouts from '#build/layouts'
const defaultLayoutTransition = { name: 'layout', mode: 'out-in' }
2021-06-30 16:32:22 +00:00
export default defineComponent({
props: {
name: {
type: [String, Boolean, Object] as unknown as () => string | false | Ref<string | false>,
default: null
2021-06-30 16:32:22 +00:00
}
},
setup (props, context) {
const route = useRoute()
let vnode: VNode
let _layout: string | false
if (process.dev && process.client) {
onMounted(() => {
nextTick(() => {
if (_layout && ['#comment', '#text'].includes(vnode?.el?.nodeName)) {
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 () => {
const layout = (isRef(props.name) ? props.name.value : props.name) ?? route.meta.layout as string ?? 'default'
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
}
const transitionProps = route.meta.layoutTransition ?? defaultLayoutTransition
// We avoid rendering layout transition if there is no layout to render
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
}
}
})