feat(portal): SSR support for portal disabled prop

This commit is contained in:
Evan You
2020-03-27 23:45:50 -04:00
parent 8ce3da0104
commit 9ed9bf3687
7 changed files with 126 additions and 23 deletions

View File

@@ -1,16 +1,31 @@
import { ComponentInternalInstance, ssrContextKey } from 'vue'
import { SSRContext, createBuffer, PushFn } from '../renderToString'
import {
SSRContext,
createBuffer,
PushFn,
SSRBufferItem
} from '../renderToString'
export function ssrRenderPortal(
parentPush: PushFn,
contentRenderFn: (push: PushFn) => void,
target: string,
disabled: boolean,
parentComponent: ComponentInternalInstance
) {
parentPush('<!--portal-->')
const { getBuffer, push } = createBuffer()
contentRenderFn(push)
push(`<!---->`) // portal end anchor
parentPush('<!--portal start-->')
let portalContent: SSRBufferItem
if (disabled) {
contentRenderFn(parentPush)
portalContent = `<!---->`
} else {
const { getBuffer, push } = createBuffer()
contentRenderFn(push)
push(`<!---->`) // portal end anchor
portalContent = getBuffer()
}
const context = parentComponent.appContext.provides[
ssrContextKey as any
@@ -18,8 +33,10 @@ export function ssrRenderPortal(
const portalBuffers =
context.__portalBuffers || (context.__portalBuffers = {})
if (portalBuffers[target]) {
portalBuffers[target].push(getBuffer())
portalBuffers[target].push(portalContent)
} else {
portalBuffers[target] = [getBuffer()]
portalBuffers[target] = [portalContent]
}
parentPush('<!--portal end-->')
}

View File

@@ -366,6 +366,7 @@ function renderPortalVNode(
parentComponent: ComponentInternalInstance
) {
const target = vnode.props && vnode.props.target
const disabled = vnode.props && vnode.props.disabled
if (!target) {
warn(`[@vue/server-renderer] Portal is missing target prop.`)
return []
@@ -386,6 +387,7 @@ function renderPortalVNode(
)
},
target,
disabled || disabled === '',
parentComponent
)
}