1

I am working in Angular and use SCSS for my styles. I can declare a variable and reuse it later in SCSS like this: $color-accent: #30bdff; $color-accentLight: mix(white, $color-accent, 90%);

But what if the user want to change the 'color-accent' value ? What if this value should be a dynamic value? Of course I can use the 'setProperty' function in my typescript file like this: document.documentElement.style.setProperty('--dynamicValue', #30bdff); And in the SCSS : $color-accent: var(--dynamicValue);

Now if I want to directly use this value it will work. like this:

***.mainContent{ Color: $color-accent }


But my problem begins when I want to reuse this variable in a SCSS function like: min, max, darken, lighten... Because this function need to have a color as their input and doesn't except a variable which contains a var(--) value. I checked a lot, and I saw many similar questions, but could not find any answer of my problem. Am I forced to not use any SCSS functions, if I want to get the value dynamically?

tnx in advance for your help

I tried to pass a variable into the SCSS function, or even search for a SCSS function which convert string into Color object, but could not find any

2
  • I think this answer serves as a solution to your question: stackoverflow.com/a/55160217/20009330 Commented Feb 11, 2023 at 9:02
  • thank you for your response. In fact that will help a lot and 99% is exactly doing what I need. only this code should be written in typescript file I prefer to do them in SCSS file. but I know there is not possible. Therefore I use this approach. thank you .
    – Hamid
    Commented Feb 13, 2023 at 15:18

1 Answer 1

0

SCSS is compiled into CSS.

That means when you run ng build, your SCSS gets transformed into CSS.

At this moment, all functions are processed, all variables too, etc.

In the end, you end up with a compiled, STATIC CSS file.

So no, you won't be able to use SCSS functions like you wish to.

On the other hand, you can probably find JS librairies that can achieve the same result, use them, then use the result of it to style your component dynamically.

1
  • tnx for your quick answer :D Can you show me a sample how to achive it with JS library ?
    – Hamid
    Commented Feb 2, 2023 at 14:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.