-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathh.ts
More file actions
97 lines (86 loc) · 2.45 KB
/
Copy pathh.ts
File metadata and controls
97 lines (86 loc) · 2.45 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
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
87
88
89
90
91
92
93
94
95
96
97
import {
createElement,
ReactElement,
ReactNode,
ElementType,
ReactHTML,
Attributes,
} from 'react';
import {incorporate, setIncorporator} from './incorporate';
import {setModules, hasModuleProps, Modulizer} from './Modulizer';
import Incorporator from './Incorporator';
export type PropsExtensions = {
sel?: string | symbol;
};
let shouldIncorporate = props => props.sel
export function useModules(modules: any) {
if (!modules || typeof modules !== 'object') return
setModules(modules);
shouldIncorporate = props => props.sel || hasModuleProps(props)
setIncorporator(Modulizer)
}
type PropsLike<P> = P & PropsExtensions & Attributes;
type Children = string | Array<ReactNode>;
function createElementSpreading<P = any>(
type: ElementType<P> | keyof ReactHTML,
props: PropsLike<P> | null,
children: Children
): ReactElement<P> {
if (typeof children === 'string') {
return createElement(type, props, children);
} else {
return createElement(type, props, ...children);
}
}
function hyperscriptProps<P = any>(
type: ElementType<P> | keyof ReactHTML,
props: PropsLike<P>
): ReactElement<P> {
if (!shouldIncorporate(props)) {
return createElement(type, props);
} else {
return createElement(incorporate(type), props);
}
}
function hyperscriptChildren<P = any>(
type: ElementType<P> | keyof ReactHTML,
children: Children
): ReactElement<P> {
return createElementSpreading(type, null, children);
}
function hyperscriptPropsChildren<P = any>(
type: ElementType<P> | keyof ReactHTML,
props: PropsLike<P>,
children: Children
): ReactElement<P> {
if (!shouldIncorporate(props)) {
return createElementSpreading(type, props, children);
} else {
return createElementSpreading(incorporate(type), props, children);
}
}
export function h<P = any>(
type: ElementType<P> | keyof ReactHTML,
a?: PropsLike<P> | Children,
b?: Children
): ReactElement<P> {
if (a === undefined && b === undefined) {
return createElement(type, null);
}
if (b === undefined && (typeof a === 'string' || Array.isArray(a))) {
return hyperscriptChildren(type, a as Array<ReactNode>);
}
if (b === undefined && typeof a === 'object' && !Array.isArray(a)) {
return hyperscriptProps(type, a);
}
if (
a !== undefined &&
typeof a !== 'string' &&
!Array.isArray(a) &&
b !== undefined
) {
return hyperscriptPropsChildren(type, a, b);
} else {
throw new Error('Unexpected usage of h() function');
}
}