exk0730
exk0730

Reputation: 733

Dynamically inject styles into Angular component

I have a component that I would like to apply CSS styles to using an object of selectors to CSS props. https://codesandbox.io/s/happy-goldberg-4740z

Styles to be applied from parent

const styles = {
  a: [
    ['color', 'red']
  ],
  b: [
    ['color', 'blue']
  ]
};

Component to be styled dynamically

@Component({
  selector: 'test',
  template: `
    <div>
      <span class="a">I am A</span>
      <span class="b">I am B</span>
    </div>
  `
})
export class Test implements OnInit {
  @Input() stylesToApply: { [key: string]: string[][] };

  constructor(private _renderer: Renderer2, private _element: ElementRef) {}

  ngOnInit() {
    let styleText = '';
    Object.entries(this.stylesToApply).forEach(([key, value]) => {
        const selector = `:host ${ key }`;
        let propStr = '';
        for (let i = 0; i < value.length; i++) {
            propStr += `${ value[i][0] }: ${ value[i][1] };\n`;
        }

        styleText += `${ selector} { ${ propStr } }\n`;
    });

    const styleElement = this._renderer.createElement('style');
    this._renderer.appendChild(styleElement, this._renderer.createText(styleText));
    this._renderer.appendChild(this._elementRef.nativeElement, styleElement);
  }
}

The result should be a <style> element injected into Test like so but the styles are not actually applied to the elements:

<style>
:host a { color: red; }
:host b { color: blue; }
</style>

Upvotes: 0

Views: 823

Answers (1)

Edward
Edward

Reputation: 1126

The angular docs have a good starting guide on how to achieve this here

You need have an object that contains property CSSStyleDeclaration. i.e.

myStyle: CSSStyleDeclaration = { width: '100px', minHeight: '200px', backgroundColor: 'red' }

then in your component you could do something along the lines of

<div [style]="myStyle"></div>

this allows you to pass the style directly into the element.

another option at your disposal is to use @HostBinding, but that's probably less ideal for what you are trying to do

Upvotes: 1

Related Questions