question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use CSS variables or SCSS variables based on CSS variables in module configurations

See original GitHub issue

If one would like to use dynamic variables the best practice is to have them as CSS variables and then use them as values for SCSS variables, the following example works fine. If the --primary variables is manipulated by JavaScript the header’s background color changes.

:root {
--primary: red;
}

$primary: var(--primary);

header {
background-color: $primary;
}

Although if one thinks in modular it’s better to multiple files and module configurations but it just doesn’t work the way it should. If this is get compiled, fails immediately even if using SCSS variables is skipped and only CSS variables are used:

Colors partial

$primary: var(--primary);

Header partial

$header-bg: grey !default;
header {
background-color: $header-bg;
}

Body style

@use 'colors' as colors;
@use 'header' as header with {
$header-bg: colors.$primary;
}

Main style

:root {
--primary: red;
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stofcommented, Sep 21, 2021

Well, Sass color functions expect to get colors to be able to change them. var(--primary) is not a Sass color. It is an unquoted string. So it cannot be changed by color.change. that’s totally expected.

0reactions
petervatlercommented, Sep 21, 2021

Well, Sass color functions expect to get colors to be able to change them. var(--primary) is not a Sass color. It is an unquoted string. So it cannot be changed by color.change. that’s totally expected.

Oh, thank you for the info, I didn’t know this. I use a UI framework based on the pattern above and wanted to implement dynamic color theme change in runtime.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use a css variable from scss file in node_modules
CSS variables are dynamic at run time and can't be transformed by SASS at compile time. The SASS compiler is picking it up...
Read more >
Variables - Sass
CSS variables can have different values for different elements, but Sass variables only have one value at a time. Sass variables are imperative,...
Read more >
Using CSS custom properties (variables) - MDN Web Docs
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific ...
Read more >
How do I use css variables with css modules in Next.JS #12147
1.Go to the styles folder and create a file roots.css · 2. Put all your css variables. · 3. Go to pages/_app.js and...
Read more >
Getting JavaScript to Talk to CSS and Sass
We can also retrieve CSS variables using getComputedStyle in JavaScript. ... that are capable of importing and translating Sass modules.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found