-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfactory.js
40 lines (34 loc) · 1.13 KB
/
factory.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
'use strict';
// wrap nanostyled in a factory that can accept any React-compatible
// +createElement+ function. Works with React, Preact, Inferno, etc
function factory(createElement) {
return function nanostyled(tag, styleProps) {
var Component = function(props) {
var separatedProps = Object.keys(styleProps).reduce(
function(memo, key) {
var style = props[key] === undefined ? styleProps[key] : props[key];
if (style) memo.styles.push(style);
delete memo.notStyles[key];
return memo;
},
{
styles: [props.className].filter(Boolean),
notStyles: assign({}, props),
}
);
var passedProps = assign(separatedProps.notStyles, {
className: separatedProps.styles.join(' '),
as: undefined,
});
return createElement(props.as || tag, passedProps);
};
Component.displayName = 'nanostyled-'.concat(tag);
return Component;
};
}
// borrow IE-compatible Object.assign from @developit/preact
function assign(obj, props) {
for (var i in props) obj[i] = props[i];
return obj;
}
module.exports = factory;