UNPKG

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