|
| 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 | +} |
0 commit comments