<Transition> component provides animated transition effects when elements enter or leave the DOM. It applies CSS classes at different stages of the transition and provides JavaScript hooks for more complex animations.
Basic Usage
Props
name
- Type:
string - Default:
'v'
name: 'fade' will auto expand to .fade-enter-active, .fade-leave-active, etc.
type
- Type:
'transition' | 'animation'
"transition" and "animation". By default, it will automatically detect the type that has a longer duration.
css
- Type:
boolean - Default:
true
false, will only trigger JavaScript hooks registered via component events.
duration
- Type:
number | { enter: number; leave: number }
transitionend or animationend event on the root transition element.
mode
- Type:
'in-out' | 'out-in' | 'default'
'in-out': New element transitions in first, then when complete, the current element transitions out.'out-in': Current element transitions out first, then when complete, the new element transitions in.'default': Both transitions happen simultaneously.
appear
- Type:
boolean - Default:
false
appearFromClass, appearActiveClass, and appearToClass.
persisted
- Type:
boolean - Default:
false
v-show).
Transition Classes
Six classes are applied for enter/leave transitions:Enter Transitions
${name}-enter-from: Starting state for enter. Added before element is inserted, removed one frame after element is inserted.${name}-enter-active: Active state for enter. Applied during the entire entering phase. Added before element is inserted, removed when transition/animation finishes.${name}-enter-to: Ending state for enter. Added one frame after element is inserted (at the same timeenter-fromis removed), removed when transition/animation finishes.
Leave Transitions
${name}-leave-from: Starting state for leave. Added immediately when a leaving transition is triggered, removed after one frame.${name}-leave-active: Active state for leave. Applied during the entire leaving phase. Added immediately when leave transition is triggered, removed when the transition/animation finishes.${name}-leave-to: Ending state for leave. Added one frame after a leaving transition is triggered (at the same timeleave-fromis removed), removed when the transition/animation finishes.
Custom Classes
You can override the default class names:- enterFromClass -
string - enterActiveClass -
string - enterToClass -
string - leaveFromClass -
string - leaveActiveClass -
string - leaveToClass -
string
Appear Classes
For initial render transitions whenappear is true:
- appearFromClass -
string(defaults toenterFromClass) - appearActiveClass -
string(defaults toenterActiveClass) - appearToClass -
string(defaults toenterToClass)
JavaScript Hooks
You can also hook into the transition process with JavaScript:Enter Hooks
onBeforeEnter
- Type:
(el: Element) => void
onEnter
- Type:
(el: Element, done: () => void) => void
done callback is provided as a second argument, it must be called to signal the end of the transition.
onAfterEnter
- Type:
(el: Element) => void
onEnterCancelled
- Type:
(el: Element) => void
Leave Hooks
onBeforeLeave
- Type:
(el: Element) => void
onLeave
- Type:
(el: Element, done: () => void) => void
done callback is provided as a second argument, it must be called to signal the end of the transition.
onAfterLeave
- Type:
(el: Element) => void
onLeaveCancelled
- Type:
(el: Element) => void
v-show). Called when the leave transition is cancelled.
Appear Hooks
Whenappear is true, these hooks are called on initial render:
- onBeforeAppear - defaults to
onBeforeEnter - onAppear - defaults to
onEnter - onAfterAppear - defaults to
onAfterEnter - onAppearCancelled - defaults to
onEnterCancelled
JavaScript-only Transitions
When using JavaScript-only transitions, it’s recommended to add the
:css="false" prop. This explicitly tells Vue to skip CSS transition detection, preventing CSS rules from accidentally interfering.Transition with Components
<Transition> can also be used around dynamic components:
Source Reference
- Package:
@vue/runtime-dom - Implementation:
/packages/runtime-dom/src/components/Transition.ts:87 - Props Interface:
/packages/runtime-dom/src/components/Transition.ts:18-33
See Also
- TransitionGroup - For animating lists
- Guide: Transition - Detailed transition guide