Unexpected Scoped Styles Application to Slotted Components
Vue version
3be4e3c
Link to minimal reproduction
Steps to reproduce
- Create a component with slot -
ComponentWithSlot.vue
- Create another component with root element having a specific scoped class (for example
repeating-class-name
). Apply some scoped styles to this class. -SlottedComponent.vue
- In yet another component apply different scoped styles to class
repeating-class-name
, useComponentWithSlot
and passSlottedComponent
as a slot intoComponentWithSlot
. -App.vue
- Check which styles are applied to
SlottedComponent
s root element with classrepeating-class-name
.
What is expected?
SlottedComponent
s root element is styled with scoped styles applied to it in SlottedComponent.vue
file
What is actually happening?
SlottedComponent
s root element is styled with scoped styles applied to it in App.vue
file
System Info
No response
Any additional comments?
I see in docs https://vuejs.org/api/sfc-css-features.html#child-component-root-elements that it says:
But in this case I didn't apply the class in parent element.
Due to this issue it's easy to unintentionally apply styles defined in parent element to its slotted children root nodes.
<SlottedComponent>
is in the template of App.vue
. Therefore, it's receiving App's scoped style attribute, just like any normal element in App.vue
- regardless of wether you put that into another component's slot, or not.
So this is working as designed - the quoted docs apply to this scenario. We may revisit this documented effect of affecting root notes in discussions next major, as we do understand it can be a footgun sometimes but so far, for now, this is not a bug and is working as intended - changing it would be a breaking change.