ngx-css-variables
Version:
Css variables module for Angular 2/4/5
131 lines (122 loc) • 2.99 kB
JavaScript
/**
* @license ngx-css-variables
* MIT license
*/
import { Directive, ElementRef, Injectable, Input, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class CssVarsDirective {
/**
* @param {?} el
*/
constructor(el) {
this.el = el;
}
/**
* @return {?}
*/
ngOnInit() {
if (!this.cssVars)
return;
Object.keys(this.cssVars).forEach(key => {
this.el.nativeElement.style.setProperty(key, this.cssVars[key]);
});
}
}
CssVarsDirective.decorators = [
{ type: Directive, args: [{
selector: '[css-vars]'
},] },
];
/** @nocollapse */
CssVarsDirective.ctorParameters = () => [
{ type: ElementRef, },
];
CssVarsDirective.propDecorators = {
"cssVars": [{ type: Input, args: ['css-vars',] },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class CssVarsService {
constructor() {
this.head = document.head || document.getElementsByTagName('head')[0];
}
/**
* @param {?} vars
* @return {?}
*/
setVariables(vars) {
const /** @type {?} */ style = document.createElement('style');
style.type = 'text/css';
const /** @type {?} */ styles = [];
Object.keys(vars).forEach((property) => {
styles.push(`${property}: ${vars[property]}`);
});
const /** @type {?} */ css = `:root {${styles.join('; ')}}`;
style.appendChild(document.createTextNode(css));
this.head.appendChild(style);
return css;
}
}
CssVarsService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
CssVarsService.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class CssVarsModule {
/**
* @return {?}
*/
static forRoot() {
return {
ngModule: CssVarsModule,
providers: [CssVarsService]
};
}
/**
* @return {?}
*/
static forChild() {
return {
ngModule: CssVarsModule,
providers: [CssVarsService]
};
}
}
CssVarsModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations: [CssVarsDirective],
exports: [CssVarsDirective]
},] },
];
/** @nocollapse */
CssVarsModule.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Generated bundle index. Do not edit.
*/
export { CssVarsModule, CssVarsDirective, CssVarsService };
//# sourceMappingURL=ngx-css-variables.js.map