Skip to content

Commit

Permalink
Add useMDXComponents hook (#440)
Browse files Browse the repository at this point in the history
* Add useMDXComponents hook

* Update snapshot
  • Loading branch information
alexandernanberg authored and johno committed Mar 6, 2019
1 parent 1f71e16 commit 07806bb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
10 changes: 4 additions & 6 deletions packages/mdx/create-element.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const React = require('react')
const {withMDXComponents} = require('@mdx-js/tag')
const {useMDXComponents} = require('@mdx-js/tag')

const TYPE_PROP_NAME = '__MDX_TYPE_PLEASE_DO_NOT_USE__'

Expand All @@ -8,12 +8,13 @@ const DEFAULTS = {
wrapper: 'div'
}

const MDXCreateElementInner = ({
components = {},
const MDXCreateElement = ({
components: propComponents,
__MDX_TYPE_PLEASE_DO_NOT_USE__,
parentName,
...etc
}) => {
const components = useMDXComponents(propComponents)
const type = __MDX_TYPE_PLEASE_DO_NOT_USE__
const Component =
components[`${parentName}.${type}`] ||
Expand All @@ -23,9 +24,6 @@ const MDXCreateElementInner = ({

return React.createElement(Component, etc)
}
MDXCreateElementInner.displayName = 'MDXCreateElementInner'

const MDXCreateElement = withMDXComponents(MDXCreateElementInner)
MDXCreateElement.displayName = 'MDXCreateElement'

module.exports = function(type, props) {
Expand Down
1 change: 1 addition & 0 deletions packages/mdx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export default class MDXContent extends React.Component {
<Layout {...layoutProps} {...props}>
<h1 >{\`Hello, world!\`}</h1>
<p >{\`I'm an awesome paragraph.\`}</p>
{/* I'm a comment */}
Expand Down
3 changes: 2 additions & 1 deletion packages/tag/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export {default as MDXTag} from './mdx-tag'
export {
default as MDXContext,
MDXProvider,
withMDXComponents
withMDXComponents,
useMDXComponents
} from './mdx-context'
20 changes: 14 additions & 6 deletions packages/tag/src/mdx-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ const isFunction = obj => typeof obj === 'function'
const MDXContext = React.createContext({})

export const withMDXComponents = Component => props => {
const contextComponents = React.useContext(MDXContext)
const allComponents = props.components || contextComponents
const allComponents = useMDXComponents(props.components)

return <Component {...props} components={allComponents} />
}

export const useMDXComponents = components => {
const contextComponents = React.useContext(MDXContext)
let allComponents = contextComponents
if (components) {
allComponents = isFunction(components)
? components(contextComponents)
: components
}

return allComponents
}

export const MDXProvider = props => {
const outerContextComponents = React.useContext(MDXContext)
const allComponents = isFunction(props.components)
? props.components(outerContextComponents)
: props.components
const allComponents = useMDXComponents(props.components)

return (
<MDXContext.Provider value={allComponents}>
Expand Down
55 changes: 26 additions & 29 deletions packages/tag/src/mdx-tag.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import React, {Component} from 'react'
import {withMDXComponents} from './mdx-context'
import React from 'react'
import {useMDXComponents} from './mdx-context'

const defaults = {
inlineCode: 'code',
wrapper: 'div'
}

class MDXTag extends Component {
render() {
const {
name,
parentName,
props: childProps = {},
children,
components = {},
Layout,
layoutProps
} = this.props
const MDXTag = ({
name,
parentName,
props: childProps = {},
children,
components: propComponents,
Layout,
layoutProps
}) => {
const components = useMDXComponents(propComponents)
const Component =
components[`${parentName}.${name}`] ||
components[name] ||
defaults[name] ||
name

const Component =
components[`${parentName}.${name}`] ||
components[name] ||
defaults[name] ||
name

if (Layout) {
return (
<Layout components={components} {...layoutProps}>
<Component {...childProps}>{children}</Component>
</Layout>
)
}

return <Component {...childProps}>{children}</Component>
if (Layout) {
return (
<Layout components={components} {...layoutProps}>
<Component {...childProps}>{children}</Component>
</Layout>
)
}

return <Component {...childProps}>{children}</Component>
}

export default withMDXComponents(MDXTag)
export default MDXTag

0 comments on commit 07806bb

Please sign in to comment.