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-group.md
1---2title: TransitionGroup Component Best Practices3impact: MEDIUM4impactDescription: TransitionGroup animates list items; missing keys or misuse leads to broken list transitions5type: best-practice6tags: [vue3, transition-group, animation, lists, keys]7---89# TransitionGroup Component Best Practices1011**Impact: MEDIUM** - `<TransitionGroup>` animates lists of items entering, leaving, and moving. Use it for `v-for` lists or dynamic collections where individual items change over time.1213## Task List1415- Use `<TransitionGroup>` only for lists and repeated items16- Provide unique, stable keys for every direct child17- Use `tag` when you need semantic or layout wrappers18- Avoid the `mode` prop (not supported)19- Use JavaScript hooks for staggered effects2021## Use TransitionGroup for Lists2223`<TransitionGroup>` is designed for list items. Use `tag` to control the wrapper element when needed.2425**BAD:**26```vue27<template>28<TransitionGroup name="fade">29<ComponentA />30<ComponentB />31</TransitionGroup>32</template>33```3435**GOOD:**36```vue37<template>38<TransitionGroup name="list" tag="ul">39<li v-for="item in items" :key="item.id">40{{ item.name }}41</li>42</TransitionGroup>43</template>44```4546## Always Provide Stable Keys4748Keys are required. Without stable keys, Vue cannot track item positions and animations break.4950**BAD:**51```vue52<template>53<TransitionGroup name="list" tag="ul">54<li v-for="(item, index) in items" :key="index">55{{ item.name }}56</li>57</TransitionGroup>58</template>59```6061**GOOD:**62```vue63<template>64<TransitionGroup name="list" tag="ul">65<li v-for="item in items" :key="item.id">66{{ item.name }}67</li>68</TransitionGroup>69</template>70```7172## Do Not Use `mode` on TransitionGroup7374`mode` is only for `<Transition>` because it swaps a single element. Use `<Transition>` if you need in/out sequencing.7576**BAD:**77```vue78<template>79<TransitionGroup name="list" tag="div" mode="out-in">80<div v-for="item in items" :key="item.id">{{ item.name }}</div>81</TransitionGroup>82</template>83```8485**GOOD:**86```vue87<template>88<Transition name="fade" mode="out-in">89<component :is="currentView" :key="currentView" />90</Transition>91</template>92```9394## Stagger List Animations with Data Attributes9596For cascading list animations, pass the index to JavaScript hooks and compute delay per item.9798```vue99<template>100<TransitionGroup101tag="ul"102:css="false"103@before-enter="onBeforeEnter"104@enter="onEnter"105>106<li v-for="(item, index) in items" :key="item.id" :data-index="index">107{{ item.name }}108</li>109</TransitionGroup>110</template>111112<script setup>113function onBeforeEnter(el) {114el.style.opacity = 0115el.style.transform = 'translateY(12px)'116}117118function onEnter(el, done) {119const delay = Number(el.dataset.index) * 80120setTimeout(() => {121el.style.transition = 'all 0.25s ease'122el.style.opacity = 1123el.style.transform = 'translateY(0)'124setTimeout(done, 250)125}, delay)126}127</script>128```129