76 lines
3.6 KiB
JavaScript
76 lines
3.6 KiB
JavaScript
|
import { __spreadArray } from "tslib";
|
||
|
import { isShadowConfig } from './shadowConfig';
|
||
|
/**
|
||
|
* Combine a set of styles together (but does not register css classes).
|
||
|
* @param styleSets - One or more stylesets to be merged (each param can also be falsy).
|
||
|
*/
|
||
|
export function concatStyleSets() {
|
||
|
var styleSets = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
styleSets[_i] = arguments[_i];
|
||
|
}
|
||
|
if (styleSets &&
|
||
|
styleSets.length === 1 &&
|
||
|
styleSets[0] &&
|
||
|
!styleSets[0].subComponentStyles &&
|
||
|
!isShadowConfig(styleSets[0])) {
|
||
|
return styleSets[0];
|
||
|
}
|
||
|
var mergedSet = {};
|
||
|
// We process sub component styles in two phases. First we collect them, then we combine them into 1 style function.
|
||
|
var workingSubcomponentStyles = {};
|
||
|
for (var _a = 0, styleSets_1 = styleSets; _a < styleSets_1.length; _a++) {
|
||
|
var currentSet = styleSets_1[_a];
|
||
|
if (currentSet && !isShadowConfig(currentSet)) {
|
||
|
for (var prop in currentSet) {
|
||
|
if (currentSet.hasOwnProperty(prop)) {
|
||
|
if (prop === 'subComponentStyles' && currentSet.subComponentStyles !== undefined) {
|
||
|
// subcomponent styles - style functions or objects
|
||
|
var currentComponentStyles = currentSet.subComponentStyles;
|
||
|
for (var subCompProp in currentComponentStyles) {
|
||
|
if (currentComponentStyles.hasOwnProperty(subCompProp)) {
|
||
|
if (workingSubcomponentStyles.hasOwnProperty(subCompProp)) {
|
||
|
workingSubcomponentStyles[subCompProp].push(currentComponentStyles[subCompProp]);
|
||
|
}
|
||
|
else {
|
||
|
workingSubcomponentStyles[subCompProp] = [currentComponentStyles[subCompProp]];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
// the as any casts below is a workaround for ts 2.8.
|
||
|
// todo: remove cast to any in ts 2.9.
|
||
|
var mergedValue = mergedSet[prop];
|
||
|
var currentValue = currentSet[prop];
|
||
|
if (mergedValue === undefined) {
|
||
|
mergedSet[prop] = currentValue;
|
||
|
}
|
||
|
else {
|
||
|
mergedSet[prop] = __spreadArray(__spreadArray([], (Array.isArray(mergedValue) ? mergedValue : [mergedValue]), true), (Array.isArray(currentValue) ? currentValue : [currentValue]), true);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if (Object.keys(workingSubcomponentStyles).length > 0) {
|
||
|
mergedSet.subComponentStyles = {};
|
||
|
var mergedSubStyles = mergedSet.subComponentStyles;
|
||
|
var _loop_1 = function (subCompProp) {
|
||
|
if (workingSubcomponentStyles.hasOwnProperty(subCompProp)) {
|
||
|
var workingSet_1 = workingSubcomponentStyles[subCompProp];
|
||
|
mergedSubStyles[subCompProp] = function (styleProps) {
|
||
|
return concatStyleSets.apply(void 0, workingSet_1.map(function (styleFunctionOrObject) {
|
||
|
return typeof styleFunctionOrObject === 'function' ? styleFunctionOrObject(styleProps) : styleFunctionOrObject;
|
||
|
}));
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
// now we process the subcomponent styles if there are any
|
||
|
for (var subCompProp in workingSubcomponentStyles) {
|
||
|
_loop_1(subCompProp);
|
||
|
}
|
||
|
}
|
||
|
return mergedSet;
|
||
|
}
|
||
|
//# sourceMappingURL=concatStyleSets.js.map
|