Skip to content

Commit

Permalink
Implement ability to opt out of MDXProvider components
Browse files Browse the repository at this point in the history
Related to #816
  • Loading branch information
johno committed Jul 22, 2020
1 parent 6098d94 commit dafd031
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
14 changes: 8 additions & 6 deletions packages/react/src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export const withMDXComponents = Component => props => {

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

if (components === false) {
// User has specified not to nest context or use the MDXProvider
return {}
} else if (isFunction(components)) {
// Custom merge via a function prop
return components(contextComponents)
}

return allComponents
return {...contextComponents, ...components}
}

export const MDXProvider = props => {
Expand Down
32 changes: 23 additions & 9 deletions packages/react/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ const H1 = props => <h1 style={{color: 'tomato'}} {...props} />
const H2 = props => <h2 style={{color: 'rebeccapurple'}} {...props} />
const CustomH2 = props => <h2 style={{color: 'papayawhip'}} {...props} />
const CustomDelete = props => <del style={{color: 'crimson'}} {...props} />
const Layout = ({children}) => <div id="layout">{children}</div>

it('Should allow components to be passed via context', () => {
const Layout = ({children}) => <div id="layout">{children}</div>

it('allows components to be passed via context', () => {
const result = renderToString(
<MDXProvider components={{h1: H1, wrapper: Layout}}>
<Fixture />
Expand All @@ -25,7 +24,7 @@ it('Should allow components to be passed via context', () => {
expect(result).toMatch(/style="color:tomato"/)
})

it('Should merge components when there is nested context', () => {
it('merges components when there is nested context', () => {
const components = {h1: H1, h2: H2}

const result = renderToString(
Expand All @@ -36,14 +35,14 @@ it('Should merge components when there is nested context', () => {
</MDXProvider>
)

// MDXTag picks up original component context
// MDX pragma picks up original component context
expect(result).toMatch(/style="color:tomato"/)

// MDXTag picks up overridden component context
// MDX pragma picks up overridden component context
expect(result).toMatch(/style="color:papayawhip"/)
})

it('Should allow removing of context components using the functional form', () => {
it('allows removing of context components using the functional form', () => {
const components = {h1: H1, h2: H2}

const result = renderToString(
Expand All @@ -54,13 +53,28 @@ it('Should allow removing of context components using the functional form', () =
</MDXProvider>
)

// MDXTag does not pick up original component context
// MDX pragma does not pick up original component context
expect(result).not.toMatch(/style="color:tomato"/)

// MDXTag picks up overridden component context
// MDX pragma picks up overridden component context
expect(result).toMatch(/style="color:papayawhip"/)
})

it('allows removing of context components by setting false', () => {
const components = {h1: H1}

const result = renderToString(
<MDXProvider components={components}>
<MDXProvider components={false}>
<Fixture />
</MDXProvider>
</MDXProvider>
)

// MDX pragma does not pick up original component context
expect(result).not.toMatch(/style="color:tomato"/)
})

it('Should pass prop components along', () => {
const result = renderToString(<Fixture />)

Expand Down

0 comments on commit dafd031

Please sign in to comment.