-
Notifications
You must be signed in to change notification settings - Fork 708
[css-nesting] simpler embedding of media queries #5805
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
Syntactically speaking, the suggested idea means that
Sebastian |
#1 is true, and intended, but I don't think #2 would follow. The closing brace of a media query or a rule within a rule tells the parser that it can start looking for property declarations (or ampersands or @ signs) again. So, this is also not ambiguous as to whether to parse as property or rule:
|
Note that I extracted the second point from your statement:
Whether or not to allow property definitions anywhere within the @media-rule needs to be discussed. Sebastian |
Perhaps I left something out of my parsing description, but #2 was not intended. |
Hm, yes this should be possible. It's the exact same parsing context as the interior of the style rule itself. |
Nice, I've been doing this but it'd be nice not to have to 👍🏻 a {
...
@media (prefers-reduced-motion: no-preference) { & {
transition: outline-offset .25s ease;
}}
} vs a {
...
@media (prefers-reduced-motion: no-preference) {
transition: outline-offset .25s ease;
}
} |
The PostCSS preprocessor has also allowed this simpler syntax for a long time through the PostCSS Nesting plugin. It is automatically enabled using PostCSS Preset Env. This syntax is familiar to many developers already. It's more readable and feels more natural than having the extra ampersand and braces. |
The spec has this for how to embed a MQ in a rule:
But the unadorned ampersand and extra set of braces seems extraneous. It is incredibly useful, and much simpler, in SCSS to just write it like this for the commonest uses:
In fact, embedded the MQ in the rule like that, without extra braces, is one of my favorite things about writing in SCSS. It just feels natural and familiar. Even in old vanilla CSS, I can't tell you the number of times I've forgotten to include the selector inside the MQ, when it was just an MQ for a single rule, written right after the bare rule. When the MQ is inside the rule, instead for the other was around, it is a joy to not have to write another selector and braces inside it.
You don't have the garden path problem here. If the first non-white-space character after the opening brace of the nested MQ (or after a closing brace of a nested rule within that MQ) is not an ampersand or @ sign, then it is the beginning of a property for a declaration.
I think there is nothing ambiguous about the following, and it is easier to write and read:
So basically, the
& {
and}
parts are assumed, as a shortcut for when you don't need any more than that, when you already have the braces from the nested @ rule. This should hold true for other nested @ rules too, such as@supports
, etc.The text was updated successfully, but these errors were encountered: