forked from preactjs/preact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreact.jsx
39 lines (34 loc) · 945 Bytes
/
preact.jsx
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
import {
options,
createElement,
cloneElement,
Component as CevicheComponent,
render
} from 'preact';
options.vnode = vnode => {
vnode.nodeName = vnode.type;
vnode.attributes = vnode.props;
vnode.children = vnode._children || [].concat(vnode.props.children || []);
};
function asArray(arr) {
return Array.isArray(arr) ? arr : [arr];
}
function normalize(obj) {
if (Array.isArray(obj)) {
return obj.map(normalize);
}
if ('type' in obj && !('attributes' in obj)) {
obj.attributes = obj.props;
}
return obj;
}
export function Component(props, context) {
CevicheComponent.call(this, props, context);
const render = this.render;
this.render = function (props, state, context) {
if (props.children) props.children = asArray(normalize(props.children));
return render.call(this, props, state, context);
};
}
Component.prototype = new CevicheComponent();
export { createElement, createElement as h, cloneElement, render };