4

I am figuring out how to apply styling to a reusable Angular component from within a client project. See Theme/style Angular 2 reusable component libraries => look for "follow up".

I now have one issue remaining:

I try to use a SCSS variable inside a CSS variable, but that CSS variable does not seem to be defined:

$primary-color: #666666;

// styling reusable components
:root {
  --ang-ux-primary-color: $primary-color;
}

In other component (in other component library, built separately):

$primary-color: var(--ang-ux-primary-color, #5FB0FD);

$primary-color seems to be empty... not #666666, neither #5FB0FD.

2
  • Changed, thanks :-) Commented May 25, 2018 at 17:18
  • 1
    There is no such var function in sass. In your case the value of $primary will be just that: var(--ang-ux-primary-color, #5FB0FD). Does the assigned styles fail to render the $primary-color? What is the css output? Commented May 25, 2018 at 18:42

1 Answer 1

4

To convert a Sass variable to a CSS variable, you need to interpolate it:

--ang-ux-primary-color: #{$primary-color};

Once you've converted your Sass variable into CSS, there's no getting it back. Therefore, in your other component you should maybe stick with pure CSS. The following will use #5FBOFD if --ang-ux-primary-color is undefined.

--ang-ux-primary-color: var(--ang-ux-primary-color, #5FB0FD);
Sign up to request clarification or add additional context in comments.

7 Comments

No, thats not a good answer. The output with or without interpolation is the same. And this does not answer the question.
I fleshed out my answer to address the second part of the poster's question. As to the interpolation, I tried it out on CodePen and it is required.
good but the first part is still wrong. You don’t need to interpolate variables if you use the variables as properties values. Just use the variable name: $primary.
@muecas, so you say my solution (--ang-ux-primary-color: $primary-color;) should have to work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.