--- title: Use Class-based Animations for Non-Enter/Leave Effects impact: LOW impactDescription: Class-based animations are simpler and more performant for elements that remain in the DOM type: best-practice tags: [vue3, animation, css, class-binding, state] --- # Use Class-based Animations for Non-Enter/Leave Effects **Impact: LOW** - For animations on elements that are not entering or leaving the DOM, use CSS class-based animations triggered by Vue's reactive state. This is simpler than `` and more appropriate for feedback animations like shake, pulse, or highlight effects. ## Task Checklist - [ ] Use class-based animations for elements staying in the DOM - [ ] Use `` only for enter/leave animations - [ ] Combine CSS animations with Vue's class bindings (`:class`) - [ ] Consider using `setTimeout` to auto-remove animation classes **When to Use Class-based Animations:** - User feedback (shake on error, pulse on success) - Attention-grabbing effects (highlight changes) - Hover/focus states that need more than CSS transitions - Any animation where the element stays mounted **When to Use Transition Component:** - Elements entering/leaving the DOM (v-if/v-show) - Route transitions - List item additions/removals ## Basic Pattern ```vue ``` ## Common Animation Patterns ### Pulse on Success ```vue ``` ### Highlight on Change ```vue ``` ### Bounce Attention ```vue ``` ## Using animationend Event Instead of `setTimeout`, use the `animationend` event for cleaner code: ```vue ``` ## Composable for Reusable Animations ```javascript // composables/useAnimation.js import { ref } from 'vue' export function useAnimation(duration = 500) { const isAnimating = ref(false) function trigger() { isAnimating.value = true setTimeout(() => { isAnimating.value = false }, duration) } return { isAnimating, trigger } } ``` ```vue ``` ## Reference - [Vue.js Animation Techniques - Class-based Animations](https://vuejs.org/guide/extras/animation.html#class-based-animations) - [CSS Animations MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations)