Backtestsvuerun-20261003-231210#11910
Transition should sniff for teleport in custom components
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the same way. When a component inside <Transition> renders a <Teleport> root, the transition hooks are moved from the teleport vnode onto its single inner child. Enter then fires when the teleport mounts its children, and leave fires when the teleport unmounts them. Patch A puts this unwrapping in one place, `setTransitionHooks`. That single change covers three paths: the initial render through `renderComponentRoot`, the component branch that recurses into `subTree`, and any other caller. It also deliberately never sets hooks on the teleport vnode itself, so the teleport's start anchor is not treated as a leaving element. The dev warning is updated through `isTransitionRoot`, which allows comment-only content (a v-if switch inside the teleport) and nested teleports, and still warns when there are multiple children. Patch B adds the same unwrap logic in two places, `renderComponentRoot` and the component branch of `setTransitionHooks`. It reuses `getInnerChild`, which picks the first non-comment child. When the teleport has no inner child, Patch B falls back to setting hooks on the teleport vnode. That matches the old behaviour for this edge case and does not improve it. Tests: Patch A's unit tests check the full sequence of enter and leave hooks with the exact elements, and confirm that the teleported element stays in the target until leave completes. They also cover nested components, a v-if inside the teleport, disabled teleports, and the multiple-children warning. All of these would fail before the fix. Patch B has a valuable e2e test that reproduces the issue with real class transitions. Its unit tests are weaker: - The KeepAlive-root test is unrelated to the issue. - The empty-teleport test only checks that nothing throws and that a warning appears. - The hook-update test may pass even on the old code, because the hooks were previously attached to the teleport vnode. Overall, correctness is roughly equal. Patch A's implementation is more centralized and handles edge cases more carefully, and its unit tests are more thorough and targeted, so A is the better patch.
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
9m 59s
Wall-clock time in the container
Full test suite
No new failures
3,640 passed · 6 skipped · 3,646 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
191vs 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..e3a9af476 100644@@ -1,7 +1,9 @@ import { BaseTransition, type BaseTransitionProps,+ Comment, KeepAlive,+ Teleport, type TestElement, type VNodeProps, h,@@ -1242,6 +1244,156 @@ describe('BaseTransition', () => { }) }) + describe('component w/ Teleport root', () => {+ test('toggle on-off', async () => {+ const target = nodeOps.createElement('div')+ const Modal = {+ render: () => h(Teleport, { to: target }, [h('div', 'modal')]),+ }+ const toggle = ref(false)+ const { props, cbs } = mockProps()+ const { root } = mount(props, () => (toggle.value ? h(Modal) : null))+ expect(serializeInner(target)).toBe(``)++ // enter+ toggle.value = true+ await nextTick()+ expect(serializeInner(target)).toBe(`<div>modal</div>`)+ expect(serializeInner(root)).toBe(+ `<!--teleport start--><!--teleport end-->`,+ )+ expect(props.onBeforeEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeEnter, `<div>modal</div>`)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, `<div>modal</div>`)+ cbs.doneEnter[`<div>modal</div>`]()+ expect(props.onAfterEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onAfterEnter, `<div>modal</div>`)++ // leave+ toggle.value = false+ await nextTick()+ expect(props.onBeforeLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onBeforeLeave, `<div>modal</div>`)+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, `<div>modal</div>`)+ // element stays in the target until leave finishes+ expect(serializeInner(target)).toBe(`<div>modal</div>`)+ expect(serializeInner(root)).toBe(`<!---->`)+ cbs.doneLeave[`<div>modal</div>`]()+ expect(serializeInner(target)).toBe(``)+ expect(props.onAfterLeave).toHaveBeenCalledTimes(1)++ assertCalls(props, {+ onBeforeEnter: 1,+ onEnter: 1,+ onAfterEnter: 1,+ onEnterCancelled: 0,+ onBeforeLeave: 1,+ onLeave: 1,+ onAfterLeave: 1,+ onLeaveCancelled: 0,+ })+ expect(+ `renders non-element root node that cannot be animated`,+ ).not.toHaveBeenWarned()+ })++ test('nested component inside Teleport root', async () => {+ const target = nodeOps.createElement('div')+ const Inner = () => h('span', 'inner')+ const Modal = {+ render: () => h(Teleport, { to: target }, [h(Inner)]),+ }+ const toggle = ref(false)+ const { props, cbs } = mockProps()+ mount(props, () => (toggle.value ? h(Modal) : null))++ toggle.value = true+ await nextTick()+ expect(serializeInner(target)).toBe(`<span>inner</span>`)+ assertCalledWithEl(props.onEnter, `<span>inner</span>`)+ cbs.doneEnter[`<span>inner</span>`]()++ toggle.value = false+ await nextTick()+ assertCalledWithEl(props.onLeave, `<span>inner</span>`)+ expect(serializeInner(target)).toBe(`<span>inner</span>`)+ cbs.doneLeave[`<span>inner</span>`]()+ expect(serializeInner(target)).toBe(``)+ expect(+ `renders non-element root node that cannot be animated`,+ ).not.toHaveBeenWarned()+ })++ test('v-if inside Teleport root of a mounted component', async () => {+ const target = nodeOps.createElement('div')+ const show = ref(false)+ const Modal = {+ render: () =>+ h(Teleport, { to: target }, [+ show.value ? h('div', 'modal') : h(Comment),+ ]),+ }+ const { props, cbs } = mockProps()+ mount(props, () => h(Modal))+ expect(serializeInner(target)).toBe(`<!---->`)++ show.value = true+ await nextTick()+ expect(serializeInner(target)).toBe(`<div>modal</div>`)+ expect(props.onEnter).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onEnter, `<div>modal</div>`)+ cbs.doneEnter[`<div>modal</div>`]()++ show.value = false+ await nextTick()+ expect(props.onLeave).toHaveBeenCalledTimes(1)+ assertCalledWithEl(props.onLeave, `<div>modal</div>`)+ cbs.doneLeave[`<div>modal</div>`]()+ expect(serializeInner(target)).toBe(`<!---->`)+ expect(+ `renders non-element root node that cannot be animated`,+ ).not.toHaveBeenWarned()+ })++ test('disabled Teleport root', async () => {+ const Modal = {+ render: () => h(Teleport, { to: null, disabled: true }, [h('div')]),+ }+ const toggle = ref(false)+ const { props, cbs } = mockProps()+ const { root } = mount(props, () => (toggle.value ? h(Modal) : null))++ toggle.value = true+ await nextTick()+ expect(serializeInner(root)).toBe(+ `<!--teleport start--><div></div><!--teleport end-->`,+ )+ assertCalledWithEl(props.onEnter, `<div></div>`)+ cbs.doneEnter[`<div></div>`]()++ toggle.value = false+ await nextTick()+ assertCalledWithEl(props.onLeave, `<div></div>`)+ expect(serializeInner(root)).toBe(`<div></div><!---->`)+ cbs.doneLeave[`<div></div>`]()+ expect(serializeInner(root)).toBe(`<!---->`)+ })++ test('warns when Teleport root has multiple children', async () => {+ const target = nodeOps.createElement('div')+ const Modal = {+ render: () => h(Teleport, { to: target }, [h('div'), h('span')]),+ }+ const { props } = mockProps()+ mount(props, () => h(Modal))+ expect(+ `renders non-element root node that cannot be animated`,+ ).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..153105023 100644@@ -255,7 +255,7 @@ export function renderComponentRoot( } // inherit transition data if (vnode.transition) {- if (__DEV__ && !isElementRoot(root)) {+ if (__DEV__ && !isTransitionRoot(root)) { warn( `Component inside <Transition> renders non-element root node ` + `that cannot be animated.`,@@ -368,6 +368,21 @@ const isElementRoot = (vnode: VNode) => { ) } +const isTransitionRoot = (vnode: VNode): boolean => {+ if (vnode.shapeFlag & ShapeFlags.TELEPORT) {+ // a teleport root is transparent to <Transition>, check its content+ const children = vnode.children as VNode[] | null+ if (!children || !(vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN)) {+ return true+ }+ const content = children.filter(c => c.type !== Comment)+ // only comments: potential v-if branch switch inside the teleport+ if (!content.length) return true+ return content.length === 1 && isTransitionRoot(content[0])+ }+ return !!isElementRoot(vnode)+}+ export function shouldUpdateComponent( prevVNode: VNode, nextVNode: VNode,packages/runtime-core/src/components/BaseTransition.tsindex 4a9d5098e..2d2f98621 100644@@ -542,11 +542,33 @@ 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) {+ // e.g. a component inside <Transition> that renders a <Teleport> root:+ // the teleport itself is transparent, apply hooks to its content instead.+ // (hooks are intentionally not set on the teleport vnode, otherwise its+ // start anchor would also be treated as a leaving element on removal)+ const child = getTeleportInnerChild(vnode)+ if (child) setTransitionHooks(child, hooks) } else { vnode.transition = hooks } } +// returns the single non-comment child of a teleport vnode, if any+function getTeleportInnerChild(vnode: VNode): VNode | undefined {+ if (vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {+ let found: VNode | undefined+ for (const child of vnode.children as VNode[]) {+ if (child.type !== Comment) {+ // multiple children cannot be animated as a single node+ if (found) return+ found = child+ }+ }+ return found+ }+}+ export function getTransitionRawChildren( children: VNode[], keepComment: boolean = false,
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', () => {