Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(custom-elements): work with async component + slots #4657

Merged
merged 1 commit into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/runtime-core/src/helpers/renderSlot.ts
Expand Up @@ -4,8 +4,9 @@ import {
ContextualRenderFn,
currentRenderingInstance
} from '../componentRenderContext'
import { Comment, isVNode } from '../vnode'
import {
Comment,
isVNode,
VNodeArrayChildren,
openBlock,
createBlock,
Expand All @@ -15,6 +16,7 @@ import {
import { PatchFlags, SlotFlags } from '@vue/shared'
import { warn } from '../warning'
import { createVNode } from '@vue/runtime-core'
import { isAsyncWrapper } from '../apiAsyncComponent'

/**
* Compiler runtime helper for rendering `<slot/>`
Expand All @@ -29,7 +31,12 @@ export function renderSlot(
fallback?: () => VNodeArrayChildren,
noSlotted?: boolean
): VNode {
if (currentRenderingInstance!.isCE) {
if (
currentRenderingInstance!.isCE ||
(currentRenderingInstance!.parent &&
isAsyncWrapper(currentRenderingInstance!.parent) &&
currentRenderingInstance!.parent.isCE)
) {
return createVNode(
'slot',
name === 'default' ? null : { name },
Expand Down
28 changes: 28 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Expand Up @@ -426,5 +426,33 @@ describe('defineCustomElement', () => {
const e = container.childNodes[0] as VueElement
expect(e.shadowRoot!.innerHTML).toBe(`<div>20,number</div>`)
})

test('with slots', async () => {
const E = defineCustomElement(
defineAsyncComponent(() => {
return Promise.resolve({
render(this: any) {
return [
h('div', null, [
renderSlot(this.$slots, 'default', undefined, () => [
h('div', 'fallback')
])
]),
h('div', null, renderSlot(this.$slots, 'named'))
]
}
})
})
)
customElements.define('my-el-async-slots', E)
container.innerHTML = `<my-el-async-slots><span>hi</span></my-el-async-slots>`

await new Promise(r => setTimeout(r))

const e = container.childNodes[0] as VueElement
expect(e.shadowRoot!.innerHTML).toBe(
`<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`
)
})
})
})