vue-loader icon indicating copy to clipboard operation
vue-loader copied to clipboard

Scoped style and slotted elements

Open mysticatea opened this issue 7 years ago • 14 comments

I have questions about <style scoped>, <slot> and ::slotted pseudo-element selectors.

  • Does not Vue.js support CSS scoping spec?
  • If so, does Vue.js have a plan to support CSS scoping spec?

For example to explain: gist@de98f89c48e16dea2230d13925d3b3e2.

I expected a blue box to exist inside of a red box because the CSS rule slot::slotted { } should apply to distributed elements. But actually, a red box existed inside of another red box bacause CSS rule div { } applied to distributed elements wrongly and slot::slotted { } was merely ignored.

mysticatea avatar Mar 12 '17 07:03 mysticatea

Thank you for your filling!

AFAIK, In the about slot::slotted { }, Vue.js (vue-loader) doesn't support (not implemented). We need to implement it. IMO, We should be supported it.

/cc @yyx990803 What do you think?

kazupon avatar Mar 12 '17 11:03 kazupon

We do not support ::slotted or >>> at the moment, but yes, we should.

yyx990803 avatar Mar 13 '17 06:03 yyx990803

Issue #113 says that 'Scoped CSS on slot content' fixed in vue-loader 9.x. But I cannot find any version description about that. And I try to define scoped style on child component when slot some element to parent element, it does work. Kind of wired. @yyx990803

FTAndy avatar Mar 14 '17 08:03 FTAndy

@FTAndy as of now slotted content is considered both part of the parent and the child so they get matched by scoped styles from both sides.

yyx990803 avatar Apr 06 '17 02:04 yyx990803

The double scope seems to work for unnamed slots, but named slots seem to only get the scope of the parent.

I'd expect there be no difference between named and unnamed. Will vue support named slots to be considered part of both parent and child?

RuneInBoots avatar May 18 '17 11:05 RuneInBoots

@RuneInBoots If you use multi scoped third party lib, and want to add style to child child scoped element. You could just remove 'scoped' and add specified classname to the parent element and use the classname to scoped css rules.

FTAndy avatar May 18 '17 11:05 FTAndy

We've been looking further into it and found out previous identified issue was incorrect.

We did identify the issue we are having, and reported it here: #820

RuneInBoots avatar May 18 '17 20:05 RuneInBoots

This doesn't work for me on either named or unnamed slots content. Only the parent component id is attached to the slot.

jwahdatehagh avatar Jul 27 '17 10:07 jwahdatehagh

I have a similar issue but with :after pseudo selector.

betacar avatar Nov 27 '17 16:11 betacar

@FTAndy as of now slotted content is considered both part of the parent and the child so they get matched by scoped styles from both sides.

@yyx990803 If slotted content and child element have the same class, their style will conflict.

caikan avatar Jan 02 '18 02:01 caikan

@yyx990803 as mentioned @caikan styles will conflict if scoped parent and scoped child will have same selector in their styles and this selector will be used in slot content. I think slotted content should be affected only by scoped parent styles, except slot root.

It's very confusing behavior.

For example we use scoped styles and this allow us to use simpler classnames. For example .submit-button instead of my-component-name__submit-button

But if I make component with .submit-button and slot, and then some parent will place .submit-button in slot too, .submit-button styles will be broken. Current behavior breaks isolation between parent-child - parent should not know about classnames of child, but for now I should check is child component have same classname or not.

reproduce link

Crazymax11 avatar Feb 28 '18 04:02 Crazymax11

I hate to be the "hey guys any progress on this" guy, but yeah, I am this guy now.

We are trying to keep everything neat and clean with components' scoped styles (A) while inserting them into this awesome, name-slotted layout (B). Guess one still cannot have both?

JStrebeyko avatar Oct 18 '19 10:10 JStrebeyko

Is it currently possible to style distributed elements (and/or their descendants) in Vue in some way?

trusktr avatar Aug 20 '20 20:08 trusktr

are we in any way able to apply the ::slotted component in vue? if not then is there any other way to style distributed elements?

Srijit16 avatar Mar 16 '22 07:03 Srijit16