-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathseparateStyles.js
More file actions
41 lines (35 loc) · 1.26 KB
/
separateStyles.js
File metadata and controls
41 lines (35 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import assign from 'object-assign';
// This function takes an array of styles and separates them into styles that
// are handled by Aphrodite and inline styles.
function separateStyles(stylesArray) {
const classNames = [];
// Since determining if an Object is empty requires collecting all of its
// keys, and we want the best performance in this code because we are in the
// render path, we are going to do a little bookkeeping ourselves.
let hasInlineStyles = false;
const inlineStyles = {};
// This is run on potentially every node in the tree when rendering, where
// performance is critical. Normally we would prefer using `forEach`, but
// old-fashioned for loops are faster so that's what we have chosen here.
for (let i = 0; i < stylesArray.length; i++) { // eslint-disable-line no-plusplus
const style = stylesArray[i];
// If this style is falsy, we just want to disregard it. This allows for
// syntax like:
//
// css(isFoo && styles.foo)
if (style) {
if (typeof style === 'string') {
classNames.push(style);
} else {
assign(inlineStyles, style);
hasInlineStyles = true;
}
}
}
return {
classNames,
hasInlineStyles,
inlineStyles,
};
}
export default separateStyles;