/
index.tsx
202 lines (163 loc) · 4.71 KB
/
index.tsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/** @jsx jsx */
import { jsx, IntrinsicSxElements } from '@theme-ui/core'
import { css, CSSObject, get, Theme } from '@theme-ui/css'
import {
ComponentType,
FC,
ReactNode,
DetailedHTMLProps,
HTMLAttributes,
ElementType,
ComponentProps,
useMemo,
} from 'react'
import { MDXProvider as _MDXProvider, useMDXComponents } from '@mdx-js/react'
type MDXProviderComponentsKnownKeys = {
[key in keyof IntrinsicSxElements]?: ComponentType<any> | string
}
export interface MDXProviderComponents extends MDXProviderComponentsKnownKeys {
[key: string]: ComponentType<any> | string | undefined
}
export type MdxAliases = {
[key in keyof IntrinsicSxElements]: keyof JSX.IntrinsicElements
}
export type MdxAliasesKeys = 'inlineCode' | 'thematicBreak' | 'root'
export type ThemedProps = {
theme: Theme
}
export interface MdxProviderProps {
components?: MDXProviderComponents
children: ReactNode
}
// mdx components
const tags: Array<keyof IntrinsicSxElements> = [
'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',
'del',
// mdx
'inlineCode',
'thematicBreak',
// other
'div',
// theme-ui
'root',
]
const aliases = {
inlineCode: 'code',
thematicBreak: 'hr',
root: 'div',
} as const
type Aliases = typeof aliases
const isAlias = (x: string): x is keyof Aliases => x in aliases
export type ThemedComponentName = keyof IntrinsicSxElements
const alias = (n: ThemedComponentName): keyof JSX.IntrinsicElements =>
isAlias(n) ? aliases[n] : n
export const themed =
(key: ThemedComponentName | (string & {})) => (theme: Theme) =>
css(get(theme, `styles.${key}`))(theme)
// opt out of typechecking whenever `as` prop is used
interface AnyComponentProps extends JSX.IntrinsicAttributes {
[key: string]: unknown
}
export interface ThemedComponent<Name extends string> {
(
props: (Name extends keyof JSX.IntrinsicElements
? ComponentProps<Name>
: {}) & { css?: CSSObject }
): JSX.Element
displayName: string
}
export type ThemedComponentsDict = {
[K in keyof IntrinsicSxElements]: K extends keyof Aliases
? ThemedComponent<Aliases[K]>
: K extends keyof JSX.IntrinsicElements
? ThemedComponent<K>
: never
}
const createThemedComponent = <Name extends string>(
name: Name,
variant: ThemedComponentName
): ThemedComponent<Name> => {
const variantStyles = themed(variant)
const component: ThemedComponent<Name> = (props) => {
const extraStyles: { textAlign?: 'left' | 'right' | 'center' | 'justify' } =
{}
if (name === 'th' || name === 'td') {
const { align } = props as DetailedHTMLProps<
React.ThHTMLAttributes<HTMLTableHeaderCellElement>,
HTMLTableHeaderCellElement
>
if (align !== 'char') extraStyles.textAlign = align
}
const css = (props as any)['css']
return jsx(name, {
...props,
css: [props.css, variantStyles, extraStyles].filter(Boolean),
})
}
component.displayName = `Themed(${name})`
return component
}
interface ThemedDivProps
extends DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement> {}
interface ThemedDiv {
(props: ThemedDivProps): JSX.Element
}
const _Themed: ThemedDiv = createThemedComponent('div', 'div')
export const components = {} as ThemedComponentsDict
export const Themed = _Themed as ThemedDiv & ThemedComponentsDict
tags.forEach((tag) => {
const component = createThemedComponent(alias(tag), tag)
components[tag] = component
Themed[tag] = component
})
const createComponents = (comps: MDXProviderComponents) => {
const componentKeys = Object.keys(comps) as Array<keyof IntrinsicSxElements>
const next = { ...components }
// We enrich user's components with the styles from theme.styles.
// Example: `components.p` will get the styles from `theme.styles.p` as className.
// todo: test this behaviour
componentKeys.forEach((key) => {
const componentAtKey = comps[key]
console.log('createComponents', key, componentAtKey)
if (componentAtKey) {
const component: ThemedComponent<string> = (props) => {
console.log('component render props=', props)
return jsx(componentAtKey, { ...props, css: themed(key) })
}
component.displayName = "MdxComponents('" + key + "')"
next[key] = component
}
})
return next
}
export const MDXProvider: FC<MdxProviderProps> = ({ components, children }) => {
const outer = useMDXComponents() as MDXProviderComponents
const themedComponents = useMemo(() => {
return createComponents({ ...outer, ...components })
}, [components, outer])
return jsx(_MDXProvider, { components: themedComponents, children })
}