-
Notifications
You must be signed in to change notification settings - Fork 672
/
index.js
113 lines (100 loc) · 2.14 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// - [ ] ComponentProvider
// - [ ] base markdown components
// - [ ] consider switching back to @emotion/styled
import { jsx } from '@theme-ui/core'
import { css, get } from '@theme-ui/css'
import React from 'react'
import { ThemeContext } from '@emotion/core'
import isPropValid from '@emotion/is-prop-valid'
import { MDXProvider as _MDXProvider } from '@mdx-js/react'
// mdx components
const tags = [
'p',
'b',
'i',
'a',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'img',
'pre',
'code',
'ol',
'ul',
'li',
'blockquote',
'hr',
'em',
'table',
'tr',
'th',
'td',
'em',
'strong',
'delete',
// mdx
'inlineCode',
'thematicBreak',
// other
'div',
// theme-ui
'root',
]
const aliases = {
inlineCode: 'code',
thematicBreak: 'hr',
root: 'div',
}
const alias = n => aliases[n] || n
export const styled = tag => (...args) => {
const Styled = React.forwardRef(({ as, ...props }, ref) => {
const shouldForwardProps =
typeof tag !== 'string' || (as && typeof as !== 'string')
const theme = React.useContext(ThemeContext)
let nextProps = shouldForwardProps ? props : {}
let styles = {}
args.forEach(arg => {
const style = typeof arg === 'function' ? arg({ theme, ...props }) : arg
Object.assign(styles, style)
})
if (!shouldForwardProps) {
for (let key in props) {
if (!isPropValid(key)) continue
nextProps[key] = props[key]
}
}
return jsx(as || tag, {
...nextProps,
ref,
css: styles,
})
})
return Styled
}
export const themed = key => props =>
css(get(props.theme, `styles.${key}`))(props.theme)
export const Styled = styled('div')(themed('div'))
export const components = {}
tags.forEach(tag => {
components[tag] = styled(alias(tag))(themed(tag))
Styled[tag] = components[tag]
})
const createComponents = () => {
const next = {}
Object.keys(components).forEach(key => {
next[key] = styled(components[key])(themed(key))
})
return next
}
export const MDXProvider = ({
components,
children,
}) => {
return jsx(_MDXProvider, {
components: createComponents(components),
children,
})
}