SCSS @use with @layer.

So I'm setting up a project and I'm looking to use @layer to implement ITCSS. I've worked with it before, but it's been a while and I can't find an example project implementing it. I want to have a style.scss file that combines a bunch of my ITCSS folders into the final CSS file:
@layer base, components, utilities

@layer components {
@use 'components'
}
@layer base, components, utilities

@layer components {
@use 'components'
}
However the example provided above is not a correct usage of this technique. It keeps shouting at me @use should always live at the highest lines in my file. Inside my SCSS folders, in this example components, there is usually a file called index.scss and some component files called _component-x.scss _component-y.scss. (These are just examples). I can't quite figure out where I am to declare @layer components within this structure. Should I define it at the component file level? Should it live in index.scss which gives me the same issue as in my style.scss. Somehow it feels a bit redundant and error prone to have to remember to wrap the code inside every _component-{x}.scss in an @layer block. If that's what's needed I'll do it, but I'd like to know if there's a better way. Hope my question is clear 😅
3 Replies
Wouter Schaap
Wouter Schaap•15mo ago
Aaand silence 🙂
Kevin Powell
Kevin Powell•15mo ago
We have to be a little more verbose to get it to work
@use 'sass:meta';

@layer base, components, utilities

@layer components {
@include meta.load-css('components');
}
@use 'sass:meta';

@layer base, components, utilities

@layer components {
@include meta.load-css('components');
}
using @include meta.load-css is almost the exact same thing as @use, but you can use it anywhere instead of only at the top. It also just loads in the css, so you can't use that, and then use a function that's inside that file, but you aren't looking to do that anyway, so this should work fine. Also, if you're layers all share the same name as the files themselves, you could probably make a loop that does it all too... not sure if it's worth the effort if you only have like 4 or 5 of them though 😅
Wouter Schaap
Wouter Schaap•15mo ago
@kevinpowell thnx, nah want to keep things legible, found that unless I generate things that are prone to change often I rather write it out.
Want results from more Discord servers?
Add your server