Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/compiler-vapor/src/generators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@ function genRawSlots(
slotDeclarations: string[],
) {
if (!slots.length) return
// like vdom's `createSlots()`, v-if / v-for slots override unconditional
// ones whatever the source order, so they go last (dynamic slots resolve
// from the end)
slots = [
...slots.filter(slot => !isConditionalOrLoopSlot(slot)),
...slots.filter(isConditionalOrLoopSlot),
]
const staticSlots = slots[0]
if (staticSlots.slotType === IRSlotType.STATIC) {
const defaultSlot = getSingleDefaultSlot(staticSlots)
Expand All @@ -623,6 +630,13 @@ function genRawSlots(
}
}

function isConditionalOrLoopSlot(slot: IRSlots): boolean {
return (
slot.slotType === IRSlotType.CONDITIONAL ||
slot.slotType === IRSlotType.LOOP
)
}

function getSingleDefaultSlot({ slots }: IRSlotsStatic) {
const names = Object.keys(slots)
return names.length === 1 && names[0] === 'default'
Expand Down
32 changes: 32 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,38 @@ describe('component: slots', () => {
expect(vapor.text).toBe(vdom.text)
})

test('v-if / v-for slots override unconditional ones like vdom', async () => {
const texts: Record<string, string[]> = {}
await renderParity(
{
Child: `<template><div><slot name="a">fa</slot>|<slot name="b">fb</slot></div></template>`,
App: `<template><components.Child>
<template v-for="n in data.names" #[n]>forwarded {{ n }}</template>
<template #a>own a</template>
<template v-if="data.ok" #b>own b</template>
<template #[data.name]>computed</template>
</components.Child></template>`,
},
() => ref({ names: ['a'], ok: true, name: 'b' }),
async (data, root, mode) => {
const seen = [root.textContent!]
data.value.names = []
await nextTick()
seen.push(root.textContent!)
data.value.ok = false
await nextTick()
seen.push(root.textContent!)
texts[mode] = seen
},
)
expect(texts.vdom).toEqual([
'forwarded a|own b',
'own a|own b',
'own a|computed',
])
expect(texts.vapor).toEqual(texts.vdom)
})

describe('slot fallback boundary', () => {
test('slot fragment insert uses active fallback output', () => {
const container = document.createElement('div')
Expand Down
Loading