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

Handle JSX member expressions #953

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 19 additions & 6 deletions packages/babel-plugin-apply-mdx-type-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@ const {types: t} = require('@babel/core')
const {declare} = require('@babel/helper-plugin-utils')
const {startsWithCapitalLetter} = require('@mdx-js/util')

const applyMdxType = (node, jsxName) => {
node.attributes.push(
t.jSXAttribute(t.jSXIdentifier(`mdxType`), t.stringLiteral(jsxName))
)
}

class BabelPluginApplyMdxTypeProp {
constructor() {
const names = []
this.state = {names}

this.plugin = declare(api => {
api.assertVersion(7)
const {types: t} = api

return {
visitor: {
JSXOpeningElement(path) {
if (t.isJSXMemberExpression(path.node.name)) {
const objName = path.node.name.object.name
const propName = path.node.name.property.name
const fullJsxName = `${objName}.${propName}`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would also need to be checked if we swapped to something more full featured in the getMemberExpression call. Perhaps we should add a warning if there are too many nested objects.


names.push(objName)
names.push(fullJsxName)

applyMdxType(path.node, fullJsxName)
}

const jsxName = path.node.name.name

if (startsWithCapitalLetter(jsxName)) {
names.push(jsxName)

path.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(`mdxType`),
t.stringLiteral(jsxName)
)
)
applyMdxType(path.node, jsxName)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/mdx/mdx-hast-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ MDXContent.isMDXComponent = true`
` +
uniq(jsxNames)
.filter(name => !importExportNames.includes(name))
.map(name => `const ${name} = makeShortcode("${name}");`)
.map(name => {
if (name.includes('.')) {
return `${name} = ${name} || makeShortcode("${name}");`
}

return `const ${name} = makeShortcode("${name}");`
})
.join('\n')

const moduleBase = `${importStatements}
Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/test/fixtures/export-with-shortcode.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const Foo = () => (
export const Foobar = () => (
<div>
<Button />
</div>
)

# Hello, world!

<Foo />
<Foobar />
4 changes: 3 additions & 1 deletion packages/mdx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ it('Should not include export wrapper if skipExport is true', async () => {

it('Should recognize components as properties', async () => {
const result = await mdx('# Hello\n\n<MDX.Foo />')
expect(dropWhitespace(result)).toContain('<h1>{`Hello`}</h1> <MDX.Foo />')
expect(dropWhitespace(result)).toContain(
'<h1>{`Hello`}</h1> <MDX.Foo mdxType="MDX.Foo" />'
)
})

it('Should contain static isMDXComponent() function', async () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/mdx/test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const components = {
React.createElement('h1', {style: {color: 'tomato'}}, children),
Button: () => React.createElement('button', {}, 'Hello, button!'),
del: ({children}) =>
React.createElement('del', {style: {color: 'crimson'}}, children)
React.createElement('del', {style: {color: 'crimson'}}, children),
Foo: {
Bar: ({children}) =>
React.createElement('p', {style: {color: 'salmon'}}, children)
}
}

it('renders using components from context', async () => {
Expand Down Expand Up @@ -75,3 +79,9 @@ it('renders delete component from context', async () => {
'<p>Hello, <del style="color:crimson">world</del> MDX!</p>'
)
})

it('renders the jsx element with that uses a member expression', async () => {
const result = await renderWithReact(`<Foo.Bar />`, {components})

expect(result).toContain('<p style="color:salmon"')
})
12 changes: 12 additions & 0 deletions packages/react/src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ const MDXCreateElement = forwardRef((props, ref) => {

const components = useMDXComponents(propComponents)
const type = mdxType

const getMemberExpressionElement = () => {
if (!type.includes('.')) {
return
}

const [objName, propName] = type.split('.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this not handle <Foo.Bar.Baz /> or am I misunderstanding this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't. This is mostly a temporary monkey patch that will be better addressed in the v2 branch.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we wanted this to be more fully supported we could copy/pasta lodash's get code, or something similar: https://lodash.com/docs/4.17.15#get

const component = (components[objName] || {})[propName]
return component
}

const Component =
components[`${parentName}.${type}`] ||
components[type] ||
getMemberExpressionElement() ||
DEFAULTS[type] ||
originalType

Expand Down