-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sibling selectors of components don't work on classes applying them #9093
Comments
Hey thanks for the report! So there's a few of things here:
Each class in We then copy each of the appropriate rules over for each matching class and replace the "candidate" portion of the selector. For instance with As such the expected output is something like this: /* Rules for section:nth-of-type(odd) */
/* Copy of rule .row.row-odd — added because @apply row */
section.row-odd:nth-of-type(odd) {
background: green;
}
/* Copy of rule .row.row-odd — added because @apply row-odd */
section.row:nth-of-type(odd) {
background: green;
}
/* Copy of rule .row.row-even — added because @apply row */
section.row-even:nth-of-type(odd) {
background: red;
}
/* Copy of rule .row-odd + .row-even — added because @apply row-odd */
section:nth-of-type(odd) + .row-even {
background: blue;
}
/* Copy of rule .row-even + .row-odd — added because @apply row-odd */
.row-even + section:nth-of-type(odd) {
background: yellow;
}
/* Rules for section:nth-of-type(even) */
/* Copy of rule .row.row-odd — added because @apply row */
section.row-odd:nth-of-type(even) {
background: green;
}
/* Copy of rule .row.row-even — added because @apply row */
section.row-even:nth-of-type(even) {
background: red;
}
/* Copy of rule .row.row-even — added because @apply row-even */
section.row:nth-of-type(even) {
background: red;
}
/* Copy of rule .row-odd + .row-even — added because @apply row-even */
.row-odd + section:nth-of-type(even) {
background: blue;
}
/* Copy of rule .row-even + .row-odd — added because @apply row-even */
section:nth-of-type(even) + .row-odd {
background: yellow;
} Having said that having We have a couple of in-depth comments about how |
Thank you for the quick response and detailed explanation!
...but I'd like to see:
I probably shouldn't have included Basically, if I were to write it as a test it'd be closer to: it('can produce selectors that replace multiple instances of the same class', async () => {
let config = {
content: [{ raw: html`<div class="foo-1 -foo-1 new-class"></div>` }],
plugins: [],
}
let input = css`
.foo + .bar {
color: blue;
}
.bar + .foo {
color: fuchsia;
}
header {
@apply foo;
}
main {
@apply foo bar;
}
footer {
@apply bar;
}
`
let result = await run(input, config)
console.log(result.css)
expect(result.css).toMatchFormattedCss(css`
.foo + .bar {
color: blue;
}
.bar + .foo {
color: fuchsia;
}
header + footer {
color: blue;
}
main + main {
color: blue;
color: fuchsia;
}
footer + header {
color: fuchsia;
}
`)
}) BTW, when I run the above code it seems to get stuck in a loop because of If what you said still applies, please let me know |
Ah yeah so @adamwathan and I just discussed this regarding my change targeting the same "candidate" in a selector and realized that it's not 100% accurate either so we're backing that bit out currently. Given: .foo + .foo { color: red; }
section { @apply foo; } We would need to end up with four rules for it to be correct: .foo + .foo { color: red; }
section + .foo { color: red; }
.foo + section { color: red; }
section + section { color: red; } As it turns out, for any number of classes, we'd end up with approximately 2^N rules that would have to be generated. For example the selector For your case specifically we'd also have to maintain a graph of how all the classes interact and are applied with one another. This would mean that earlier rules affect later ones and getting this right generally would be fairly difficult as the cascade — for the same specificity — last declaration wins. I believe in your case the correct generated code would be the following: .row-odd + .row-even { … }
section:nth-of-type(odd) + .row-even { … }
.row-odd + section:nth-of-type(even) { … }
section:nth-of-type(odd) + section:nth-of-type(even) { … } And, if you have any additional rules that target All in all — it is entirely possible this is out-of-scope for |
That sounds about right! Thing is, I understand this generates a lot of output rules, but the only reason someone would do this is if they really needed it.
That looks about correct! However... I'm probably oversimplifying, but couldn't they be merged with a comma selector?
If I add a rule to
Is it? Doesn't the current output feel 'odd' given my second TW Play example?
That makes sense. Please let me know if I can help somehow |
What version of Tailwind CSS are you using?
For example: v3.1.8
What build tool (or framework if it abstracts the build tool) are you using?
Tailwind Play
What browser are you using?
Chrome
What operating system are you using?
macOS
Reproduction URL
https://play.tailwindcss.com/Gd9jsdpkCi?file=css
Describe your issue
When applying custom components to selectors with a sibling operator, the output CSS does not apply to them properly. For example, with the following code
...should output something similar to:
...and yet it only outputs:
It also seems that when declaring composite rules (multiple classes, such as
.row.row-odd
) this breaks during compilation when applying it to a tag instead of a class, resulting in rules such as.rowsection
(with no separator, which makes no sense) as a result of the following code:Both behaviors are observable in the output of the Tailwind Play document I linked
The text was updated successfully, but these errors were encountered: