-
Notifications
You must be signed in to change notification settings - Fork 711
[css-nesting] support nesting at-rules #2891
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
Comments
Thanks for starting this discussion as a proper issue. I agree that it often improves readability and reduces duplication to nest media queries inside selectors. Same, too, for I also agree that it makes sense to move forward with this at the same time as the rest of the nesting proposal, as most of the parsing changes that will be needed could be implemented together. The thing that is different about nested @-rules is that the interior content of the rule changes. Instead of having full style rules with selectors inside the media query, we would only expect property declarations, because the selector would come from the outer context. This therefore affects not only parsing, but also the CSSOM model for the conditional rules. Do we define new CSSOM models for the nested rules? Or do we keep them as Of course, how this question is addressed will be tied to how the CSSOM handles the |
Now that the css-nesting spec draft is listed on the w3c site, here's the updated URL for Ameila’s CSSOM comment above: https://drafts.csswg.org/css-nesting-1/#cssom
Excellent point! I've been pondering this problem for several days since I read your comment. Section 3 of the conditional spec is the part of the spec covering what goes in @-rules:
I think we can actually leave this part of the spec unchanged if we instead alter section 2 and section 4. In section 2 of the conditional spec, we would add something like this:
We would also need to change section 4 of the conditional spec from:
to:
This still leaves a mismatch between
It sounds like my verbose solution above is the same as what you are suggesting as a solution in CSSOM. Yes, we keep them as I should point out I only just started playing with CSSOM today using this codepen I made based on this MDN article on modifying a stylesheet rule with CSSOM. However, the hands-on experience made it much easier to understand the CSSOM than by just reading; I recommend it for other new-to-CSSOM people. |
Conditional rules nesting is now defined, but Browsers currently do not support nested rules, but I hope the following exemple is syntactically correct: .selector {
& .child {
color: green;
}
@media all {
color: red;
}
} const { styleSheets: [{ cssRules: [nestedRule, mediaRule] }] } = document
console.log(nestedRule.style.color) // green
console.log(mediaRule.style) // undefined
console.log(el.style.color) // red EDIT: I realize just now that it is trickier than I thought. /* top level */
@media {
color: red; /* Ignored because invalid */
.selector {
color: green;
}
} console.log(document.styleSheets[0].cssRules[0].style) // ?? Side question How do you know when/if a spec is ready for testing, to receive implementation experience, before moving to CR? I'm not sure that CSS Nesting is ready for testing. This is the reason why I did not create a new issue for this. If it is not ready, my comment does not matter so please forgive me for the noise. |
Closing, since we ended up doing this (https://www.w3.org/TR/css-nesting-1/#conditionals). |
There are situations when nesting at-rules is more intuitive than spreading rule selectors.
Consider the following CSS for a typical website ”top bar”:
In this example, it is easier to scan at-rules nested within style rules, rather than the other way around. The occasional side-effect of brevity (due to the lack of selector repetition) further improves readability:
As at-rule nesting is such a common feature in CSS Preprocessors, is is often conceptually bundled with “nesting” in general. This is not the case in the current css-nesting proposal, which is why I’ve created this separate issue.
I’m not sure how at-rule nesting would be added to the specs. It may (only?) require updates to css-syntax and css-conditional (citation). Now seems like a good time to discuss this, as similar changes may be needed to support the
@nest
rule.The text was updated successfully, but these errors were encountered: