Backtestsvuerun-20261001-022054#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 the same way. When a component's root is a <Teleport>, they forward the transition hooks to the teleported child element instead of leaving them on the Teleport vnode, which cannot be animated. Both use findNonCommentChild semantics, matching how BaseTransition already handles a direct Teleport child. Patch B does this in one place, a new Teleport branch inside setTransitionHooks. That single branch covers both paths: - the initial render path (renderComponentRoot calls setTransitionHooks(root)); - the leave/update path (the component branch recurses into subTree, which hits the Teleport branch). It also handles nested component → component → Teleport chains naturally. Patch A instead repeats the `isTeleport(x) ? getInnerChild(x) || x : x` unwrapping at two call sites (renderComponentRoot and the component branch of setTransitionHooks) and exports getInnerChild to do it. This works, but the logic is scattered rather than centralized. On the dev warning, Patch A keeps it meaningful for a Teleport with no element child. Patch B suppresses it for any Teleport root. This is a minor edge-case difference. On tests, Patch B adds thorough unit tests in the existing BaseTransition style. They cover the full enter/leave lifecycle through a component with a Teleport root, check hook call counts and the element arguments, verify the element stays in the target until the leave finishes, and add an out-in mode case. These would fail before the fix. Patch A adds a useful e2e test plus unit tests. However, one of them (KeepAlive root) is unrelated to the issue, and the others only cover empty Teleport and hook updates rather than the core enter/leave behavior at the unit level. Overall, Patch B delivers the fix more cohesively and with stronger targeted tests.
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 55s15 turns
Wall-clock time in the container
Full test suite
No new failures
3,637 passed · 6 skipped · 3,643 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
134vs 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..d3eb41d9d 100644@@ -2,6 +2,7 @@ import { BaseTransition, type BaseTransitionProps, KeepAlive,+ Teleport, type TestElement, type VNodeProps, h,@@ -1242,6 +1243,128 @@ describe('BaseTransition', () => { }) }) + // #5836 (direct <Teleport> child) - same case through a component whose+ // root is a <Teleport>+ test('w/ component that renders Teleport as root', async () => {+ const target = nodeOps.createElement('div')+ const Comp = {+ render: () => h(Teleport, { to: target }, [h('div', 'teleported')]),+ }+ const toggle = ref(false)+ const { props, cbs } = mockProps()+ const { root } = mount(props, () => (toggle.value ? h(Comp) : null))+ const el = `<div>teleported</div>`++ expect(serializeInner(root)).toBe(`<!---->`)+ expect(serializeInner(target)).toBe(``)++ // enter+ toggle.value = true+ await nextTick()+ expect(serializeInner(target)).toBe(el)+ expect(props.onBeforeEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeEnter, el)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, el)+ expect(props.onAfterEnter).not.toHaveBeenCalled()+ cbs.doneEnter[el]()+ expect(props.onAfterEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterEnter, el)++ // leave+ toggle.value = false+ await nextTick()+ expect(props.onBeforeLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeLeave, el)+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, el)+ expect(props.onAfterLeave).not.toHaveBeenCalled()+ // element stays in the target until the leave transition finishes+ expect(serializeInner(target)).toBe(el)+ cbs.doneLeave[el]()+ expect(props.onAfterLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterLeave, el)+ expect(serializeInner(target)).toBe(``)+ expect(serializeInner(root)).toBe(`<!---->`)++ assertCalls(props, {+ onBeforeEnter: 1,+ onEnter: 1,+ onAfterEnter: 1,+ onEnterCancelled: 0,+ onBeforeLeave: 1,+ onLeave: 1,+ onAfterLeave: 1,+ onLeaveCancelled: 0,+ })+ expect(+ 'Component inside <Transition> renders non-element root node that cannot be animated',+ ).not.toHaveBeenWarned()+ })++ test('w/ component that renders Teleport as root (out-in)', async () => {+ const target = nodeOps.createElement('div')+ const CompA = {+ render: () => h(Teleport, { to: target }, [h('div', 'A')]),+ }+ const CompB = {+ render: () => h(Teleport, { to: target }, [h('div', 'B')]),+ }+ const toggle = ref(true)+ const { props, cbs } = mockProps({ mode: 'out-in' })+ mount(props, () => (toggle.value ? h(CompA) : h(CompB)))+ const elA = `<div>A</div>`+ const elB = `<div>B</div>`++ expect(serializeInner(target)).toBe(elA)++ // trigger toggle+ toggle.value = false+ await nextTick()+ expect(props.onBeforeLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeLeave, elA)+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, elA)+ expect(props.onAfterLeave).not.toHaveBeenCalled()+ // B must not be mounted into the target until A's leave finishes+ expect(serializeInner(target)).toBe(elA)+ // enter should not have started+ expect(props.onBeforeEnter).not.toHaveBeenCalled()+ expect(props.onEnter).not.toHaveBeenCalled()+ expect(props.onAfterEnter).not.toHaveBeenCalled()++ cbs.doneLeave[elA]()+ expect(props.onAfterLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterLeave, elA)+ // have to wait for a tick because this triggers an update+ await nextTick()+ expect(serializeInner(target)).toBe(elB)+ // enter should start+ expect(props.onBeforeEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeEnter, elB)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, elB)+ expect(props.onAfterEnter).not.toHaveBeenCalled()+ // finish enter+ cbs.doneEnter[elB]()+ expect(props.onAfterEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterEnter, elB)++ assertCalls(props, {+ onBeforeEnter: 1,+ onEnter: 1,+ onAfterEnter: 1,+ onEnterCancelled: 0,+ onBeforeLeave: 1,+ onLeave: 1,+ onAfterLeave: 1,+ onLeaveCancelled: 0,+ })+ expect(+ 'Component inside <Transition> renders non-element root node that cannot be animated',+ ).not.toHaveBeenWarned()+ })+ // #10719 test('should not error on KeepAlive w/ function children', () => { expect(() => mount({}, () => () => h('div'), true)).not.toThrow()packages/runtime-core/src/componentRenderUtils.tsindex 0b46130c0..d5ec2a48e 100644@@ -35,6 +35,7 @@ import { } from './compat/compatConfig' import { shallowReadonly } from '@vue/reactivity' import { setTransitionHooks } from './components/BaseTransition'+import { isTeleport } from './components/Teleport' /** * dev only flag to track whether $attrs was used during render.@@ -255,7 +256,7 @@ export function renderComponentRoot( } // inherit transition data if (vnode.transition) {- if (__DEV__ && !isElementRoot(root)) {+ if (__DEV__ && !isElementRoot(root) && !isTeleport(root.type)) { warn( `Component inside <Transition> renders non-element root node ` + `that cannot be animated.`,packages/runtime-core/src/components/BaseTransition.tsindex 4a9d5098e..077cf424b 100644@@ -542,6 +542,14 @@ 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 (+ isTeleport(vnode.type) &&+ vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN &&+ (vnode.children as VNode[]).length+ ) {+ // e.g. a component whose root is a <Teleport>: forward hooks to the+ // teleported child, mirroring getInnerChild+ setTransitionHooks(findNonCommentChild(vnode.children as VNode[]), 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', () => {
What the agent reported
<Transition><CustomComp/></Transition>, where the component's root is a <Teleport>, now animates the teleported element on enter and leave, and the warning is gone. The full ./bt-test run passes: 3637 passed, 6 skipped, 0 failed.
What was wrong: When a component sits inside <Transition>, its transition hooks are handed down to whatever the component renders at its root. The earlier fix for #5836 only unwrapped a <Teleport> placed directly inside <Transition>. When the Teleport was the component's root, the hooks stopped at the Teleport, which isn't a real element. So the <div> inside never animated, and Vue warned "Component inside <Transition> renders non-element root node that cannot be animated."
What I changed (code edits are uncommitted; nothing was committed):
packages/runtime-core/src/components/BaseTransition.ts,setTransitionHooks: if the node is a Teleport with children, the hooks now go to its first non-comment child. This is the same lookup the direct<Transition><Teleport>case already uses.- A first version also attached the hooks to the Teleport itself. Review caught that this breaks
mode="out-in": removing the Teleport fires the "leave finished" step straight away, so the next child mounted before the old one finished animating out. I removed that line. - I also added a check so a Teleport with no children is skipped instead of crashing.
packages/runtime-core/src/componentRenderUtils.ts: the warning no longer fires when a component's root is a Teleport. The similar warning for directives is unchanged.
Tests (added to packages/runtime-core/__tests__/components/BaseTransition.spec.ts):
w/ component that renders Teleport as root: the reported scenario. It checks that enter and leave run on the teleported div, the div stays in the target until the leave finishes, and the warning is not shown. Before the fix it failed: no enter hooks were called and the warning appeared.w/ component that renders Teleport as root (out-in): checks that the next component doesn't mount until the old one's leave finishes. It passes with the fix and fails if the hooks are also attached to the Teleport.- Both are unit tests. The original #5836 test is a browser (e2e) test, and
./bt-testcan't run those. It also refuses arguments, so I ran it without them (the full suite); the subagents used vitest directly for single-file checks.
Requirements:
- "Both should transition in" (the reproduction): done. The first test covers enter, and I checked leave too.
- No warning in that case: done. Both tests assert it isn't shown.
- The Teleport should be transparent, so
<Transition><CustomComp/></Transition>behaves like<Transition><Teleport>…</Teleport></Transition>: done. It reuses the same lookup, and the existing #5836 test and the rest of the suite still pass. - Use cases (modals, popovers): these follow the same component-with-Teleport-root pattern, so the same tests cover them, including switching between components with
out-in.
Known limitation: the warning is now silenced for any component whose root is a Teleport. That includes a Teleport whose first child is text or that has several children, cases that still can't animate. I left it that way to keep the change small.