Skip to content

Commit 62eba63

Browse files
committed
fix(transition): handle transition for v-if branches with comment
fix #5675
1 parent 767d212 commit 62eba63

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

packages/runtime-core/src/components/BaseTransition.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,25 @@ const BaseTransitionImpl: ComponentOptions = {
148148
return
149149
}
150150

151-
// warn multiple elements
152-
if (__DEV__ && children.length > 1) {
153-
warn(
154-
'<transition> can only be used on a single element or component. Use ' +
155-
'<transition-group> for lists.'
156-
)
151+
let child: VNode = children[0]
152+
if (children.length > 1) {
153+
let hasFound = false
154+
// locate first non-comment child
155+
for (const c of children) {
156+
if (c.type !== Comment) {
157+
if (__DEV__ && hasFound) {
158+
// warn more than one non-comment child
159+
warn(
160+
'<transition> can only be used on a single element or component. ' +
161+
'Use <transition-group> for lists.'
162+
)
163+
break
164+
}
165+
child = c
166+
hasFound = true
167+
if (!__DEV__) break
168+
}
169+
}
157170
}
158171

159172
// there's no need to track reactivity for these props so use the raw
@@ -171,8 +184,6 @@ const BaseTransitionImpl: ComponentOptions = {
171184
warn(`invalid <transition> mode: ${mode}`)
172185
}
173186

174-
// at this point children has a guaranteed length of 1.
175-
const child = children[0]
176187
if (state.isLeaving) {
177188
return emptyPlaceholder(child)
178189
}

packages/vue/__tests__/Transition.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ describe('e2e: Transition', () => {
20392039
template: `
20402040
<div id="container">
20412041
<transition>
2042-
<Comp class="test" v-if="toggle">
2042+
<Comp class="test" v-if="toggle">
20432043
<div>content</div>
20442044
</Comp>
20452045
</transition>

0 commit comments

Comments
 (0)