Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Enforces Vue 3 Composition API best practices with script setup, TypeScript, Pinia, and Vite.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/component-transition.md
1---2title: Transition Component Best Practices3impact: MEDIUM4impactDescription: Transition animates a single element or component; incorrect structure or keys prevent animations5type: best-practice6tags: [vue3, transition, animation, performance, keys]7---89# Transition Component Best Practices1011**Impact: MEDIUM** - `<Transition>` animates entering/leaving of a single element or component. It is ideal for toggling UI states, swapping views, or animating one component at a time.1213## Task List1415- Wrap a single element or component inside `<Transition>`16- Provide a `key` when switching between same element types17- Use `mode="out-in"` when you need sequential swaps18- Prefer `transform` and `opacity` for smooth animations1920## Use Transition for a Single Root Element2122`<Transition>` only supports one direct child. Wrap multiple nodes in a single element or component.2324**BAD:**25```vue26<template>27<Transition name="fade">28<h3>Title</h3>29<p>Description</p>30</Transition>31</template>32```3334**GOOD:**35```vue36<template>37<Transition name="fade">38<div>39<h3>Title</h3>40<p>Description</p>41</div>42</Transition>43</template>44```4546## Force Transitions Between Same Element Types4748Vue reuses the same DOM element when the tag type does not change. Add `key` so Vue treats it as a new element and triggers enter/leave.4950**BAD:**51```vue52<template>53<Transition name="fade">54<p v-if="isActive">Active</p>55<p v-else>Inactive</p>56</Transition>57</template>58```5960**GOOD:**61```vue62<template>63<Transition name="fade" mode="out-in">64<p v-if="isActive" key="active">Active</p>65<p v-else key="inactive">Inactive</p>66</Transition>67</template>68```6970## Use `mode` to Avoid Overlap During Swaps7172When swapping components or views, use `mode="out-in"` to prevent both from being visible at the same time.7374**BAD:**75```vue76<template>77<Transition name="fade">78<component :is="currentView" />79</Transition>80</template>81```8283**GOOD:**84```vue85<template>86<Transition name="fade" mode="out-in">87<component :is="currentView" :key="currentView" />88</Transition>89</template>90```9192## Animate `transform` and `opacity` for Performance9394Avoid layout-triggering properties such as `height`, `margin`, or `top`. Use `transform` and `opacity` for smooth, GPU-friendly transitions.9596**BAD:**97```css98.slide-enter-active,99.slide-leave-active {100transition: height 0.3s ease;101}102103.slide-enter-from,104.slide-leave-to {105height: 0;106}107```108109**GOOD:**110```css111.slide-enter-active,112.slide-leave-active {113transition: transform 0.3s ease, opacity 0.3s ease;114}115116.slide-enter-from {117transform: translateX(-12px);118opacity: 0;119}120121.slide-leave-to {122transform: translateX(12px);123opacity: 0;124}125```126