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

Layout props #189

Merged
merged 2 commits into from
Jul 26, 2018
Merged

Layout props #189

merged 2 commits into from
Jul 26, 2018

Conversation

silvenon
Copy link
Contributor

@silvenon silvenon commented Jul 15, 2018

I'm sure that this solution for layout props isn't ideal, but it's a crucial piece of the puzzle for integrating MDX into some tools. The API can always be changed later. Also, this is a breaking change to anyone who decided to use the components prop in their MDX file, because now that should become props.components.

With this change we'll be able to do stuff like this:

posts/layout-props.mdx:

import Layout from '../layouts/post'

# Layout Props!

Passing additional props to custom layouts now works!

export default ({ children, timeStamp }) => <Layout timeStamp={timeStamp}>{children}</Layout>

pages/index.js:

import Post from '../posts/layout-props.mdx'

export default () => (
  <Post
    comonents={{}}
    timeStamp={Date.now()}
  />
)

Would resolve:

Copy link
Member

@johno johno left a comment

Choose a reason for hiding this comment

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

🥇 Looking good so far!

Mind adding a test to ensure that props are passed to the layout?

'\n' +
`export default ({components}) => <MDXTag name="wrapper" ${layout ? `Layout={${layout}}` : ''} components={components}>${jsxNodes
`export default (props) => <MDXTag name="wrapper" ${layout ? `Layout={${layout}} layoutProps={objectWithoutProperties(props, ['components'])}` : ''} components={props.components}>${jsxNodes
Copy link
Member

Choose a reason for hiding this comment

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

Since the output JSX still requires babel parsing maybe it makes sense to use destructuring?

export default ({ components, ...props }) =>

Copy link
Contributor Author

@silvenon silvenon Jul 23, 2018

Choose a reason for hiding this comment

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

Great idea! That way I won't be breaking anything, and the change set will be cleaner.

silvenon added 2 commits July 23, 2018 17:27
The component exported by MDX files recognized only one prop:
components. That was the only prop being passed to the custom layout
component, which made MDX very hard to integrate with tools like Gatsby.

This change passes all props to the custom layout component, not only
"components".

Fixes #187.
@silvenon
Copy link
Contributor Author

Done. I added that test to @mdx-js/runtime tests, I hope that's fine. There's also a @mdx-js/tag test which tests if layoutProps are applied to the layout component.

@silvenon
Copy link
Contributor Author

I'm not sure why tests for Node 8.11.3 are failing. I tried locally with that version and it works.

@johno
Copy link
Member

johno commented Jul 23, 2018

This is great, thanks!

Looks like the failed test was a timeout blip that happens every once in a while. Just reran it and all seems good 💟

@johno johno merged commit 12686f3 into mdx-js:master Jul 26, 2018
@silvenon silvenon deleted the layout-props branch July 27, 2018 01:22
johno pushed a commit that referenced this pull request Jul 28, 2018
* Git-ignore Lerna's debug log file

* Pass rest of the props to custom Layout component

The component exported by MDX files recognized only one prop:
components. That was the only prop being passed to the custom layout
component, which made MDX very hard to integrate with tools like Gatsby.

This change passes all props to the custom layout component, not only
"components".

Fixes #187.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants