Skip to content

Commit

Permalink
feat: add react-dom support (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
loiacon committed Jul 10, 2021
1 parent a746363 commit 1ba3925
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,35 @@ test('should support over-riding the entry point', async () => {
assert.match(container.innerHTML, 'Thanks for being willing to contribute')
})

test('should work with react-dom api', async () => {
const mdxSource = `
import Demo from './demo'
<Demo />
`.trim()

const result = await bundleMDX(mdxSource, {
files: {
'./demo.tsx': `
import * as ReactDOM from 'react-dom'
function Demo() {
return ReactDOM.createPortal(
<div>Portal!</div>,
document.body
)
}
export default Demo
`.trim()
}
})

const Component = getMDXComponent(result.code)

const {container} = render(React.createElement(Component), { container: document.body })

assert.match(container.innerHTML, 'Portal!')
})

test.run()
3 changes: 2 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react'
import * as _jsx_runtime from 'react/jsx-runtime.js'
import * as ReactDOM from 'react-dom'

/**
* @typedef {{[name: string]: React.ComponentType | string | ComponentMap}} ComponentMap
Expand All @@ -16,7 +17,7 @@ import * as _jsx_runtime from 'react/jsx-runtime.js'
* @return {React.FunctionComponent<MDXContentProps>}
*/
function getMDXComponent(code, globals) {
const scope = {React, _jsx_runtime, ...globals}
const scope = {React, ReactDOM, _jsx_runtime, ...globals}
// eslint-disable-next-line
const fn = new Function(...Object.keys(scope), code)
return fn(...Object.values(scope))
Expand Down

0 comments on commit 1ba3925

Please sign in to comment.