Skip to content

Commit 187396d

Browse files
committed
Merge branch 'edison/fix/vaporRenderSlotfallback' into edison/testVapor
2 parents 3a3fcda + 53564e0 commit 187396d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/runtime-vapor/__tests__/componentSlots.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,43 @@ describe('component: slots', () => {
470470
expect(html()).toBe('content<!--if--><!--slot-->')
471471
})
472472

473+
test('use fallback on initial render', async () => {
474+
const Child = {
475+
setup() {
476+
return createSlot('default', null, () =>
477+
document.createTextNode('fallback'),
478+
)
479+
},
480+
}
481+
482+
const toggle = ref(false)
483+
484+
const { html } = define({
485+
setup() {
486+
return createComponent(Child, null, {
487+
default: () => {
488+
return createIf(
489+
() => toggle.value,
490+
() => {
491+
return document.createTextNode('content')
492+
},
493+
)
494+
},
495+
})
496+
},
497+
}).render()
498+
499+
expect(html()).toBe('fallback<!--if--><!--slot-->')
500+
501+
toggle.value = true
502+
await nextTick()
503+
expect(html()).toBe('content<!--if--><!--slot-->')
504+
505+
toggle.value = false
506+
await nextTick()
507+
expect(html()).toBe('fallback<!--if--><!--slot-->')
508+
})
509+
473510
test('dynamic slot work with v-if', async () => {
474511
const val = ref('header')
475512
const toggle = ref(false)

packages/runtime-vapor/src/componentSlots.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import {
66
isArray,
77
isFunction,
88
} from '@vue/shared'
9-
import { type Block, type BlockFn, DynamicFragment, insert } from './block'
9+
import {
10+
type Block,
11+
type BlockFn,
12+
DynamicFragment,
13+
insert,
14+
isValidBlock,
15+
} from './block'
1016
import { rawPropsProxyHandlers } from './componentProps'
1117
import { currentInstance, isRef } from '@vue/runtime-dom'
1218
import type { LooseRawProps, VaporComponentInstance } from './component'
@@ -149,7 +155,13 @@ export function createSlot(
149155
(slot._bound = () => {
150156
const slotContent = slot(slotProps)
151157
if (slotContent instanceof DynamicFragment) {
152-
slotContent.fallback = fallback
158+
if (
159+
(slotContent.fallback = fallback) &&
160+
!isValidBlock(slotContent.nodes)
161+
) {
162+
// use fallback if the slot content is invalid
163+
slotContent.update(fallback)
164+
}
153165
}
154166
return slotContent
155167
}),

0 commit comments

Comments
 (0)