Created
November 4, 2018 16:12
-
-
Save sulco/6860711bd1f7d72344f317118a28ff4f to your computer and use it in GitHub Desktop.
Angular theme directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, ElementRef, Input, OnChanges } from '@angular/core'; | |
/** | |
* Usage: | |
* <mycomponent [dtTheme]="{'color-main': '#bada55'}"></mycomponent> | |
*/ | |
@Directive({ | |
selector: '[dtTheme]' | |
}) | |
export class ThemeDirective implements OnChanges { | |
@Input('dtTheme') theme: {[prop: string]: string}; | |
constructor(private el: ElementRef<HTMLElement>) { | |
} | |
ngOnChanges() { | |
Object.keys(this.theme).forEach(prop => { | |
this.el.nativeElement.style.setProperty(`--${prop}`, this.theme[prop]); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment