Open jangeroo opened 1 year ago
Lol, this one is fun, I can reproduce it, it looks like the layers are not working from imported files inside the app directory.
It will work if you move your theme.css file to the public directory, then import it
@import "/theme.css" layer(theme);
Just chipping in to share a similar experience, but in my case it looks like the statement doesn't even compile. Example:
import "react-toggle/style.css" layer(components);
Expected ';', got 'layer'
The above code does compile without the layer addition. This is a bit of a deal-breaker for working with CSS layers in Next.js, in particular when you organize your own custom CSS into layers and then import a npm package that has styles. As the styles of the npm package are not in any of your layers, they take a higher specificity than any of your layers, thus making layer order somewhat pointless.
The above syntax, if it would work, would solve that. It allows for the importing of external styles neatly into your layer order.
For now I worked around it by copying the external styles, putting them in my own styles folder and wrapping the external styles into a layer, like so:
@layer components {
... external styles here
}
The downside is of course that this breaks npm update.
For us, this was fixed by installing postcss-import, and adding it to postcss.config.js
as the first plugin in plugins
.
It also appears that any @layer
rule used to specify the priority/order of the layers in the @import
statements is placed/moved after the styles from the @import
s. this would force the layers into order of appearance priority instead of the order dictated by @layer
.
This problem is also fixed by @hipdev 's workaround so I'm unsure if this is a separate bug (that would only have a visible effect once this bug is resolved) or a symptom of the same problem.
I've been messing around with trying to get CSS Cascade layers working with NextJS, and found some interesting things of note. If you try to do @import url() layer(someLayer);
syntax, it appears that by default, NextJS bundles it like this:
@media layer(someLayer) {
// your css
}
Which is basically invalid CSS and the selectors are essentially black hole'd. One interesting finding I did have though, is using --turbo
in dev, appears to fix all these problems (although --turbo
appears to have a bunch of other potential issues with it, not sure if it's considered production ready, such as not allowing composes
in @layer {}
definitions or sometimes converting invalid CSS values into other valid but still not what you want CSS values which took a bit of debugging to suss out.
However, all that said, when I build the bundle for prod, the broken @media layer(someLayer) {}
syntax returns :(
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/next-js-css-import-layer-bug-h3j5tq
To Reproduce
global.css
) add@import './theme.css' layer(theme)
Current vs. Expected behavior
Expected behaviour: All styles from
theme.css
are applied.Actual behaviour: No styles from
theme.css
are applied.Verify canary release
Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure
Additional context
This bug appears in both the old pages router and app router. It does not happen with a bare-bones React application created with
created-react-app
, where importing css layers works as expected.