Backtestsvuerun-20261009-161443#11910
Transition should sniff for teleport in custom components
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the root cause. Each makes a component whose root is a Teleport forward the Transition hooks to the Teleport's inner child: at initial render in renderComponentRoot, and on later hook updates through setTransitionHooks recursing into the component subTree.
Patch A does this with an `isTeleport(...) ? getInnerChild(...) || ... : ...` check repeated at two call sites. It reuses getInnerChild/findNonCommentChild, which reads `c.type` on every child. Teleport children built with h() are not normalized yet at renderComponentRoot time, so a null or false entry (e.g. `h(Teleport, {to}, [null, h('div')])`) would throw a TypeError.
Patch B puts the Teleport handling inside setTransitionHooks as its own TELEPORT branch. That covers the component-subTree recursion, HOC chains and any other caller in one place. Its getTeleportInnerChild filters out non-vnode children first, which avoids the crash above. B also adds isTransitionableRoot so the dev warning is suppressed only when the Teleport actually wraps an element. If the child is missing it falls back cleanly.
On tests, B's unit tests are stronger. They check enter and leave hook calls with the right elements. They check that the element stays in the teleport target until leave completes, and they cover the HOC wrapper, null/false children and out-in mode, plus the absence of warnings. All of these would fail before the fix.
A's e2e test does cover the issue scenario well. Its unit tests are thinner, though: one KeepAlive test unrelated to the issue, one empty-Teleport test and one hook-update test.
Overall, B is more robust in implementation and test coverage with comparable scope.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
7m 50s
Wall-clock time in the container
Full test suite
No new failures
1,748 passed · 1 skipped · 1,749 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
179vs 160 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.4
Link to minimal reproduction
Steps to reproduce
Click the button
What is expected?
Both should transition in
What is actually happening?
One transitions correctly, the other throws a warning and blinks into existance
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
CPU: (16) x64 AMD Ryzen 7 PRO 6850HS with Radeon Graphics
Memory: 11.86 GB / 15.28 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node
Yarn: 3.2.2 - ~/.nvm/versions/node/v22.2.0/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v22.2.0/bin/npm
pnpm: 7.25.0 - ~/.nvm/versions/node/v22.2.0/bin/pnpm
bun: 1.1.8 - ~/.bun/bin/bun
Browsers:
Chrome: 126.0.6478.55
Any additional comments?
This is a follow up from
- #5836
My Intention in #5836 was, that a user should be able to do this:
<Transition>
<CustomComp />
</Transition>
<!-- Custom Comp -->
<Teleport>
<div></div>
</Teleport>
However, the fix for #5836 only allows:
<Transition>
<Teleport>
<div></div>
</Teleport>
</Transition>
For me as a user those 2 should feel equivalent.
The way it is now is not really useful. If you are not gonna use a custom component inside transition, you could just easily swap teleport and transition around (which already worked before the fix):
<teleport>
<transition>
<div></div>
</transition>
</teleport>
But the whole point of #5836 was to allow to transition any component that teleports internally.
And since nesting a custom component into a transition works already, teleport should just be transparent in this case
Usecase:
- modals
<transition><Modal /></transition> - popovers
- ...
The two patches
packages/runtime-core/__tests__/components/BaseTransition.spec.tsindex c541d5fc4..6bb9e78d5 100644@@ -2,6 +2,7 @@ import { BaseTransition, type BaseTransitionProps, KeepAlive,+ Teleport, type TestElement, type VNodeProps, h,@@ -1293,4 +1294,147 @@ describe('BaseTransition', () => { await runTestWithKeepAlive(testOutIn) __DEV__ = true })++ // #11908+ describe('w/ component rendering Teleport as root', () => {+ function createTeleportComps(target: TestElement) {+ const TeleportComp = {+ props: ['msg'],+ render(this: any) {+ return h(Teleport, { to: target }, [h('div', this.msg)])+ },+ }+ // HOC wrapping the teleporting component+ const Wrapper = {+ props: ['msg'],+ render(this: any) {+ return h(TeleportComp, { msg: this.msg })+ },+ }+ return { TeleportComp, Wrapper }+ }++ async function testTeleportToggle(getComp: (target: TestElement) => any) {+ const target = nodeOps.createElement('div')+ const Comp = getComp(target)+ const toggle = ref(false)+ const { props, cbs } = mockProps()+ const { root } = mount(props, () =>+ toggle.value ? h(Comp, { msg: 'hello' }) : null,+ )+ expect(serializeInner(target)).toBe(``)++ // enter+ toggle.value = true+ await nextTick()+ expect(serializeInner(target)).toBe(`<div>hello</div>`)+ expect(props.onBeforeEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeEnter, `<div>hello</div>`)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, `<div>hello</div>`)+ expect(props.onAfterEnter).not.toHaveBeenCalled()+ cbs.doneEnter[`<div>hello</div>`]()+ expect(props.onAfterEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterEnter, `<div>hello</div>`)++ // leave+ toggle.value = false+ await nextTick()+ expect(props.onBeforeLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeLeave, `<div>hello</div>`)+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, `<div>hello</div>`)+ // element stays in the teleport target until leave finishes+ expect(serializeInner(target)).toBe(`<div>hello</div>`)+ expect(props.onAfterLeave).not.toHaveBeenCalled()+ cbs.doneLeave[`<div>hello</div>`]()+ expect(serializeInner(target)).toBe(``)+ expect(serializeInner(root)).toBe(`<!---->`)+ expect(props.onAfterLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterLeave, `<div>hello</div>`)++ expect(+ `renders non-element root node that cannot be animated`,+ ).not.toHaveBeenWarned()+ }++ test('toggle on-off', async () => {+ await testTeleportToggle(+ target => createTeleportComps(target).TeleportComp,+ )+ })++ test('toggle on-off w/ HOC', async () => {+ await testTeleportToggle(target => createTeleportComps(target).Wrapper)+ })++ test('toggle on-off w/ non-vnode Teleport children', async () => {+ const target = nodeOps.createElement('div')+ const Comp = {+ render: () =>+ h(Teleport, { to: target }, [null, false as any, h('div', 'hi')]),+ }+ const toggle = ref(false)+ const { props, cbs } = mockProps()+ mount(props, () => (toggle.value ? h(Comp) : null))++ toggle.value = true+ await nextTick()+ expect(serializeInner(target)).toBe(`<!----><!----><div>hi</div>`)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, `<div>hi</div>`)+ cbs.doneEnter[`<div>hi</div>`]()++ toggle.value = false+ await nextTick()+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, `<div>hi</div>`)+ expect(serializeInner(target)).toBe(`<div>hi</div>`)+ cbs.doneLeave[`<div>hi</div>`]()+ expect(serializeInner(target)).toBe(``)+ expect(props.onAfterLeave).toHaveBeenCalledTimes(1)++ expect(+ `renders non-element root node that cannot be animated`,+ ).not.toHaveBeenWarned()+ expect(+ `<transition> can only be used on a single element`,+ ).not.toHaveBeenWarned()+ })++ test('mode: "out-in" between teleporting components', async () => {+ const target = nodeOps.createElement('div')+ const CompA = {+ render: () => h(Teleport, { to: target }, [h('div', 'a')]),+ }+ const CompB = {+ render: () => h(Teleport, { to: target }, [h('span', 'b')]),+ }+ const toggle = ref(true)+ const { props, cbs } = mockProps({ mode: 'out-in' })+ mount(props, () => (toggle.value ? h(CompA) : h(CompB)))+ expect(serializeInner(target)).toBe(`<div>a</div>`)++ toggle.value = false+ await nextTick()+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, `<div>a</div>`)+ // enter should not start before leave finishes+ expect(props.onEnter).not.toHaveBeenCalled()+ expect(serializeInner(target)).toBe(`<div>a</div>`)++ cbs.doneLeave[`<div>a</div>`]()+ await nextTick()+ expect(props.onAfterLeave).toHaveBeenCalledTimes(1)+ expect(serializeInner(target)).toBe(`<span>b</span>`)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, `<span>b</span>`)+ cbs.doneEnter[`<span>b</span>`]()+ expect(props.onAfterEnter).toHaveBeenCalledTimes(1)++ expect(+ `renders non-element root node that cannot be animated`,+ ).not.toHaveBeenWarned()+ })+ }) })packages/runtime-core/src/componentRenderUtils.tsindex 0b46130c0..9530dc0bd 100644@@ -34,7 +34,10 @@ import { warnDeprecation, } from './compat/compatConfig' import { shallowReadonly } from '@vue/reactivity'-import { setTransitionHooks } from './components/BaseTransition'+import {+ getTeleportInnerChild,+ setTransitionHooks,+} from './components/BaseTransition' /** * dev only flag to track whether $attrs was used during render.@@ -255,7 +258,7 @@ export function renderComponentRoot( } // inherit transition data if (vnode.transition) {- if (__DEV__ && !isElementRoot(root)) {+ if (__DEV__ && !isTransitionableRoot(root)) { warn( `Component inside <Transition> renders non-element root node ` + `that cannot be animated.`,@@ -368,6 +371,15 @@ const isElementRoot = (vnode: VNode) => { ) } +// a Teleport root is transparent to Transition: its inner child is animated+const isTransitionableRoot = (vnode: VNode): boolean => {+ if (vnode.shapeFlag & ShapeFlags.TELEPORT) {+ const child = getTeleportInnerChild(vnode)+ return !!child && isTransitionableRoot(child)+ }+ return !!isElementRoot(vnode)+}+ export function shouldUpdateComponent( prevVNode: VNode, nextVNode: VNode,packages/runtime-core/src/components/BaseTransition.tsindex 4a9d5098e..1536c5895 100644@@ -12,6 +12,7 @@ import { cloneVNode, createCommentVNode, isSameVNodeType,+ isVNode, } from '../vnode' import { warn } from '../warning' import { isKeepAlive } from './KeepAlive'@@ -506,6 +507,15 @@ function emptyPlaceholder(vnode: VNode): VNode | undefined { } } +export function getTeleportInnerChild(vnode: VNode): VNode | undefined {+ const { shapeFlag, children } = vnode+ if (shapeFlag & ShapeFlags.TELEPORT && shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ // children may not be normalized yet (e.g. null / false from h() calls)+ const vnodes = (children as VNodeArrayChildren).filter(isVNode)+ return vnodes.length ? findNonCommentChild(vnodes) : undefined+ }+}+ function getInnerChild(vnode: VNode): VNode | undefined { if (!isKeepAlive(vnode)) { if (isTeleport(vnode.type) && vnode.children) {@@ -542,6 +552,15 @@ export function setTransitionHooks(vnode: VNode, hooks: TransitionHooks): void { } else if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) { vnode.ssContent!.transition = hooks.clone(vnode.ssContent!) vnode.ssFallback!.transition = hooks.clone(vnode.ssFallback!)+ } else if (vnode.shapeFlag & ShapeFlags.TELEPORT) {+ // a Teleport (e.g. a component's root) is transparent to Transition:+ // apply the hooks to its single inner child instead+ const child = getTeleportInnerChild(vnode)+ if (child) {+ setTransitionHooks(child, hooks)+ } else {+ vnode.transition = hooks+ } } else { vnode.transition = hooks }
packages/runtime-core/__tests__/components/BaseTransition.spec.tsindex c541d5fc4..65abcc6ff 100644@@ -2,6 +2,7 @@ import { BaseTransition, type BaseTransitionProps, KeepAlive,+ Teleport, type TestElement, type VNodeProps, h,@@ -1293,4 +1294,66 @@ describe('BaseTransition', () => { await runTestWithKeepAlive(testOutIn) __DEV__ = true })++ test('should preserve transition for component with KeepAlive root', async () => {+ const root = nodeOps.createElement('div')+ const show = ref(false)+ const onBeforeEnter = vi.fn()+ const Child = { render: () => h('div', 'content') }+ const Comp = {+ render: () => h(KeepAlive, null, { default: () => h(Child) }),+ }+ const App = () =>+ h(BaseTransition, { onBeforeEnter }, () => (show.value ? h(Comp) : null))++ render(h(App), root)++ show.value = true+ await nextTick()+ expect(serializeInner(root)).toBe('<div>content</div>')+ expect(onBeforeEnter).toHaveBeenCalledTimes(1)+ })++ test('should not error on component with empty Teleport root', () => {+ const root = nodeOps.createElement('div')+ const target = nodeOps.createElement('div')+ const Comp = () => h(Teleport, { to: target }, [])++ expect(() =>+ render(+ h(BaseTransition, null, () => h(Comp)),+ root,+ ),+ ).not.toThrow()+ expect(serializeInner(root)).toBe(+ '<!--teleport start--><!--teleport end-->',+ )+ expect(serializeInner(target)).toBe('')+ expect(+ 'Component inside <Transition> renders non-element root node',+ ).toHaveBeenWarned()+ })++ test('should update transition hooks for component with Teleport root', async () => {+ const root = nodeOps.createElement('div')+ const target = nodeOps.createElement('div')+ const show = ref(true)+ const firstBeforeLeave = vi.fn()+ const nextBeforeLeave = vi.fn()+ const beforeLeave = ref(firstBeforeLeave)+ const Comp = () => h(Teleport, { to: target }, h('div', 'content'))+ const App = () =>+ h(BaseTransition, { onBeforeLeave: beforeLeave.value }, () =>+ show.value ? h(Comp) : null,+ )++ render(h(App), root)++ beforeLeave.value = nextBeforeLeave+ show.value = false+ await nextTick()++ expect(firstBeforeLeave).not.toHaveBeenCalled()+ expect(nextBeforeLeave).toHaveBeenCalledTimes(1)+ }) })packages/runtime-core/src/componentRenderUtils.tsindex 0b46130c0..be58e5b0a 100644@@ -34,7 +34,8 @@ import { warnDeprecation, } from './compat/compatConfig' import { shallowReadonly } from '@vue/reactivity'-import { setTransitionHooks } from './components/BaseTransition'+import { getInnerChild, setTransitionHooks } from './components/BaseTransition'+import { isTeleport } from './components/Teleport' /** * dev only flag to track whether $attrs was used during render.@@ -255,13 +256,14 @@ export function renderComponentRoot( } // inherit transition data if (vnode.transition) {- if (__DEV__ && !isElementRoot(root)) {+ const child = isTeleport(root.type) ? getInnerChild(root) || root : root+ if (__DEV__ && !isElementRoot(child)) { warn( `Component inside <Transition> renders non-element root node ` + `that cannot be animated.`, ) }- setTransitionHooks(root, vnode.transition)+ setTransitionHooks(child, vnode.transition) } if (__DEV__ && setRoot) {packages/runtime-core/src/components/BaseTransition.tsindex 4a9d5098e..a8cfe8b71 100644@@ -506,7 +506,7 @@ function emptyPlaceholder(vnode: VNode): VNode | undefined { } } -function getInnerChild(vnode: VNode): VNode | undefined {+export function getInnerChild(vnode: VNode): VNode | undefined { if (!isKeepAlive(vnode)) { if (isTeleport(vnode.type) && vnode.children) { return findNonCommentChild(vnode.children as VNode[])@@ -538,7 +538,11 @@ function getInnerChild(vnode: VNode): VNode | undefined { export function setTransitionHooks(vnode: VNode, hooks: TransitionHooks): void { if (vnode.shapeFlag & ShapeFlags.COMPONENT && vnode.component) { vnode.transition = hooks- setTransitionHooks(vnode.component.subTree, hooks)+ const subTree = vnode.component.subTree+ setTransitionHooks(+ isTeleport(subTree.type) ? getInnerChild(subTree) || subTree : subTree,+ hooks,+ ) } else if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) { vnode.ssContent!.transition = hooks.clone(vnode.ssContent!) vnode.ssFallback!.transition = hooks.clone(vnode.ssFallback!)packages/vue/__tests__/e2e/Transition.spec.tsindex 17519e832..025cc7c00 100644@@ -2782,6 +2782,87 @@ describe('e2e: Transition', () => { }, E2E_TIMEOUT, )++ // #11910+ test(+ 'apply transition to teleport component child',+ async () => {+ await page().evaluate(() => {+ const { createApp, ref } = (window as any).Vue+ createApp({+ template: `+ <div id="target"></div>+ <div id="container">+ <transition>+ <Comp v-if="toggle"></Comp>+ </transition>+ </div>+ <button id="toggleBtn" @click="click">button</button>+ `,+ components: {+ Comp: {+ template: `+ <Teleport to="#target">+ <div class="test">content</div>+ </Teleport>+ `,+ },+ },+ setup: () => {+ const toggle = ref(false)+ const click = () => (toggle.value = !toggle.value)+ return { toggle, click }+ },+ }).mount('#app')+ })++ expect(await html('#target')).toBe('')+ expect(await html('#container')).toBe('<!--v-if-->')++ const classWhenTransitionStart = () =>+ page().evaluate(() => {+ ;(document.querySelector('#toggleBtn') as any)!.click()+ return Promise.resolve().then(() => {+ // find the class of teleported node+ return document+ .querySelector('#target div')!+ .className.split(/\s+/g)+ })+ })++ // enter+ expect(await classWhenTransitionStart()).toStrictEqual([+ 'test',+ 'v-enter-from',+ 'v-enter-active',+ ])+ await nextFrame()+ expect(await classList('.test')).toStrictEqual([+ 'test',+ 'v-enter-active',+ 'v-enter-to',+ ])+ await transitionFinish()+ expect(await html('#target')).toBe('<div class="test">content</div>')++ // leave+ expect(await classWhenTransitionStart()).toStrictEqual([+ 'test',+ 'v-leave-from',+ 'v-leave-active',+ ])+ await nextFrame()+ expect(await classList('.test')).toStrictEqual([+ 'test',+ 'v-leave-active',+ 'v-leave-to',+ ])+ await transitionFinish()+ expect(await html('#target')).toBe('')+ expect(await html('#container')).toBe('<!--v-if-->')+ },+ E2E_TIMEOUT,+ ) }) describe('transition with v-show', () => {