export function findProp(
node: ElementNode,
name: string,
- dynamicOnly: boolean = false
+ dynamicOnly: boolean = false,
+ allowEmpty: boolean = false
): ElementNode['props'][0] | undefined {
for (let i = 0; i < node.props.length; i++) {
const p = node.props[i]
if (p.type === NodeTypes.ATTRIBUTE) {
if (dynamicOnly) continue
- if (p.name === name && p.value) {
+ if (p.name === name && (p.value || allowEmpty)) {
return p
}
} else if (p.name === 'bind' && p.exp && isBindKey(p.arg, name)) {
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderPortal(_push, (_push) => {
_push(\`<div></div>\`)
- }, _ctx.target, _parent)
+ }, _ctx.target, false, _parent)
}"
`)
})
+
+ test('disabled prop handling', () => {
+ expect(compile(`<portal :target="target" disabled><div/></portal>`).code)
+ .toMatchInlineSnapshot(`
+ "const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\")
+
+ return function ssrRender(_ctx, _push, _parent) {
+ _ssrRenderPortal(_push, (_push) => {
+ _push(\`<div></div>\`)
+ }, _ctx.target, true, _parent)
+ }"
+ `)
+
+ expect(
+ compile(`<portal :target="target" :disabled="foo"><div/></portal>`).code
+ ).toMatchInlineSnapshot(`
+ "const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\")
+
+ return function ssrRender(_ctx, _push, _parent) {
+ _ssrRenderPortal(_push, (_push) => {
+ _push(\`<div></div>\`)
+ }, _ctx.target, _ctx.foo, _parent)
+ }"
+ `)
+ })
})
import {
ComponentNode,
findProp,
- JSChildNode,
NodeTypes,
createSimpleExpression,
createFunctionExpression,
- createCallExpression
+ createCallExpression,
+ ExpressionNode
} from '@vue/compiler-dom'
import {
SSRTransformContext,
return
}
- let target: JSChildNode
- if (targetProp.type === NodeTypes.ATTRIBUTE && targetProp.value) {
- target = createSimpleExpression(targetProp.value.content, true)
- } else if (targetProp.type === NodeTypes.DIRECTIVE && targetProp.exp) {
- target = targetProp.exp
+ let target: ExpressionNode | undefined
+ if (targetProp.type === NodeTypes.ATTRIBUTE) {
+ target =
+ targetProp.value && createSimpleExpression(targetProp.value.content, true)
} else {
+ target = targetProp.exp
+ }
+ if (!target) {
context.onError(
createSSRCompilerError(
SSRErrorCodes.X_SSR_NO_PORTAL_TARGET,
return
}
+ const disabledProp = findProp(node, 'disabled', false, true /* allow empty */)
+ const disabled = disabledProp
+ ? disabledProp.type === NodeTypes.ATTRIBUTE
+ ? `true`
+ : disabledProp.exp || `false`
+ : `false`
+
const contentRenderFn = createFunctionExpression(
[`_push`],
undefined, // Body is added later
`_push`,
contentRenderFn,
target,
+ disabled,
`_parent`
])
)
REORDER // moved in the main view
}
+const isDisabled = (props: VNode['props']): boolean =>
+ props && (props.disabled || props.disabled === '')
+
const movePortal = (
vnode: VNode,
container: RendererElement,
// if this is a re-order and portal is enabled (content is in target)
// do not move children. So the opposite is: only move children if this
// is not a reorder, or the portal is disabled
- if (!isReorder || (props && props.disabled)) {
+ if (!isReorder || isDisabled(props)) {
// Portal has either Array children or no children.
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
for (let i = 0; i < (children as VNode[]).length; i++) {
} = internals
const targetSelector = n2.props && n2.props.target
- const disabled = n2.props && n2.props.disabled
+ const disabled = isDisabled(n2.props)
const { shapeFlag, children } = n2
if (n1 == null) {
if (__DEV__ && isString(targetSelector) && !querySelector) {
const mainAnchor = (n2.anchor = n1.anchor)!
const target = (n2.target = n1.target)!
const targetAnchor = (n2.targetAnchor = n1.targetAnchor)!
- const wasDisabled = n1.props && n1.props.disabled
+ const wasDisabled = isDisabled(n1.props)
const currentContainer = wasDisabled ? container : target
const currentAnchor = wasDisabled ? mainAnchor : targetAnchor
_push(`<div>content</div>`)
},
'#target',
+ false,
_parent
)
}
}),
ctx
)
- expect(html).toBe('<!--portal-->')
+ expect(html).toBe('<!--portal start--><!--portal end-->')
expect(ctx.portals!['#target']).toBe(`<div>content</div><!---->`)
})
+ test('portal rendering (compiled + disabled)', async () => {
+ const ctx: SSRContext = {}
+ const html = await renderToString(
+ createApp({
+ data() {
+ return { msg: 'hello' }
+ },
+ ssrRender(_ctx, _push, _parent) {
+ ssrRenderPortal(
+ _push,
+ _push => {
+ _push(`<div>content</div>`)
+ },
+ '#target',
+ true,
+ _parent
+ )
+ }
+ }),
+ ctx
+ )
+ expect(html).toBe('<!--portal start--><div>content</div><!--portal end-->')
+ expect(ctx.portals!['#target']).toBe(`<!---->`)
+ })
+
test('portal rendering (vnode)', async () => {
const ctx: SSRContext = {}
const html = await renderToString(
),
ctx
)
- expect(html).toBe('<!--portal-->')
+ expect(html).toBe('<!--portal start--><!--portal end-->')
expect(ctx.portals!['#target']).toBe('<span>hello</span><!---->')
})
+ test('portal rendering (vnode + disabled)', async () => {
+ const ctx: SSRContext = {}
+ const html = await renderToString(
+ h(
+ Portal,
+ {
+ target: `#target`,
+ disabled: true
+ },
+ h('span', 'hello')
+ ),
+ ctx
+ )
+ expect(html).toBe('<!--portal start--><span>hello</span><!--portal end-->')
+ expect(ctx.portals!['#target']).toBe(`<!---->`)
+ })
+
test('multiple portals with same target', async () => {
const ctx: SSRContext = {}
const html = await renderToString(
]),
ctx
)
- expect(html).toBe('<div><!--portal--><!--portal--></div>')
+ expect(html).toBe(
+ '<div><!--portal start--><!--portal end--><!--portal start--><!--portal end--></div>'
+ )
expect(ctx.portals!['#target']).toBe(
'<span>hello</span><!---->world<!---->'
)
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
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-->')
}
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 []
)
},
target,
+ disabled || disabled === '',
parentComponent
)
}