Skip to content

Commit 671997a

Browse files
authored
fix(teleport): handle teleport unmount edge case (#12705)
close #12702
1 parent 164af63 commit 671997a

2 files changed

Lines changed: 80 additions & 8 deletions

File tree

‎packages/runtime-core/__tests__/components/Teleport.spec.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,76 @@ describe('renderer: teleport', () => {
11421142
expect(root.innerHTML).toBe('<!--v-if-->')
11431143
})
11441144

1145+
test('skip unmount children if teleport not disabled & target missing', async () => {
1146+
const root = document.createElement('div')
1147+
const childShow = ref(true)
1148+
1149+
const Comp = {
1150+
setup() {
1151+
return () => h(Teleport, { to: null }, [h('div', 'foo')])
1152+
},
1153+
}
1154+
1155+
const App = defineComponent({
1156+
setup() {
1157+
return () => {
1158+
return h(Fragment, { key: 0 }, [
1159+
childShow.value ? h(Comp) : createCommentVNode('v-if'),
1160+
])
1161+
}
1162+
},
1163+
})
1164+
1165+
domRender(h(App), root)
1166+
expect('Invalid Teleport target: null').toHaveBeenWarned()
1167+
expect('Invalid Teleport target on mount').toHaveBeenWarned()
1168+
expect(root.innerHTML).toBe('<!--teleport start--><!--teleport end-->')
1169+
1170+
childShow.value = false
1171+
await nextTick()
1172+
expect(root.innerHTML).toBe('<!--v-if-->')
1173+
})
1174+
1175+
test('unmount mounted children after target becomes missing', async () => {
1176+
const root = document.createElement('div')
1177+
const target = document.createElement('div')
1178+
const teleportTarget = ref<Element | null>(target)
1179+
const childShow = ref(true)
1180+
1181+
const Comp = {
1182+
setup() {
1183+
return () =>
1184+
h(Teleport, { to: teleportTarget.value }, [h('div', 'foo')])
1185+
},
1186+
}
1187+
1188+
const App = defineComponent({
1189+
setup() {
1190+
return () => {
1191+
return h(Fragment, { key: 0 }, [
1192+
childShow.value ? h(Comp) : createCommentVNode('v-if'),
1193+
])
1194+
}
1195+
},
1196+
})
1197+
1198+
domRender(h(App), root)
1199+
expect(root.innerHTML).toBe('<!--teleport start--><!--teleport end-->')
1200+
expect(target.innerHTML).toBe('<div>foo</div>')
1201+
1202+
teleportTarget.value = null
1203+
await nextTick()
1204+
expect('Invalid Teleport target: null').toHaveBeenWarned()
1205+
expect('Invalid Teleport target on update').toHaveBeenWarned()
1206+
expect(target.innerHTML).toBe('<div>foo</div>')
1207+
1208+
childShow.value = false
1209+
await nextTick()
1210+
expect(root.innerHTML).toBe('<!--v-if-->')
1211+
expect(target.innerHTML).toBe('')
1212+
expect(target.childNodes.length).toBe(0)
1213+
})
1214+
11451215
test('accessing template refs inside teleport', async () => {
11461216
const target = nodeOps.createElement('div')
11471217
const tRef = ref()

‎packages/runtime-core/src/components/Teleport.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,9 @@ export const TeleportImpl = {
279279
} else {
280280
// target changed
281281
if ((n2.props && n2.props.to) !== (n1.props && n1.props.to)) {
282-
const nextTarget = (n2.target = resolveTarget(
283-
n2.props,
284-
querySelector,
285-
))
282+
const nextTarget = resolveTarget(n2.props, querySelector)
286283
if (nextTarget) {
284+
n2.target = nextTarget
287285
moveTeleport(
288286
n2,
289287
nextTarget,
@@ -330,8 +328,8 @@ export const TeleportImpl = {
330328
target,
331329
props,
332330
} = vnode
333-
334-
const shouldRemove = doRemove || !isTeleportDisabled(props)
331+
const disabled = isTeleportDisabled(props)
332+
const shouldRemove = doRemove || !disabled
335333
// A deferred teleport inside a pending suspense may be unmounted before its
336334
// content is ever mounted. Clear the queued mount effect; the children
337335
// loop below is skipped because nothing has been mounted yet.
@@ -348,8 +346,12 @@ export const TeleportImpl = {
348346

349347
// an unmounted teleport should always unmount its children whether it's disabled or not
350348
doRemove && hostRemove(anchor!)
351-
// #14876 don't unmount children if in pending mount (nothing was mounted)
352-
if (!pendingMount && shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
349+
// #14876 don't unmount children if nothing was mounted
350+
if (
351+
!pendingMount &&
352+
(disabled || target) &&
353+
shapeFlag & ShapeFlags.ARRAY_CHILDREN
354+
) {
353355
for (let i = 0; i < (children as VNode[]).length; i++) {
354356
const child = (children as VNode[])[i]
355357
unmount(

0 commit comments

Comments
 (0)