Skip to content
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

[css-cascade] A way to escape the current cascade layer and render styles in the parent layer (@layer ..) #11010

Closed
benface opened this issue Oct 7, 2024 · 8 comments
Labels
Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. css-cascade-5

Comments

@benface
Copy link

benface commented Oct 7, 2024

My use case is a framework that lets you generate styles in a particular layer. For instance, Tailwind CSS v4 generates all the utility classes in a utilities layer (which I can customize). If I want some utilities to have lower precedence than others, I can wrap them in another layer since unlayered styles beat layered styles. But if I want some utilities to have higher precedence than others, I am forced to resort to specificity hacks (e.g. :is(#a, .my-actual-selector)). It would be nice if I could wrap them in @layer ..overrides and they would win because I would define overrides after utilities.

@flashymittens
Copy link

Wouldn’t that defeat the purpose of having layers in the first place?

@benface
Copy link
Author

benface commented Oct 8, 2024

@flashymittens – How so? In the example I gave, layers are still useful as a way to define precedence when we can't / don't want to rely on specificity.

/* assume this stylesheet is imported in the `utilities` layer */

.dark .bg-primary {
  background-color: red;
}

@layer ../overrides {
  [forced-bg="blue"] {
    background-color: blue;
  }
}
<body class="dark">
  <div class="bg-primary" forced-bg="blue">This should be blue</div>
</body>

(Of course this is a silly example, but hopefully you get what I mean.)

@romainmenke
Copy link
Member

I think the proper way would be for tailwind (as a style vendor) to organise its styles into a more specific cascade layer.

@layer utilities.tailwind {
  color: green;
}

Then you can write your own utilities as:

@layer utilities.my-styles {
  color: purple;
}

And organise your layers freely with:

@layer utilities.my-styles, utilities.tailwind;
@layer utilities.tailwind, utilities.my-styles;

@benface
Copy link
Author

benface commented Oct 8, 2024

Well, Tailwind generates the CSS for utilities (including the custom-defined ones) on demand based on config + usage in the markup. There's an almost unlimited number of potential utility classes, since they can be modified with any number of "variants" (e.g. hover:dark:bg-red-400) and can even have arbitrary values (e.g. bg-[#cfef40]). The only control Tailwind can possibly give us at that level is a way to wrap some utilities in a different layer (e.g. [@layer_defaults]:border-0), but they would still be nested in the main utilities layer. Hence this feature request, so I can do something like [@layer_../overrides]:border-0 (I would probably create a custom variant as opposed to an arbitrary one, so it might look more like overrides:border-0).

@benface
Copy link
Author

benface commented Oct 8, 2024

I see this feature request as a more precise !important. In a cascade layer, !important allows a declaration to beat a conflicting declaration in the current layer and in any layer defined after the current one. With this, we would be able to place whole rules in a specific layer outside the current one.

@mirisuzanne
Copy link
Contributor

It seems to me like this issue is tool-specific, and the solution should be provided by that tool. The ideal solution here is that you specify what layer each selector is in, instead of having a default that you need to break out of. That's already how things work, if a third party wasn't writing (and layering) all your layers for you. I'm not sure we can solve problems created by specific tools, unless there's some real good reason the tools themselves can't provide a solution.

Could this become a Tailwind syntax, rather than being added to CSS? It seems to me like that should be possible.

@benface
Copy link
Author

benface commented Oct 14, 2024

Thanks for the feedback everyone. I'm going to close this and hope that #6323 gets addressed at some point, as it will also provide a (less direct) solution to my use case (assuming Tailwind doesn't introduce some specific feature for it, which I think is more unlikely).

@benface benface closed this as completed Oct 14, 2024
@mirisuzanne mirisuzanne added the Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. label Oct 14, 2024
@mirisuzanne
Copy link
Contributor

Thanks @benface - feel free to reopen at some point if it seems like there are more use-cases we should be considering here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. css-cascade-5
Projects
None yet
Development

No branches or pull requests

5 participants