--- title: Avoid Scoped Styles in Reusable Transition Components impact: MEDIUM impactDescription: Scoped styles in transition wrapper components won't apply to slotted content, breaking the transition animation type: gotcha tags: [vue3, transition, scoped-css, slot, reusable-component] --- # Avoid Scoped Styles in Reusable Transition Components **Impact: MEDIUM** - When creating reusable transition wrapper components, using ` ``` ```vue ``` **Correct Code:** ```vue ``` ## Alternative: Use Unique Prefixed Class Names To avoid global style conflicts, use distinctive prefixes: ```vue ``` ## Alternative: CSS Modules with :global() ```vue ``` ## Alternative: Custom Transition Classes Use the custom class props to apply scoped classes: ```vue ``` ## Complete Reusable Transition Component Example ```vue ``` Usage: ```vue ``` ## Why This Happens Vue's scoped styles work by adding a unique data attribute (e.g., `data-v-7ba5bd90`) to elements and selectors: ```css /* What you write */ .my-fade-enter-active { ... } /* What Vue generates (scoped) */ .my-fade-enter-active[data-v-7ba5bd90] { ... } ``` Slotted content comes from the parent component and gets the parent's data attribute, not the transition component's attribute. So the selectors never match. ## Reference - [Vue.js Reusable Transitions](https://vuejs.org/guide/built-ins/transition.html#reusable-transitions) - [Vue.js Scoped CSS](https://vuejs.org/api/sfc-css-features.html#scoped-css)