-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.js
86 lines (74 loc) · 2.1 KB
/
style.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const forEach = require("lodash/forEach")
const isFunction = require("lodash/isFunction")
const isNumber = require("lodash/isNumber")
const last = require("lodash/last")
const get = require("lodash/get")
const pull = require("lodash/pull")
const swap = require("./swap")
const defaultNumericUnits = {
borderRadius: "px",
fontSize: "px",
height: "px",
margin: "px",
marginBottom: "px",
marginLeft: "px",
marginRight: "px",
marginTop: "px",
maxHeight: "px",
maxWidth: "px",
minHeight: "px",
minWidth: "px",
padding: "px",
paddingBottom: "px",
paddingLeft: "px",
paddingRight: "px",
paddingTop: "px",
width: "px",
}
const convertNumericValues = (prop, value) => {
if (defaultNumericUnits[prop]) {
if (isNumber(value)) {
return value + defaultNumericUnits[prop]
}
}
return value
}
const stackableProps = ['display', 'color', 'backgroundColor']
const staticStyle = styleObj => node => {
const styleIdentifiedValues = {}
forEach(styleObj, (v, k) => {
const value = convertNumericValues(k, v)
// applique la valeur
node.style[k] = value
if (stackableProps.indexOf(k) > -1) {
// convertit les valeurs en objet pour avoir une référence mémoire
const objValue = { value }
// crée une pile de valeurs pour cette propriété si besoin
if (!node.stackStyle) node.stackStyle = {}
if (!node.stackStyle[k]) node.stackStyle[k] = []
// ajoute la valeur en haut de la pile
node.stackStyle[k].push(objValue)
styleIdentifiedValues[k] = objValue
}
})
return () =>
forEach(styleObj, (v, k) => {
const sIV = styleIdentifiedValues[k]
let value = null
if (sIV) {
// retire la valeur de la pile
pull(node.stackStyle[k], sIV)
// applique la valeur restante en haut de pile
value = get(last(node.stackStyle[k]), "value") || null
}
node.style[k] = value
})
}
const dynamicStyle = getStyleObj => swap(() => staticStyle(getStyleObj()))
module.exports = function (arg) {
if (isFunction(arg)) {
return dynamicStyle(arg)
} else {
return staticStyle(arg)
}
}