Skip to content

Commit 9a33d79

Browse files
authored
feat(runtime-vapor): attach current instance to render slot (#168)
1 parent db140a1 commit 9a33d79

File tree

2 files changed

+97
-28
lines changed

2 files changed

+97
-28
lines changed

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

+64-15
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ function renderWithSlots(slots: any): any {
3434
}
3535

3636
describe('component: slots', () => {
37-
test('initSlots: instance.slots should be set correctly', () => {
38-
const { slots } = renderWithSlots({ _: 1 })
39-
expect(slots).toMatchObject({ _: 1 })
40-
})
41-
42-
// NOTE: slot normalization is not supported
43-
test.todo(
44-
'initSlots: should normalize object slots (when value is null, string, array)',
45-
() => {},
46-
)
47-
test.todo(
48-
'initSlots: should normalize object slots (when value is function)',
49-
() => {},
50-
)
51-
5237
test('initSlots: instance.slots should be set correctly', () => {
5338
let instance: any
5439
const Comp = defineComponent({
@@ -73,6 +58,16 @@ describe('component: slots', () => {
7358
)
7459
})
7560

61+
// NOTE: slot normalization is not supported
62+
test.todo(
63+
'initSlots: should normalize object slots (when value is null, string, array)',
64+
() => {},
65+
)
66+
test.todo(
67+
'initSlots: should normalize object slots (when value is function)',
68+
() => {},
69+
)
70+
7671
// runtime-core's "initSlots: instance.slots should be set correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)"
7772
test('initSlots: instance.slots should be set correctly', () => {
7873
const { slots } = renderWithSlots({
@@ -152,6 +147,60 @@ describe('component: slots', () => {
152147
expect(instance.slots).toHaveProperty('footer')
153148
})
154149

150+
test('the current instance should be kept in the slot', async () => {
151+
let instanceInDefaultSlot: any
152+
let instanceInVForSlot: any
153+
let instanceInVIfSlot: any
154+
155+
const Comp = defineComponent({
156+
render() {
157+
const instance = getCurrentInstance()
158+
instance!.slots.default!()
159+
instance!.slots.inVFor!()
160+
instance!.slots.inVIf!()
161+
return template('<div></div>')()
162+
},
163+
})
164+
165+
const { instance } = define({
166+
render() {
167+
return createComponent(
168+
Comp,
169+
{},
170+
{
171+
default: () => {
172+
instanceInDefaultSlot = getCurrentInstance()
173+
return template('content')()
174+
},
175+
},
176+
() => [
177+
[
178+
{
179+
name: 'inVFor',
180+
fn: () => {
181+
instanceInVForSlot = getCurrentInstance()
182+
return template('content')()
183+
},
184+
},
185+
],
186+
{
187+
name: 'inVIf',
188+
key: '1',
189+
fn: () => {
190+
instanceInVIfSlot = getCurrentInstance()
191+
return template('content')()
192+
},
193+
},
194+
],
195+
)
196+
},
197+
}).render()
198+
199+
expect(instanceInDefaultSlot).toBe(instance)
200+
expect(instanceInVForSlot).toBe(instance)
201+
expect(instanceInVIfSlot).toBe(instance)
202+
})
203+
155204
test.todo('should respect $stable flag', async () => {
156205
// TODO: $stable flag?
157206
})

packages/runtime-vapor/src/componentSlots.ts

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { type IfAny, extend, isArray } from '@vue/shared'
1+
import { type IfAny, isArray } from '@vue/shared'
22
import { baseWatch } from '@vue/reactivity'
3-
import type { ComponentInternalInstance } from './component'
3+
import { type ComponentInternalInstance, setCurrentInstance } from './component'
44
import type { Block } from './apiRender'
55
import { createVaporPreScheduler } from './scheduler'
66

@@ -29,7 +29,14 @@ export const initSlots = (
2929
rawSlots: InternalSlots | null = null,
3030
dynamicSlots: DynamicSlots | null = null,
3131
) => {
32-
const slots: InternalSlots = extend({}, rawSlots)
32+
const slots: InternalSlots = {}
33+
34+
for (const key in rawSlots) {
35+
const slot = rawSlots[key]
36+
if (slot) {
37+
slots[key] = withCtx(slot)
38+
}
39+
}
3340

3441
if (dynamicSlots) {
3542
const dynamicSlotKeys: Record<string, true> = {}
@@ -41,20 +48,22 @@ export const initSlots = (
4148
// array of dynamic slot generated by <template v-for="..." #[...]>
4249
if (isArray(slot)) {
4350
for (let j = 0; j < slot.length; j++) {
44-
slots[slot[j].name] = slot[j].fn
51+
slots[slot[j].name] = withCtx(slot[j].fn)
4552
dynamicSlotKeys[slot[j].name] = true
4653
}
4754
} else if (slot) {
4855
// conditional single slot generated by <template v-if="..." #foo>
49-
slots[slot.name] = slot.key
50-
? (...args: any[]) => {
51-
const res = slot.fn(...args)
52-
// attach branch key so each conditional branch is considered a
53-
// different fragment
54-
if (res) (res as any).key = slot.key
55-
return res
56-
}
57-
: slot.fn
56+
slots[slot.name] = withCtx(
57+
slot.key
58+
? (...args: any[]) => {
59+
const res = slot.fn(...args)
60+
// attach branch key so each conditional branch is considered a
61+
// different fragment
62+
if (res) (res as any).key = slot.key
63+
return res
64+
}
65+
: slot.fn,
66+
)
5867
dynamicSlotKeys[slot.name] = true
5968
}
6069
}
@@ -77,4 +86,15 @@ export const initSlots = (
7786
}
7887

7988
instance.slots = slots
89+
90+
function withCtx(fn: Slot): Slot {
91+
return (...args: any[]) => {
92+
const reset = setCurrentInstance(instance.parent!)
93+
try {
94+
return fn(...args)
95+
} finally {
96+
reset()
97+
}
98+
}
99+
}
80100
}

0 commit comments

Comments
 (0)