styled-components / styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
https://styled-components.com
MIT License
40.11k stars 2.48k forks source link

Failed to parse `&` multiple times in `:is() :where() :has()` pseudo-class selector #4279

Open otomad opened 2 months ago

otomad commented 2 months ago

Environment

System:

Reproduction

https://stackblitz.com/edit/styled-components-example-bvz5fl?file=App.js

Steps to reproduce

When write & in :is() CSS pseudo-class selector more than twice, and then write any nested child selector, styled component will not parse & to the parent selector. Same bug applies in :where() and :has().

Example CSS:

:is(&:hover, .parent:hover &) .child {
    color: red;

    .grandchild {
        color: blue;
    }
}

Expected Behavior

:is(.componentId:hover, .parent:hover .componentId) .child {
    color: red;
}

:is(.componentId:hover, .parent:hover .componentId) .child .grandchild {
    color: blue;
}

Actual Behavior

:is(.componentId:hover, .parent:hover .componentId) .child {
    color: red;
}

:is(.componentId:hover, .parent:hover &) .child .grandchild {
    color: blue;
}

Additional

  1. SCSS/Sass will parse it correctly.
  2. The reproduce shows that, expect when hovering over both parent element and current compoent element, the grandchild color will be blue. But actually, the grandchild color will be blue only when hovering over current compoent element, and be red when hovering over parent element.