27

In my angular project, I have some css variables defined in top level styles.scss file like this. I use these variable at many places to keep the whole theme consistent.

:root {
  --theme-color-1: #f7f7f7;
  --theme-color-2: #ec4d3b;
  --theme-color-3: #ffc107;
  --theme-color-4: #686250;

  --font-weight: 300
}

How can I update values of these variables dynamically from app.component.ts ? And What is the clean way to do this in angular ?

2

3 Answers 3

34

You can update them using

 document.documentElement.style.setProperty('--theme-color-1', '#fff');

If u want to update many values, then create a object

 this.styles = [
      { name: 'primary-dark-5', value: "#111" },
      { name: 'primary-dark-7_5', value: "#fff" },
    ];

 this.styles.forEach(data => {
      document.documentElement.style.setProperty(`--${data.name}`, data.value);
 });

The main thing here is document.documentElement.style.setProperty. This line allows you to access the root element (HTML tag) and assigns/overrides the style values.

Note that the names of the variables should match at both places(css and js files)


if you don't want to use document API, then you can use inline styles on HTML tag directly

    const styleObject = {};

    this.styles.forEach(data => {
      styleObject[`--${data.name}`] = data.value;
    });

Then In your template file using ngStyle (https://angular.io/api/common/NgStyle)

Set a collection of style values using an expression that returns key-value pairs.

<some-element [ngStyle]="objExp">...</some-element>
<html [ngStyle]="styleObject" >...</html>  //not sure about quotes syntax

Above methods do the same thing, "Update root element values" but in a different way.

When you used :root, the styles automatically got attached to HTML tag

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, but I don't want to use DOM api straightly. That's why i asked the second question regarding the clean/angular way.
@abhaytripathi, do you have access to html tag?I mean can you add attributes to HTML tag?
ngStyle might help, I will try some permutation and combinations of your suggestion.
Than i let you know in this regard.
18

Starting with Angular v9 you can use the style binding to change a value of a custom property

<app-component-name [style.--theme-color-1]="'#CCC'"></app-component-name>

Comments

0

Some examples add variables directly to html tag and it seem in the element source as a long list. I hope this helps to you,


class AppComponent {
  private variables=['--my-var: 123;', '--my-second-var: 345;'];

  private addAsLink(): void {
    const cssVariables = `:root{ ${this.variables.join('')}};
    const blob = new Blob([cssVariables]);
    const url = URL.createObjectURL(blob);
    const cssElement = document.createElement('link');
    cssElement.setAttribute('rel', 'stylesheet');
    cssElement.setAttribute('type', 'text/css');
    cssElement.setAttribute('href', url);
    document.head.appendChild(cssElement);
  }
}

Comments

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.