Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useMDXComponents hook #440

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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