-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcreate-element.js
74 lines (57 loc) · 1.74 KB
/
create-element.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
import React from 'react'
import {useMDXComponents} from './context'
const TYPE_PROP_NAME = 'mdxType'
const DEFAULTS = {
inlineCode: 'code',
wrapper: ({children}) => React.createElement(React.Fragment, {}, children)
}
const MDXCreateElement = React.forwardRef((props, ref) => {
const {
components: propComponents,
mdxType,
originalType,
parentName,
...etc
} = props
const components = useMDXComponents(propComponents)
const type = mdxType
const Component =
components[`${parentName}.${type}`] ||
components[type] ||
DEFAULTS[type] ||
originalType
/* istanbul ignore if - To do: what is this useful for? */
if (propComponents) {
return React.createElement(Component, {
ref,
...etc,
components: propComponents
})
}
return React.createElement(Component, {ref, ...etc})
})
MDXCreateElement.displayName = 'MDXCreateElement'
export default function (type, props) {
const args = arguments
const mdxType = props && props.mdxType
if (typeof type === 'string' || mdxType) {
const argsLength = args.length
const createElementArgArray = new Array(argsLength)
createElementArgArray[0] = MDXCreateElement
const newProps = {}
for (let key in props) {
/* istanbul ignore else - folks putting stuff in `prototype`. */
if (hasOwnProperty.call(props, key)) {
newProps[key] = props[key]
}
}
newProps.originalType = type
newProps[TYPE_PROP_NAME] = typeof type === 'string' ? type : mdxType
createElementArgArray[1] = newProps
for (let i = 2; i < argsLength; i++) {
createElementArgArray[i] = args[i]
}
return React.createElement.apply(null, createElementArgArray)
}
return React.createElement.apply(null, args)
}