UNPKG

1.3 kBJavaScriptView Raw
1import has from 'has';
2
3// This function takes the array of styles and separates them into styles that
4// are handled by Aphrodite and inline styles.
5export default function separateStyles(stylesArray) {
6 const aphroditeStyles = [];
7
8 // Since determining if an Object is empty requires collecting all of its
9 // keys, and we want the best performance in this code because we are in the
10 // render path, we are going to do a little bookkeeping ourselves.
11 let hasInlineStyles = false;
12 const inlineStyles = {};
13
14 // This is run on potentially every node in the tree when rendering, where
15 // performance is critical. Normally we would prefer using `forEach`, but
16 // old-fashioned for loops are faster so that's what we have chosen here.
17 for (let i = 0; i < stylesArray.length; i += 1) {
18 const style = stylesArray[i];
19
20 // If this style is falsey, we just want to disregard it. This allows for
21 // syntax like:
22 //
23 // css(isFoo && styles.foo)
24 if (style) {
25 if (
26 has(style, '_name') &&
27 has(style, '_definition')
28 ) {
29 aphroditeStyles.push(style);
30 } else {
31 Object.assign(inlineStyles, style);
32 hasInlineStyles = true;
33 }
34 }
35 }
36
37 return {
38 aphroditeStyles,
39 hasInlineStyles,
40 inlineStyles,
41 };
42}