Skip to content

fix(runtime-vapor): use slot fallback on initial render #13144

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

Open
wants to merge 1 commit into
base: vapor
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
@@ -469,6 +469,43 @@ describe('component: slots', () => {
expect(html()).toBe('content<!--if--><!--slot-->')
})

test('use fallback on initial render', async () => {
const Child = {
setup() {
return createSlot('default', null, () =>
document.createTextNode('fallback'),
)
},
}

const toggle = ref(false)

const { html } = define({
setup() {
return createComponent(Child, null, {
default: () => {
return createIf(
() => toggle.value,
() => {
return document.createTextNode('content')
},
)
},
})
},
}).render()

expect(html()).toBe('fallback<!--if--><!--slot-->')

toggle.value = true
await nextTick()
expect(html()).toBe('content<!--if--><!--slot-->')

toggle.value = false
await nextTick()
expect(html()).toBe('fallback<!--if--><!--slot-->')
})

test('dynamic slot work with v-if', async () => {
const val = ref('header')
const toggle = ref(false)
16 changes: 14 additions & 2 deletions packages/runtime-vapor/src/componentSlots.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { EMPTY_OBJ, NO, hasOwn, isArray, isFunction } from '@vue/shared'
import { type Block, type BlockFn, DynamicFragment, insert } from './block'
import {
type Block,
type BlockFn,
DynamicFragment,
insert,
isValidBlock,
} from './block'
import { rawPropsProxyHandlers } from './componentProps'
import { currentInstance, isRef } from '@vue/runtime-dom'
import type { LooseRawProps, VaporComponentInstance } from './component'
@@ -127,7 +133,13 @@ export function createSlot(
(slot._bound = () => {
const slotContent = slot(slotProps)
if (slotContent instanceof DynamicFragment) {
slotContent.fallback = fallback
if (
(slotContent.fallback = fallback) &&
!isValidBlock(slotContent.nodes)
) {
// use fallback if the slot content is invalid
slotContent.update(fallback)
}
}
return slotContent
}),