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

feat(transition): support transition to teleport component child #11959

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 4 additions & 3 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
warnDeprecation,
} from './compat/compatConfig'
import { shallowReadonly } from '@vue/reactivity'
import { setTransitionHooks } from './components/BaseTransition'
import { getInnerChild, setTransitionHooks } from './components/BaseTransition'

/**
* dev only flag to track whether $attrs was used during render.
Expand Down Expand Up @@ -248,13 +248,14 @@ export function renderComponentRoot(
}
// inherit transition data
if (vnode.transition) {
if (__DEV__ && !isElementRoot(root)) {
const child = getInnerChild(root) ?? root
yangxiuxiu1115 marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/components/BaseTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,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[])
Expand Down
81 changes: 80 additions & 1 deletion packages/vue/__tests__/e2e/Transition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,86 @@ describe('e2e: Transition', () => {
)
},
E2E_TIMEOUT,
)
),
test(
yangxiuxiu1115 marked this conversation as resolved.
Show resolved Hide resolved
'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', () => {
Expand Down