Skip to content

Commit 8c76f57

Browse files
committed
feat(runtime-vapor): dynamic component fallback work with dynamic slots
1 parent 1134405 commit 8c76f57

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,35 @@ describe('api: createDynamicComponent', () => {
148148
await nextTick()
149149
expect(html()).toBe('<div><div>B</div><!--dynamic-component--></div>')
150150
})
151+
152+
test('fallback with dynamic slots', async () => {
153+
const slotName = ref('default')
154+
const { html } = define({
155+
setup() {
156+
return createDynamicComponent(() => 'div', null, {
157+
$: [
158+
() => ({
159+
name: slotName.value,
160+
fn: () => template('<span>hi</span>')(),
161+
}),
162+
] as any,
163+
})
164+
},
165+
}).render()
166+
167+
expect(html()).toBe(
168+
'<div><span>hi</span><!--slot--></div><!--dynamic-component-->',
169+
)
170+
171+
// update slot name
172+
slotName.value = 'custom'
173+
await nextTick()
174+
expect(html()).toBe('<div><!--slot--></div><!--dynamic-component-->')
175+
176+
slotName.value = 'default'
177+
await nextTick()
178+
expect(html()).toBe(
179+
'<div><span>hi</span><!--slot--></div><!--dynamic-component-->',
180+
)
181+
})
151182
})

packages/runtime-vapor/src/component.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,20 @@ export function createComponentWithFallback(
665665
setCurrentHydrationNode(el.firstChild)
666666
}
667667
if (rawSlots.$) {
668-
// TODO dynamic slot fragment
668+
const frag =
669+
isHydrating || __DEV__
670+
? new DynamicFragment('slot')
671+
: new DynamicFragment()
672+
673+
renderEffect(() => frag.update(getSlot(rawSlots as RawSlots, 'default')))
674+
675+
if (!isHydrating) {
676+
const scopeId = currentInstance!.type.__scopeId
677+
if (scopeId) setScopeId(frag, [`${scopeId}-s`])
678+
insert(frag, el)
679+
} else {
680+
frag.hydrate()
681+
}
669682
} else {
670683
insert(getSlot(rawSlots as RawSlots, 'default')!(), el)
671684
}

0 commit comments

Comments
 (0)