VuReact is a compiler toolchain for migrating from Vue to React — and for writing React with Vue 3 syntax. In this article, we dive straight into the core: how Vue's built-in <Transition> component is compiled into React code by VuReact.
Before We Start
To keep the examples easy to read, this article follows two simple conventions:
- All Vue and React snippets focus on core logic only, with full component wrappers and unrelated configuration omitted.
- The discussion assumes you are already familiar with Vue 3's
<Transition>component usage.
Compilation Mapping
Transition: Single element transition animation
<Transition> is Vue's built-in component for adding enter/leave transition animations to a single element or component.
Basic Transition usage
- Vue
<template>
<Transition name="fade">
<div v-if="show">Content</div>
</Transition>
</template>
- Compiled React
import { Transition } from '@vureact/runtime-core';
<Transition name="fade">
{show ? <div>Content</div> : null}
</Transition>
As the example shows, Vue's <Transition> component is compiled into the Transition adapter component provided by VuReact Runtime — think of it as "Vue's Transition for React".
The key characteristics of this compilation approach are:
-
Semantic consistency: Fully simulates Vue
<Transition>behavior by implementing transition animations - CSS class names: Automatically generates and applies transition-related CSS class names
- Conditional rendering: Supports transition effects for conditionally rendered elements
- React integration: Implements Vue's transition semantics in the React environment
Corresponding CSS styles:
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
.fade-enter-active {
opacity: 1;
transition: opacity 0.5s ease;
}
.fade-leave-active {
opacity: 0;
transition: opacity 0.5s ease;
}
Transition mode control
The mode attribute controls the order of新旧 content switching, preventing enter and leave animations from running simultaneously.
- Vue
<template>
<Transition name="slide-fade" mode="out-in">
<button v-if="state" key="on">On</button>
<button v-else key="off">Off</button>
</Transition>
</template>
- Compiled React
<Transition name="slide-fade" mode="out-in">
{state ? <button key="on">On</button> : <button key="off">Off</button>}
</Transition>
Transition modes:
- out-in: The leave animation finishes first, then the enter animation plays
- in-out: The enter animation finishes first, then the leave animation plays
- Default: Enter and leave animations play simultaneously
The importance of key:
- Node identification: Helps Transition identify different elements
- Animation trigger: A change in key triggers the transition animation
- State preservation: Ensures animations are correctly applied to the corresponding element
-
Automatic multi-node key handling: When no explicit
keyis provided, VuReact automatically generates a random identifier to ensure proper transition animation triggering
Custom transition class names
Beyond using name to auto-generate class names, you can also directly specify custom transition class names for easy integration with third-party animation libraries.
- Vue
<template>
<Transition
enter-active-class="animate__animated animate__fadeIn"
leave-active-class="animate__animated animate__fadeOut"
>
<div v-if="show">Custom animation</div>
</Transition>
</template>
- Compiled React
<Transition
enterActiveClass="animate__animated animate__fadeIn"
leaveActiveClass="animate__animated animate__fadeOut"
>
{show ? <div>Custom animation</div> : null}
</Transition>
Custom class name attributes:
- enterFromClass: Class name at the start of enter
- enterActiveClass: Class name during the active enter phase
- enterToClass: Class name at the end of enter
- leaveFromClass: Class name at the start of leave
- leaveActiveClass: Class name during the active leave phase
- leaveToClass: Class name at the end of leave
JavaScript hook functions
Transition supports JavaScript hook functions for executing custom logic at different stages of the animation.
- Vue
<template>
<Transition
@before-enter="onBeforeEnter"
@enter="onEnter"
@after-enter="onAfterEnter"
@leave="onLeave"
>
<div v-if="show">JS controlled animation</div>
</Transition>
</template>
- Compiled React
<Transition
onBeforeEnter={onBeforeEnter}
onEnter={onEnter}
onAfterEnter={onAfterEnter}
onLeave={onLeave}
>
{show ? <div>JS controlled animation</div> : null}
</Transition>
JavaScript hooks:
- onBeforeEnter: Triggered before the enter animation starts
- onEnter: Triggered during the enter animation
- onAfterEnter: Triggered after the enter animation completes
- onLeave: Triggered during the leave animation
- onAfterLeave: Triggered after the leave animation completes
Transition duration control
The duration attribute explicitly specifies the transition duration.
- Vue
<template>
<Transition :duration="800">
<div v-if="show">Specified duration animation</div>
</Transition>
</template>
- Compiled React
<Transition duration={800}>
{show ? <div>Specified duration animation</div> : null}
</Transition>
Duration configuration:
- Number: Sets a uniform duration for both enter and leave
- Object: Sets separate durations for enter and leave
<Transition duration={{ enter: 300, leave: 500 }}>
{show ? <div>Content</div> : null}
</Transition>
Compilation strategy summary
VuReact's Transition compilation strategy demonstrates a complete transition animation conversion capability:
-
Direct component mapping: Maps Vue
<Transition>directly to VuReact's<Transition> -
Full attribute support: Supports all attributes including
name,mode, custom class names, hook functions, etc. - CSS class name generation: Automatically generates and applies transition-related CSS class names
- JavaScript integration: Supports animation control through JS hooks
Core features:
-
Automatic class names: Auto-generates CSS transition class names via the
nameattribute -
Mode control: Controls the order of enter/leave animations via
mode - Custom class names: Directly specifies transition class names, supporting third-party animation libraries
- JS hooks: Executes JavaScript logic at different animation stages
- Duration control: Precisely controls the duration of transition animations
Important notes:
-
Single child node:
<Transition>can only have one direct child node -
Key requirement: It is recommended to provide a stable
keywhen switching between different elements -
CSS requirement: The transition appearance must be set in
*-enter-activeand*-leave-active - Performance considerations: Complex animations may affect performance; use them wisely
VuReact's compilation strategy ensures a smooth migration from Vue to React. Developers do not need to manually implement transition animation logic. The compiled code preserves Vue's transition semantics and animation effects while following React's component design patterns, keeping the migrated application fully capable of transition animations.
Top comments (0)