Search code examples
csssasscss-variables

Accessing custom properties in SASS modules


So, let's say I have a SASS module _module.scss that declares some css variables aka custom properties. When I now load this module in another SASS styleshee, let's call it main.scss, I have access to all SASS-variables, mixins and also rules, but not the custom properties inside of it? Am I missing something?

Example files:

//_module.scss
:root {
   --some-variable: red;
}
// main.scss
@use 'module';

div {
   background-color: var(module.--some-variable); // won't work, private property because of leading '-'
   background-color: module.var(--some-variable); // won't work, would have been horrible syntax as well
}

I could use @import but that is discouraged and deprecated (see SASS Documentation). I've tried including the variables in a pure css file module.css, which compiled but didn't declare any custom properties at runtime as it directly translates the @use 'module' from my SASS file to the exact same in CSS - which browsers don't understand obviously. It should just more or less copy the content of a pure css file but it doesn't. Sure, I could try writing mixins in my modules that set the variables but that's just a workaround.

Am I missing something? Do I really need to write a mixin, that sets the variables and needs to be loaded? This makes the use of custom properties within modules pretty cumbersome.

EDIT

Forgot to mention, that background-color: var(--some-variable) doesn't work either even though it should according to the documentation, since rules should just be applied directly without scoping.


Solution

  • Ugh. The issue is most definitely the fact that my VS Code extension uses LibSass and not Dart Sass. Therefore @use is not yet supported in most environments. The documentation should most definitely be more explicit about this especially when warning about the use of @import.

    Since I know it works with @import the issue is resolved though I'd love to see the module system being included in LibSass as well.

    tl;dr

    Do no use @use if you're not absolutely certain that you use Dart Sass!