Subscribe on changes!

scoped css class applied to child component

avatar
Dec 7th 2023

Vue version

3.2.45

Link to minimal reproduction

https://play.vuejs.org/#eNqdUstugzAQ/BXLl1wiUJVbSiO1FYf20FZtjlyI2YATYyPbpFRR/r1rAwl5NKoiIWHPPjyzO1v6WFXBpgY6pZFhmleWGLB1NUskLyulLXlW+JcgbSygxB9ZalWSURCeBlybUSKjsO2DHfBioaxEagFvhETF3SxuUkQgCvHssdM2HkU845vZvOCG4GcLIEYoG4UO9WVnzyMchYPn6Jhaw5Rc8jxYGSVR4dZVJpRhJReg3yvLlTQJnRIfcbFUCPX96jGraxj3OCuArS/gK9M4LKEfGgzoDSR0H7OpzsG24fjrDRo874OlymqB2VeCn2CUqB3HNu2plhnSHuR5ti9+T1zmcxM3FqTpRTmiLnPn8xOK+3FD+0v6ge4kmPi6RO5wipf2fMUuBRfZmVUG4D9sgksmTKTGPLhlSZty6WR3zhiawq3Scws61wxf6vIRdd4Je2MdsenM1NrqyD94NfZHoPGYqiDzSLBn045tkbJ1rhXuZUo0ZDgvXxf6wtaDp9Ld5G5W63r1Sg+abqbPlFB6Shaihvtz7rtf/d1nhA==

Steps to reproduce

  • Pass to other component

What is expected?

css to be scoped correctly (not leaking into child)

What is actually happening?

Child has an attribute data-v-5275d7b8 coming from the parent. Because the child also uses class="container" the css rule leaks into the child

System Info

No response

Any additional comments?

No response

avatar
Dec 7th 2023

Guess i misunderstood that. Can i turn off that feature with some feature flag of some kind? It seems unnecessary because things like :deep() exist.

avatar
Dec 7th 2023

i mean now i have to somehow (lol) make sure, no component uses the same class for their root node, if they include the other, which is hard to guarantee when you have hundreds of components. I would rather turn off that feature (and use :deep()) instead of having to spend hours debugging non obvious behavior ...

avatar
Dec 7th 2023

https://vuejs.org/guide/components/attrs.html#class-and-style-merging

This feature cannot be turned off, this is related to class inheritance. If we use a class within a component, it will be bound to the root element of the child component. It can still utilize styles scoped in the parent component.

<template>
  <div>
    This is the component.
    <ChildElement class="container">
      <slot />
    </ChildElement>
  </div>
</template>

In this situation, since the style will be applied to the child components, it is similar to your use case.

Solution:

  1. You can use different class names to achieve style isolation.
  2. Wrap the root element of the child component with a div.
avatar
Dec 7th 2023

thx for taking the time!