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

Pass through properties to top-level MDX component #292

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions .jest/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ export async function renderStatic(
components,
scope,
mdxOptions,
minifyOptions,
parseFrontmatter,
}: SerializeOptions & Pick<MDXRemoteProps, 'components'> = {}
): Promise<string> {
const mdxSource = await serialize(mdx, {
mdxOptions,
minifyOptions,
parseFrontmatter,
})

Expand Down
12 changes: 12 additions & 0 deletions __tests__/serialize.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ describe('serialize', () => {
expect(result).toMatchInlineSnapshot(`"<p>test</p>"`)
})

test('with scope props', async () => {
const result = await renderStatic('<p>{props.baz}</p>', {
scope: {
bar: 'test',
props: {
baz: 'test2',
},
},
})
expect(result).toMatchInlineSnapshot(`"<p>test2</p>"`)
})

test('with custom provider', async () => {
const TestContext = React.createContext(null)

Expand Down
12 changes: 8 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare global {

export type MDXRemoteProps = MDXRemoteSerializeResult & {
/**
* A object mapping names to React components.
* An object mapping names to React components.
* The key used will be the name accessible to MDX.
*
* For example: `{ ComponentName: Component }` will be accessible in the MDX as `<ComponentName/>`.
Expand All @@ -46,7 +46,7 @@ export { MDXRemoteSerializeResult }
export function MDXRemote({
compiledSource,
frontmatter,
scope,
scope: scopeWithProps,
components = {},
lazy,
}: MDXRemoteProps) {
Expand All @@ -67,6 +67,8 @@ export function MDXRemote({
}, [])

const Content: React.ElementType = useMemo(() => {
const { props = {}, ...scope } = scopeWithProps || {}

// if we're ready to render, we can assemble the component tree and let React do its thing
// first we set up the scope which has to include the mdx custom
// create element function as well as any components we're using
Expand All @@ -88,8 +90,10 @@ export function MDXRemote({
keys.concat(`${compiledSource}`)
)

return hydrateFn.apply(hydrateFn, values).default
}, [scope, compiledSource])
const Component = hydrateFn.apply(hydrateFn, values).default

return () => <Component {...props} />
}, [scopeWithProps, compiledSource])

if (!isReadyToRender) {
// If we're not ready to render, return an empty div to preserve SSR'd markup
Expand Down