Skip to content
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

fix(Teleport): handle teleport unmount edge case #12705

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions packages/runtime-core/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,36 @@ describe('renderer: teleport', () => {
expect(root.innerHTML).toBe('<!--v-if-->')
})

test('skip unmount children if teleport not disabled & target missing', async () => {
const root = document.createElement('div')
const childShow = ref(true)

const Comp = {
setup() {
return () => h(Teleport, { to: null }, [h('div', 'foo')])
},
}

const App = defineComponent({
setup() {
return () => {
return h(Fragment, { key: 0 }, [
childShow.value ? h(Comp) : createCommentVNode('v-if'),
])
}
},
})

domRender(h(App), root)
expect('Invalid Teleport target: null').toHaveBeenWarned()
expect('Invalid Teleport target on mount').toHaveBeenWarned()
expect(root.innerHTML).toBe('<!--teleport start--><!--teleport end-->')

childShow.value = false
await nextTick()
expect(root.innerHTML).toBe('<!--v-if-->')
})

test('accessing template refs inside teleport', async () => {
const target = nodeOps.createElement('div')
const tRef = ref()
Expand Down
10 changes: 7 additions & 3 deletions packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,20 @@ export const TeleportImpl = {
target,
props,
} = vnode
// an unmounted teleport should always unmount its children whether it's disabled or not
doRemove && hostRemove(anchor!)

// skip unmount if not disabled & target missing (children not rendered)
const disabled = isTeleportDisabled(props)
if (!disabled && !target) return

if (target) {
hostRemove(targetStart!)
hostRemove(targetAnchor!)
}

// an unmounted teleport should always unmount its children whether it's disabled or not
doRemove && hostRemove(anchor!)
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
const shouldRemove = doRemove || !isTeleportDisabled(props)
const shouldRemove = doRemove || !disabled
for (let i = 0; i < (children as VNode[]).length; i++) {
const child = (children as VNode[])[i]
unmount(
Expand Down
Loading