-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcss-to-style.js
51 lines (48 loc) · 1.18 KB
/
css-to-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
import expandRule from "css-shorthand-expand";
import { conv as expandHexColor } from "color-shorthand-hex-to-six-digit";
import camelCase from "lodash.camelcase";
function shouldExpandRule(name) {
return [
"background",
"font",
"padding",
"margin",
"border",
"border-width",
"border-style",
"border-color",
"border-top",
"border-right",
"border-bottom",
"border-left",
"border-radius",
"outline",
].includes(name);
}
function convertCssPairsToStyle(rules) {
return (
rules
// Expand shorthand rules
.reduce((out, [name, value]) => {
if (shouldExpandRule(name)) {
const expanded = expandRule(name, value);
const next = Object.keys(expanded).map((rule) => [
rule,
expanded[rule],
]);
return [...out, ...next];
}
return [...out, [name, value]];
}, [])
// Expand colors to 6 digits
.reduce((out, [name, value]) => {
return {
...out,
[camelCase(name)]: /color/i.test(name)
? expandHexColor(value)
: value,
};
}, {})
);
}
export default convertCssPairsToStyle;