-
Notifications
You must be signed in to change notification settings - Fork 693
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
When using @import scope(), are the imported styles 'nested'? #11756
Comments
Well, I guess we don't have to, but then it wouldn't be "nested" with all that it currently implies, it would be something new. If the imported stylesheet is truly "nested", then even declarations would be allowed top level [1]. |
@import "foo.css" scope(.bar)
a { color: green; } Could be merged as: @sheet some-sheet-id {
a { color: green; }
}
@import some-sheet-id scope(.bar) This implies that tools can't bundle I can also see use cases for both behaviors: 1: A CSS author might be importing a 3rd party stylesheet and might want to scope it. 2: A CSS author might organize their files so that each component has their own stylesheet. Importing each with a |
It would be helpful to list out the alternatives explicitly, as I for one, am not 100% sure I understand it correctly. My understanding is that if the imported styles are not nested, it would require an explicit That said, there is utility in being able to "escape" the scope and specify root values as well. What if |
@LeaVerou your questions cover a lot of different ground, but let me see if I can clarify. Selectors inside an
The scope at-rule applies both scoping and nesting to selectors inside. There is not a way to 'escape the scope' in the first sense. If the subject shouldn't be inside the scope, then the selector doesn't belong in a scope rule. But in the second sense it works like any other nested selectors. By default, we assume an implied descendant combinator - but it can also be placed explicitly: @scope (article) {
h2 { /* implicit `:scope h2` */ }
main :scope h2 { /* explicit, as written */ }
} In that sense, you can have complex selectors that escape the scope to establish context. And yes, One way to implement scoped imports would be to apply only the scoping, but not the nesting. For sure we want to require all matched subjects are in-scope. So the question is: do we also imply an implicit descendant combinator to all selectors in the imported document, unless they explicitly contain either /* importer.css */
@import 'to-be-imported.css' scope(aside);
/* to-be-imported.css */
section h2 { … } What does above
If we want this to be consistent with the at-rule, we should apply both scoping and nesting. But the nesting changes the meaning of selectors, so it feels a bit more invasive. You would want to write the imported stylesheet with nesting in mind. It's not simply limiting the results to subjects that are in our desired scope. As a side note: if we do decide to nest imported style sheets, maybe we also would want to allow |
@mirisuzanne thank you for this explanation, this also makes it more clear where exactly the difference is noticeable. Is it then correct that any selector containing either I think it could be an acceptable tradeoff for bundlers to require authors to always add an explicit |
@romainmenke I don't think I understand what you are trying to do. I don't see what that achieves, and I'm not sure the logic is quite right. But I also think we need to agree on the most useful CSS behavior, before we spend too much time worrying about how to polyfill it? Currently, selectors at the top level of a CSS document are not 'relative' to anything. They can't, for example, start with a combinator. Nested selectors are relative to the parent selector. Relative selectors often start with a combinator: > h2 { /* not a valid selector */ }
main {
> h2 { /* nesting makes it valid, with implied `&` */ }
} The first selector here is currently invalid if we apply the stylesheet directly. Would it suddenly become valid when the stylesheet is imported with nesting applied? |
I see, thanks for the explanation. I still maintain that to the extent possible, rules should behave identically to being nested in an Even consistency aside, that seems like the clearer mental model. If authors desire the other behavior where the rest of the selector is essentially a filter, they can make their intent clear by rewriting
Like @mirisuzanne, I also don't understand what purpose this serves. Additionally, I see being able to repurpose existing stylesheets as an important use case for this feature, so anything that requires writing them differently eliminates this.
This seems orthogonal and likely warrants a new issue: why aren't they valid? We resolved a while ago that outside of nesting, Another orthogonal issue is that many stylesheets define CSS variables under |
This both seem like good issues to open, and potentially consider as part of a scope break-out session. |
In #11237 @andruud's draft language for
@import scope()
includes the following note:This behavior wasn't discussed is the original issue #7348
There has been some discussion on the PR, but this seems like a better place to have the full conversation and come to a resolution. Applying scope without nesting would be a departure from the behavior of
@scope
rules.@romainmenke points out that the difference makes it hard for tooling to merge files, and wrap the imported sheet in a scope block. And Anders says:
The text was updated successfully, but these errors were encountered: