Skip to content

Commit 5f8dcdd

Browse files
committed
feat(@theme-ui/mdx): add TypeScript support
1 parent cf5b00a commit 5f8dcdd

File tree

5 files changed

+117
-89
lines changed

5 files changed

+117
-89
lines changed

packages/core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface IntrinsicSxElements {
4848
em: JSX.IntrinsicElements['em'] & SxProps
4949
strong: JSX.IntrinsicElements['strong'] & SxProps
5050
div: JSX.IntrinsicElements['div'] & SxProps
51-
delete: JSX.IntrinsicElements['div'] & SxProps
51+
del: JSX.IntrinsicElements['div'] & SxProps
5252
inlineCode: JSX.IntrinsicElements['div'] & SxProps
5353
thematicBreak: JSX.IntrinsicElements['div'] & SxProps
5454
root: JSX.IntrinsicElements['div'] & SxProps

packages/mdx/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "@theme-ui/mdx",
33
"version": "0.3.0",
4+
"source": "src/index.ts",
45
"main": "dist/index.js",
56
"module": "dist/index.esm.js",
7+
"types": "dist/index.d.ts",
68
"sideEffects": false,
79
"scripts": {
8-
"prepare": "microbundle --no-compress",
9-
"watch": "microbundle watch --no-compress"
10+
"prepare": "microbundle --no-compress --tsconfig tsconfig.json",
11+
"watch": "microbundle watch --no-compress --tsconfig tsconfig.json"
1012
},
1113
"dependencies": {
1214
"@emotion/core": "^10.0.0",

packages/mdx/src/index.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

packages/mdx/src/index.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { jsx, IntrinsicSxElements } from '@theme-ui/core'
2+
import { css, get, Theme } from '@theme-ui/css'
3+
import { ComponentType, FC, ReactNode } from 'react'
4+
import styled, { Interpolation } from '@emotion/styled'
5+
import {
6+
MDXProvider as _MDXProvider,
7+
useMDXComponents
8+
} from '@mdx-js/react'
9+
10+
11+
export type MdxAliases = {
12+
[key in keyof IntrinsicSxElements]: keyof JSX.IntrinsicElements
13+
}
14+
15+
export type MdxAliasesKeys = 'inlineCode' | 'thematicBreak' | 'root'
16+
17+
export type MdxProviderComponents = {
18+
[key in keyof IntrinsicSxElements]: ComponentType
19+
}
20+
21+
export type ThemedProps = {
22+
theme: Theme
23+
}
24+
25+
export interface MdxProviderProps {
26+
components?: MdxProviderComponents
27+
children: ReactNode
28+
}
29+
30+
// mdx components
31+
const tags: Array<keyof IntrinsicSxElements> = [
32+
'p',
33+
'b',
34+
'i',
35+
'a',
36+
'h1',
37+
'h2',
38+
'h3',
39+
'h4',
40+
'h5',
41+
'h6',
42+
'img',
43+
'pre',
44+
'code',
45+
'ol',
46+
'ul',
47+
'li',
48+
'blockquote',
49+
'hr',
50+
'em',
51+
'table',
52+
'tr',
53+
'th',
54+
'td',
55+
'em',
56+
'strong',
57+
'del',
58+
// mdx
59+
'inlineCode',
60+
'thematicBreak',
61+
// other
62+
'div',
63+
// theme-ui
64+
'root',
65+
]
66+
67+
const aliases: Pick<MdxAliases, MdxAliasesKeys> = {
68+
inlineCode: 'code',
69+
thematicBreak: 'hr',
70+
root: 'div',
71+
}
72+
73+
const alias = (n: keyof IntrinsicSxElements): keyof JSX.IntrinsicElements => aliases[n] || n
74+
75+
export const themed = (key: keyof IntrinsicSxElements) => (props: ThemedProps) =>
76+
css(get(props.theme, `styles.${key}`))(props.theme) as Interpolation<ThemedProps>
77+
78+
export const Styled = styled('div')(themed('div'))
79+
80+
export const components = {}
81+
82+
tags.forEach(tag => {
83+
components[tag] = styled(alias(tag))(themed(tag))
84+
Styled[tag] = components[tag]
85+
})
86+
87+
const createComponents = (comps: MdxProviderComponents) => {
88+
const next = { ...components }
89+
90+
const componentKeys = Object.keys(comps) as Array<keyof IntrinsicSxElements>
91+
92+
componentKeys.forEach(key => {
93+
next[key] = styled(comps[key])(themed(key))
94+
})
95+
return next
96+
}
97+
98+
export const MDXProvider: FC<MdxProviderProps> = ({
99+
components,
100+
children,
101+
}) => {
102+
const outer = useMDXComponents()
103+
104+
return jsx(_MDXProvider, {
105+
components: createComponents({ ...outer, ...components }),
106+
children,
107+
})
108+
}

packages/mdx/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../core/tsconfig.json",
3+
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts"]
4+
}

0 commit comments

Comments
 (0)