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

feat: pass in react #859

Merged
merged 1 commit into from
Apr 12, 2024
Merged
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
35 changes: 35 additions & 0 deletions __tests__/react.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { compile, run } from '../index';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

describe('import React', () => {
it('allows importing react', async () => {
const mdx = `
import { useState } from 'react';

export default function Counter() {
const [count, setCount] = useState(0)

return (
<div>
<p>You clicked {count} times!</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
)
}

<Counter />
`;

const Content = await run(compile(mdx));
render(<Content />);

expect(screen.getByText('You clicked 0 times!')).toBeVisible();
userEvent.click(screen.getByRole('button'));

await waitFor(() => screen.getByText('You clicked 1 times!'));
});
});
8 changes: 5 additions & 3 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import { options } from './options';
require('./styles/main.scss');

import calloutTransformer from './processor/transform/callouts';
import react from 'react';
import React from 'react';

const unimplemented = debug('mdx:unimplemented');

type RunOpts = Omit<RunOptions, 'Fragment'> & {
components?: Record<string, React.Component>;
imports?: Record<string, unknown>;
};

export { Components };
Expand Down Expand Up @@ -54,7 +55,7 @@ export const compile = (text: string, opts = {}) => {
remarkPlugins: [calloutTransformer],
...opts,
}),
);
).replace(/await import\(_resolveDynamicMdxSpecifier\('react'\)\)/, 'arguments[0].imports.React');
};

export const run = async (code: string, _opts: RunOpts = {}) => {
Expand All @@ -64,7 +65,8 @@ export const run = async (code: string, _opts: RunOpts = {}) => {
const file = await mdxRun(code, {
...runtime,
Fragment,
baseUrl: '',
baseUrl: import.meta.url,
imports: { React },
useMDXComponents: makeUseMDXComponents(components),
...opts,
});
Expand Down
Loading
Loading