UNPKG

3.54 kBJavaScriptView Raw
1import { __spreadArrays } from "tslib";
2/**
3 * Combine a set of styles together (but does not register css classes).
4 * @param styleSets - One or more stylesets to be merged (each param can also be falsy).
5 */
6export function concatStyleSets() {
7 var styleSets = [];
8 for (var _i = 0; _i < arguments.length; _i++) {
9 styleSets[_i] = arguments[_i];
10 }
11 if (styleSets && styleSets.length === 1 && styleSets[0] && !styleSets[0].subComponentStyles) {
12 return styleSets[0];
13 }
14 var mergedSet = {};
15 // We process sub component styles in two phases. First we collect them, then we combine them into 1 style function.
16 var workingSubcomponentStyles = {};
17 for (var _a = 0, styleSets_1 = styleSets; _a < styleSets_1.length; _a++) {
18 var currentSet = styleSets_1[_a];
19 if (currentSet) {
20 for (var prop in currentSet) {
21 if (currentSet.hasOwnProperty(prop)) {
22 if (prop === 'subComponentStyles' && currentSet.subComponentStyles !== undefined) {
23 // subcomponent styles - style functions or objects
24 var currentComponentStyles = currentSet.subComponentStyles;
25 for (var subCompProp in currentComponentStyles) {
26 if (currentComponentStyles.hasOwnProperty(subCompProp)) {
27 if (workingSubcomponentStyles.hasOwnProperty(subCompProp)) {
28 workingSubcomponentStyles[subCompProp].push(currentComponentStyles[subCompProp]);
29 }
30 else {
31 workingSubcomponentStyles[subCompProp] = [currentComponentStyles[subCompProp]];
32 }
33 }
34 }
35 continue;
36 }
37 // the as any casts below is a workaround for ts 2.8.
38 // todo: remove cast to any in ts 2.9.
39 var mergedValue = mergedSet[prop];
40 var currentValue = currentSet[prop];
41 if (mergedValue === undefined) {
42 mergedSet[prop] = currentValue;
43 }
44 else {
45 mergedSet[prop] = __spreadArrays((Array.isArray(mergedValue) ? mergedValue : [mergedValue]), (Array.isArray(currentValue) ? currentValue : [currentValue]));
46 }
47 }
48 }
49 }
50 }
51 if (Object.keys(workingSubcomponentStyles).length > 0) {
52 mergedSet.subComponentStyles = {};
53 var mergedSubStyles = mergedSet.subComponentStyles;
54 var _loop_1 = function (subCompProp) {
55 if (workingSubcomponentStyles.hasOwnProperty(subCompProp)) {
56 var workingSet_1 = workingSubcomponentStyles[subCompProp];
57 mergedSubStyles[subCompProp] = function (styleProps) {
58 return concatStyleSets.apply(void 0, workingSet_1.map(function (styleFunctionOrObject) {
59 return typeof styleFunctionOrObject === 'function' ? styleFunctionOrObject(styleProps) : styleFunctionOrObject;
60 }));
61 };
62 }
63 };
64 // now we process the subcomponent styles if there are any
65 for (var subCompProp in workingSubcomponentStyles) {
66 _loop_1(subCompProp);
67 }
68 }
69 return mergedSet;
70}
71//# sourceMappingURL=concatStyleSets.js.map
\No newline at end of file