@uifabric/merge-styles
Version:
Style loading utilities.
71 lines • 3.54 kB
JavaScript
import { __spreadArrays } from "tslib";
/**
* 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) {
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) {
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] = __spreadArrays((Array.isArray(mergedValue) ? mergedValue : [mergedValue]), (Array.isArray(currentValue) ? currentValue : [currentValue]));
}
}
}
}
}
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