Skip to content

Commit b0c04eb

Browse files
fix(vapor): properly move vapor component / slot (#14363)
* fix(runtime-code): MoveFn is compatible with vapor block * fix(vdomInterop): update move function to accept moveType parameter --------- Co-authored-by: daiwei <daiwei521@126.com>
1 parent 7da7615 commit b0c04eb

4 files changed

Lines changed: 70 additions & 16 deletions

File tree

packages/runtime-core/src/apiCreateApp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
import { type Directive, validateDirectiveName } from './directives'
2020
import type {
2121
ElementNamespace,
22+
MoveType,
2223
RootRenderFunction,
2324
UnmountComponentFn,
2425
} from './renderer'
@@ -191,7 +192,7 @@ export interface VaporInteropInterface {
191192
): GenericComponentInstance // VaporComponentInstance
192193
update(n1: VNode, n2: VNode, shouldUpdate: boolean): void
193194
unmount(vnode: VNode, doRemove?: boolean): void
194-
move(vnode: VNode, container: any, anchor: any): void
195+
move(vnode: VNode, container: any, anchor: any, moveType: MoveType): void
195196
slot(
196197
n1: VNode | null,
197198
n2: VNode,

packages/runtime-core/src/renderer.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,18 +2202,25 @@ function baseCreateRenderer(
22022202
parentSuspense = null,
22032203
) => {
22042204
const { el, type, transition, children, shapeFlag } = vnode
2205+
2206+
if (isVaporComponent(type as ConcreteComponent) || type === VaporSlot) {
2207+
getVaporInterface(parentComponent, vnode).move(
2208+
vnode,
2209+
container,
2210+
anchor,
2211+
moveType,
2212+
)
2213+
return
2214+
}
2215+
22052216
if (shapeFlag & ShapeFlags.COMPONENT) {
2206-
if (isVaporComponent(type as ConcreteComponent)) {
2207-
getVaporInterface(parentComponent, vnode).move(vnode, container, anchor)
2208-
} else {
2209-
move(
2210-
vnode.component!.subTree,
2211-
container,
2212-
anchor,
2213-
moveType,
2214-
parentComponent,
2215-
)
2216-
}
2217+
move(
2218+
vnode.component!.subTree,
2219+
container,
2220+
anchor,
2221+
moveType,
2222+
parentComponent,
2223+
)
22172224
return
22182225
}
22192226

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
child,
2929
createComponent,
3030
createDynamicComponent,
31+
createIf,
3132
createSlot,
3233
defineVaporAsyncComponent,
3334
defineVaporComponent,
@@ -1219,5 +1220,44 @@ describe('vdomInterop', () => {
12191220
expect(inputEl.value).toBe('vapor')
12201221
assertHookCalls(hooks, [2, 2, 3, 2, 1])
12211222
})
1223+
1224+
test('render vapor slot', async () => {
1225+
const show = ref(true)
1226+
1227+
const VDomComp = defineComponent({
1228+
setup(_, { slots }) {
1229+
return () => renderSlot(slots, 'default')
1230+
},
1231+
})
1232+
const App = defineVaporComponent({
1233+
setup() {
1234+
const n5 = createComponent(VaporKeepAlive, null, {
1235+
default: () =>
1236+
createIf(
1237+
() => show.value,
1238+
() =>
1239+
createComponent(VDomComp as any, null, {
1240+
default: () => template('slot text')(),
1241+
}),
1242+
),
1243+
})
1244+
return n5
1245+
},
1246+
})
1247+
1248+
const { html } = define({
1249+
setup() {
1250+
return () => h(App)
1251+
},
1252+
}).render()
1253+
1254+
expect(html()).toBe('slot text<!--if-->')
1255+
show.value = false
1256+
await nextTick()
1257+
expect(html()).toBe('<!--if-->')
1258+
show.value = true
1259+
await nextTick()
1260+
expect(html()).toBe('slot text<!--if-->')
1261+
})
12221262
})
12231263
})

packages/runtime-vapor/src/vdomInterop.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ import {
4848
mountComponent,
4949
unmountComponent,
5050
} from './component'
51-
import { type Block, type VaporTransitionHooks, insert, remove } from './block'
51+
import {
52+
type Block,
53+
type VaporTransitionHooks,
54+
insert,
55+
move,
56+
remove,
57+
} from './block'
5258
import {
5359
EMPTY_OBJ,
5460
ShapeFlags,
@@ -213,9 +219,9 @@ const vaporInteropImpl: Omit<
213219
}
214220
},
215221

216-
move(vnode, container, anchor) {
217-
insert(vnode.vb || (vnode.component as any), container, anchor)
218-
insert(vnode.anchor as any, container, anchor)
222+
move(vnode, container, anchor, moveType) {
223+
move(vnode.vb || (vnode.component as any), container, anchor, moveType)
224+
move(vnode.anchor as any, container, anchor, moveType)
219225
},
220226

221227
hydrate(vnode, node, container, anchor, parentComponent, parentSuspense) {

0 commit comments

Comments
 (0)