Skip to content

fix(runtime-vapor): render slot fallback if no content is provided #13561

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion packages/runtime-vapor/__tests__/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createVaporApp } from '../src'
import { createVaporApp, vaporInteropPlugin } from '../src'
import type { App } from '@vue/runtime-dom'
import type { VaporComponent, VaporComponentInstance } from '../src/component'
import type { RawProps } from '../src/componentProps'
import { type Component, createApp } from 'vue'

export interface RenderContext {
component: VaporComponent
Expand Down Expand Up @@ -82,3 +83,54 @@ export function makeRender<C = VaporComponent>(

return define
}

export interface InteropRenderContext {
mount: (container?: string | ParentNode) => InteropRenderContext
render: (
props?: RawProps,
container?: string | ParentNode,
) => InteropRenderContext
html: () => string
}

export function makeInteropRender(): (comp: Component) => InteropRenderContext {
let host: HTMLElement
beforeEach(() => {
host = document.createElement('div')
})
afterEach(() => {
host.remove()
})

function define(comp: Component) {
let app: App
function render(
props: RawProps | undefined = undefined,
container: string | ParentNode = host,
) {
app?.unmount()
app = createApp(comp, props)
app.use(vaporInteropPlugin)
return mount(container)
}

function mount(container: string | ParentNode = host) {
app.mount(container)
return res()
}

function html() {
return host.innerHTML
}

const res = () => ({
mount,
render,
html,
})

return res()
}

return define
}
29 changes: 29 additions & 0 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { h } from 'vue'
import { createSlot, defineVaporComponent, template } from '../src'
import { makeInteropRender } from './_utils'

const define = makeInteropRender()

describe('vdomInterop', () => {
describe('slots', () => {
test('should render slot fallback if no slot content is provided', () => {
const VaporChild = defineVaporComponent({
setup() {
const n0 = createSlot('default', null, () => {
const n2 = template('<div>hi</div>')()
return n2
})
return n0
},
})

const { html } = define({
setup() {
return () => h(VaporChild as any)
},
}).render()

expect(html()).toBe('<div>hi</div>')
})
})
})
36 changes: 19 additions & 17 deletions packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,26 @@ function renderVDOMSlot(
frag.insert = (parentNode, anchor) => {
if (!isMounted) {
renderEffect(() => {
const vnode = renderSlot(
slotsRef.value,
isFunction(name) ? name() : name,
props,
)
if ((vnode.children as any[]).length) {
if (fallbackNodes) {
remove(fallbackNodes, parentNode)
fallbackNodes = undefined
}
internals.p(
oldVNode,
vnode,
parentNode,
anchor,
parentComponent as any,
if (slotsRef.value) {
const vnode = renderSlot(
slotsRef.value,
isFunction(name) ? name() : name,
props,
)
oldVNode = vnode
if ((vnode.children as any[]).length) {
if (fallbackNodes) {
remove(fallbackNodes, parentNode)
fallbackNodes = undefined
}
internals.p(
oldVNode,
vnode,
parentNode,
anchor,
parentComponent as any,
)
oldVNode = vnode
}
} else {
if (fallback && !fallbackNodes) {
// mount fallback
Expand Down
Loading