Skip to content

Commit 4aaa69a

Browse files
committed
wip: save
1 parent b5f6f01 commit 4aaa69a

File tree

4 files changed

+110
-27
lines changed

4 files changed

+110
-27
lines changed

packages/runtime-core/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,11 @@ export { type VaporInteropInterface } from './apiCreateApp'
505505
/**
506506
* @internal
507507
*/
508-
export { type RendererInternals, MoveType } from './renderer'
508+
export {
509+
type RendererInternals,
510+
MoveType,
511+
getInheritedScopeIds,
512+
} from './renderer'
509513
/**
510514
* @internal
511515
*/

packages/runtime-core/src/renderer.ts

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -764,30 +764,9 @@ function baseCreateRenderer(
764764
hostSetScopeId(el, slotScopeIds[i])
765765
}
766766
}
767-
let subTree = parentComponent && parentComponent.subTree
768-
if (subTree) {
769-
if (
770-
__DEV__ &&
771-
subTree.patchFlag > 0 &&
772-
subTree.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT
773-
) {
774-
subTree =
775-
filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
776-
}
777-
if (
778-
vnode === subTree ||
779-
(isSuspense(subTree.type) &&
780-
(subTree.ssContent === vnode || subTree.ssFallback === vnode))
781-
) {
782-
const parentVNode = parentComponent!.vnode!
783-
setScopeId(
784-
el,
785-
parentVNode,
786-
parentVNode.scopeId,
787-
parentVNode.slotScopeIds,
788-
parentComponent!.parent,
789-
)
790-
}
767+
const inheritedScopeIds = getInheritedScopeIds(vnode, parentComponent)
768+
for (let i = 0; i < inheritedScopeIds.length; i++) {
769+
hostSetScopeId(el, inheritedScopeIds[i])
791770
}
792771
}
793772

@@ -2656,3 +2635,54 @@ function getVaporInterface(
26562635
}
26572636
return res!
26582637
}
2638+
2639+
/**
2640+
* shared between vdom and vapor
2641+
*/
2642+
export function getInheritedScopeIds(
2643+
vnode: VNode,
2644+
parentComponent: GenericComponentInstance | null,
2645+
): string[] {
2646+
const inheritedScopeIds: string[] = []
2647+
2648+
let currentParent = parentComponent
2649+
let currentVNode = vnode
2650+
2651+
while (currentParent) {
2652+
let subTree = currentParent.subTree
2653+
if (!subTree) break
2654+
2655+
if (
2656+
__DEV__ &&
2657+
subTree.patchFlag > 0 &&
2658+
subTree.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT
2659+
) {
2660+
subTree =
2661+
filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
2662+
}
2663+
2664+
if (
2665+
currentVNode === subTree ||
2666+
(isSuspense(subTree.type) &&
2667+
(subTree.ssContent === currentVNode ||
2668+
subTree.ssFallback === currentVNode))
2669+
) {
2670+
const parentVNode = currentParent.vnode!
2671+
2672+
if (parentVNode.scopeId) {
2673+
inheritedScopeIds.push(parentVNode.scopeId)
2674+
}
2675+
2676+
if (parentVNode.slotScopeIds) {
2677+
inheritedScopeIds.push(...parentVNode.slotScopeIds)
2678+
}
2679+
2680+
currentVNode = parentVNode
2681+
currentParent = currentParent.parent
2682+
} else {
2683+
break
2684+
}
2685+
}
2686+
2687+
return inheritedScopeIds
2688+
}

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,50 @@ describe('vdom interop', () => {
321321
)
322322
})
323323

324+
test('vdom parent > vapor > vapor > vdom child', () => {
325+
const InnerVdomChild = {
326+
__scopeId: 'inner-vdom-child',
327+
setup() {
328+
return () => h('button')
329+
},
330+
}
331+
332+
const VaporChild = defineVaporComponent({
333+
__scopeId: 'vapor-child',
334+
setup() {
335+
return createComponent(InnerVdomChild as any, null, null, true)
336+
},
337+
})
338+
339+
const VaporChild2 = defineVaporComponent({
340+
__scopeId: 'vapor-child2',
341+
setup() {
342+
return createComponent(VaporChild as any, null, null, true)
343+
},
344+
})
345+
346+
const VdomChild = {
347+
__scopeId: 'vdom-child',
348+
setup() {
349+
return () => h(VaporChild2 as any)
350+
},
351+
}
352+
353+
const App = {
354+
__scopeId: 'parent',
355+
setup() {
356+
return () => h(VdomChild)
357+
},
358+
}
359+
360+
const root = document.createElement('div')
361+
createApp(App).use(vaporInteropPlugin).mount(root)
362+
363+
expect(root.innerHTML).toBe(
364+
`<button inner-vdom-child="" vapor-child="" vapor-child2="" vdom-child="" parent=""></button>`,
365+
)
366+
})
367+
324368
test('vdom parent > vapor dynamic child', () => {
325369
const VaporChild = defineVaporComponent({
326370
__scopeId: 'vapor-child',
@@ -418,7 +462,7 @@ describe('vdom interop', () => {
418462
)
419463
})
420464

421-
test.todo('vapor parent > vdom > vdom > vapor child', () => {
465+
test('vapor parent > vdom > vdom > vapor child', () => {
422466
const InnerVaporChild = defineVaporComponent({
423467
__scopeId: 'inner-vapor-child',
424468
setup() {

packages/runtime-vapor/src/block.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { createComment, createTextNode } from './dom/node'
99
import { EffectScope, pauseTracking, resetTracking } from '@vue/reactivity'
1010
import { isHydrating } from './dom/hydration'
11+
import { getInheritedScopeIds } from '@vue/runtime-dom'
1112

1213
export type Block =
1314
| Node
@@ -213,12 +214,16 @@ export function setComponentScopeId(instance: VaporComponentInstance): void {
213214
setScopeId(instance.block, scopeId)
214215
}
215216

216-
// vdom parent
217+
// inherit scopeId from vdom parent
217218
if (
218219
parent.subTree &&
219220
(parent.subTree.component as any) === instance &&
220221
parent.vnode!.scopeId
221222
) {
222223
setScopeId(instance.block, parent.vnode!.scopeId)
224+
const scopeIds = getInheritedScopeIds(parent.vnode!, parent.parent)
225+
for (const id of scopeIds) {
226+
setScopeId(instance.block, id)
227+
}
223228
}
224229
}

0 commit comments

Comments
 (0)