40 lines
1.0 KiB
TypeScript
Raw Normal View History

import { ComponentInternalInstance, Slots } from 'vue'
2020-06-26 18:09:47 +03:00
import { Props, PushFn, renderVNodeChildren } from '../render'
2020-02-05 21:04:40 -05:00
export type SSRSlots = Record<string, SSRSlot>
export type SSRSlot = (
props: Props,
push: PushFn,
2020-02-06 17:45:34 -05:00
parentComponent: ComponentInternalInstance | null,
scopeId: string | null
2020-02-05 21:04:40 -05:00
) => void
2020-02-06 12:07:25 -05:00
export function ssrRenderSlot(
2020-02-05 21:04:40 -05:00
slots: Slots | SSRSlots,
slotName: string,
slotProps: Props,
fallbackRenderFn: (() => void) | null,
push: PushFn,
2020-02-16 01:41:20 +03:00
parentComponent: ComponentInternalInstance
2020-02-05 21:04:40 -05:00
) {
// template-compiled slots are always rendered as fragments
push(`<!--[-->`)
2020-02-05 21:04:40 -05:00
const slotFn = slots[slotName]
if (slotFn) {
const scopeId = parentComponent && parentComponent.type.__scopeId
const ret = slotFn(
slotProps,
push,
parentComponent,
scopeId ? ` ${scopeId}-s` : ``
)
if (Array.isArray(ret)) {
2020-02-05 21:04:40 -05:00
// normal slot
renderVNodeChildren(push, ret, parentComponent)
2020-02-05 21:04:40 -05:00
}
} else if (fallbackRenderFn) {
fallbackRenderFn()
}
push(`<!--]-->`)
2020-02-05 21:04:40 -05:00
}