UNPKG

1.87 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
8
9exports.isNestedStyle = isNestedStyle;
10exports.mergeStyles = mergeStyles;
11function isNestedStyle(value) {
12 // Don't merge objects overriding toString, since they should be converted
13 // to string values.
14 return value && value.constructor === Object && value.toString === Object.prototype.toString;
15}
16
17// Merge style objects. Deep merge plain object values.
18function mergeStyles(styles) {
19 var result = {};
20
21 styles.forEach(function (style) {
22 if (!style || (typeof style === 'undefined' ? 'undefined' : _typeof(style)) !== 'object') {
23 return;
24 }
25
26 if (Array.isArray(style)) {
27 style = mergeStyles(style);
28 }
29
30 Object.keys(style).forEach(function (key) {
31 // Simple case, nothing nested
32 if (!isNestedStyle(style[key]) || !isNestedStyle(result[key])) {
33 result[key] = style[key];
34 return;
35 }
36
37 // If nested media, don't merge the nested styles, append a space to the
38 // end (benign when converted to CSS). This way we don't end up merging
39 // media queries that appear later in the chain with those that appear
40 // earlier.
41 if (key.indexOf('@media') === 0) {
42 var newKey = key;
43 while (true) {
44 // eslint-disable-line no-constant-condition
45 newKey += ' ';
46 if (!result[newKey]) {
47 result[newKey] = style[key];
48 return;
49 }
50 }
51 }
52
53 // Merge all other nested styles recursively
54 result[key] = mergeStyles([result[key], style[key]]);
55 });
56 });
57
58 return result;
59}
\No newline at end of file