Skip to content

fix(runtime-vapor): dynamic component work with insertionState #13142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 20, 2025
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
31 changes: 31 additions & 0 deletions packages/runtime-vapor/__tests__/apiCreateDynamicComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { nextTick, resolveDynamicComponent } from '@vue/runtime-dom'
import {
createComponentWithFallback,
createDynamicComponent,
defineVaporComponent,
renderEffect,
setHtml,
setInsertionState,
Expand Down Expand Up @@ -79,4 +80,34 @@ describe('api: createDynamicComponent', () => {
mount()
expect(html()).toBe('<div><button>hi</button></div>')
})

test('switch dynamic component children', async () => {
const CompA = defineVaporComponent({
setup() {
return template('<div>A</div>')()
},
})
const CompB = defineVaporComponent({
setup() {
return template('<div>B</div>')()
},
})

const current = shallowRef(CompA)
const { html } = define({
setup() {
const t1 = template('<div></div>')
const n2 = t1() as any
setInsertionState(n2)
createDynamicComponent(() => current.value)
return n2
},
}).render()

expect(html()).toBe('<div><div>A</div><!--dynamic-component--></div>')

current.value = CompB
await nextTick()
expect(html()).toBe('<div><div>B</div><!--dynamic-component--></div>')
})
})
22 changes: 21 additions & 1 deletion packages/runtime-vapor/src/apiCreateDynamicComponent.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { resolveDynamicComponent } from '@vue/runtime-dom'
import { DynamicFragment, type VaporFragment } from './block'
import { DynamicFragment, type VaporFragment, insert } from './block'
import { createComponentWithFallback } from './component'
import { renderEffect } from './renderEffect'
import type { RawProps } from './componentProps'
import type { RawSlots } from './componentSlots'
import {
insertionAnchor,
insertionParent,
resetInsertionState,
} from './insertionState'
import { isHydrating, locateHydrationNode } from './dom/hydration'

export function createDynamicComponent(
getter: () => any,
rawProps?: RawProps | null,
rawSlots?: RawSlots | null,
isSingleRoot?: boolean,
): VaporFragment {
const _insertionParent = insertionParent
const _insertionAnchor = insertionAnchor
if (isHydrating) {
locateHydrationNode()
} else {
resetInsertionState()
}

const frag = __DEV__
? new DynamicFragment('dynamic-component')
: new DynamicFragment()

renderEffect(() => {
const value = getter()
frag.update(
Expand All @@ -27,5 +42,10 @@ export function createDynamicComponent(
value,
)
})

if (!isHydrating && _insertionParent) {
insert(frag, _insertionParent, _insertionAnchor)
}

return frag
}