Unverified Commit 42bcacd9 authored by Remco Haszing's avatar Remco Haszing Committed by GitHub

Remove useMDXComponents argument (#80871)

### What?

Fix the signatures of `useMDXComponents`.

### Why?

The `useMDXComponents` function is always called without any arguments.
It’s also always supposed to be a named export, not a default export.
It’s also called during render. Defining React components during render
is an anti-pattern. All components that were previously defined inside
of `useMDXComponents`, are now defined outside of the function.

### How?

Manual fixes.

---------
Co-authored-by: 's avatargraphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: 's avatarJoseph <sephxd1234@gmail.com>
Co-authored-by: 's avatarJoseph <joseph.chamochumbi@vercel.com>
parent 10e0798f
...@@ -77,18 +77,18 @@ Create an `mdx-components.tsx` (or `.js`) file in the root of your project to de ...@@ -77,18 +77,18 @@ Create an `mdx-components.tsx` (or `.js`) file in the root of your project to de
```tsx filename="mdx-components.tsx" switcher ```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types' import type { MDXComponents } from 'mdx/types'
export function useMDXComponents(components: MDXComponents): MDXComponents { const components: MDXComponents = {}
return {
...components, export function useMDXComponents(): MDXComponents {
} return components
} }
``` ```
```js filename="mdx-components.js" switcher ```js filename="mdx-components.js" switcher
export function useMDXComponents(components) { const components = {}
return {
...components, export function useMDXComponents() {
} return components
} }
``` ```
...@@ -357,8 +357,7 @@ import Image, { ImageProps } from 'next/image' ...@@ -357,8 +357,7 @@ import Image, { ImageProps } from 'next/image'
// React component you want, including inline styles, // React component you want, including inline styles,
// components from other libraries, and more. // components from other libraries, and more.
export function useMDXComponents(components: MDXComponents): MDXComponents { const components = {
return {
// Allows customizing built-in components, e.g. to add styling. // Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => ( h1: ({ children }) => (
<h1 style={{ color: 'red', fontSize: '48px' }}>{children}</h1> <h1 style={{ color: 'red', fontSize: '48px' }}>{children}</h1>
...@@ -370,8 +369,10 @@ export function useMDXComponents(components: MDXComponents): MDXComponents { ...@@ -370,8 +369,10 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
{...(props as ImageProps)} {...(props as ImageProps)}
/> />
), ),
...components, } satisfies MDXComponents
}
export function useMDXComponents(): MDXComponents {
return components
} }
``` ```
...@@ -383,21 +384,18 @@ import Image from 'next/image' ...@@ -383,21 +384,18 @@ import Image from 'next/image'
// React component you want, including inline styles, // React component you want, including inline styles,
// components from other libraries, and more. // components from other libraries, and more.
export function useMDXComponents(components) { const components = {
return {
// Allows customizing built-in components, e.g. to add styling. // Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => ( h1: ({ children }) => (
<h1 style={{ color: 'red', fontSize: '48px' }}>{children}</h1> <h1 style={{ color: 'red', fontSize: '48px' }}>{children}</h1>
), ),
img: (props) => ( img: (props) => (
<Image <Image sizes="100vw" style={{ width: '100%', height: 'auto' }} {...props} />
sizes="100vw"
style={{ width: '100%', height: 'auto' }}
{...props}
/>
), ),
...components, }
}
export function useMDXComponents() {
return components
} }
``` ```
......
...@@ -14,18 +14,18 @@ Use the file `mdx-components.tsx` (or `.js`) in the root of your project to defi ...@@ -14,18 +14,18 @@ Use the file `mdx-components.tsx` (or `.js`) in the root of your project to defi
```tsx filename="mdx-components.tsx" switcher ```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types' import type { MDXComponents } from 'mdx/types'
export function useMDXComponents(components: MDXComponents): MDXComponents { const components: MDXComponents = {}
return {
...components, export function useMDXComponents(): MDXComponents {
} return components
} }
``` ```
```js filename="mdx-components.js" switcher ```js filename="mdx-components.js" switcher
export function useMDXComponents(components) { const components = {}
return {
...components, export function useMDXComponents() {
} return components
} }
``` ```
...@@ -33,37 +33,26 @@ export function useMDXComponents(components) { ...@@ -33,37 +33,26 @@ export function useMDXComponents(components) {
### `useMDXComponents` function ### `useMDXComponents` function
The file must export a single function, either as a default export or named `useMDXComponents`. The file must export a single function named `useMDXComponents`. This function does not accept any arguments.
```tsx filename="mdx-components.tsx" switcher ```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types' import type { MDXComponents } from 'mdx/types'
export function useMDXComponents(components: MDXComponents): MDXComponents { const components: MDXComponents = {}
return {
...components, export function useMDXComponents(): MDXComponents {
} return components
} }
``` ```
```js filename="mdx-components.js" switcher ```js filename="mdx-components.js" switcher
export function useMDXComponents(components) { const components = {}
return {
...components, export function useMDXComponents() {
} return components
} }
``` ```
## Params
### `components`
When defining MDX Components, the export function accepts a single parameter, `components`. This parameter is an instance of `MDXComponents`.
- The key is the name of the HTML element to override.
- The value is the component to render instead.
> **Good to know**: Remember to pass all other components (i.e. `...components`) that do not have overrides.
## Version History ## Version History
| Version | Changes | | Version | Changes |
......
import type { MDXComponents } from "mdx/types"; import type { MDXComponents } from "mdx/types";
// This file is required to use MDX in `app` directory. const components: MDXComponents = {};
export function useMDXComponents(components: MDXComponents): MDXComponents {
return { // Allows customizing built-in components, e.g. to add styling.
// Allows customizing built-in components, e.g. to add styling. export function useMDXComponents(): MDXComponents {
// h1: ({ children }) => <h1 style={{ fontSize: "100px" }}>{children}</h1>, return components;
...components,
};
} }
...@@ -105,14 +105,13 @@ yarn add @next/mdx ...@@ -105,14 +105,13 @@ yarn add @next/mdx
Create an `mdx-components.js` file at the root of your project with the following contents: Create an `mdx-components.js` file at the root of your project with the following contents:
```js ```js
// This file is required to use @next/mdx in the `app` directory. // Allows customizing built-in components, e.g. to add styling.
export function useMDXComponents(components) { const components = {
return components
// Allows customizing built-in components, e.g. to add styling.
// return {
// h1: ({ children }) => <h1 style={{ fontSize: "100px" }}>{children}</h1>, // h1: ({ children }) => <h1 style={{ fontSize: "100px" }}>{children}</h1>,
// ...components, }
// }
export function useMDXComponents() {
return components
} }
``` ```
......
...@@ -15,11 +15,7 @@ import { Callout } from 'app/(post)/components/callout' ...@@ -15,11 +15,7 @@ import { Callout } from 'app/(post)/components/callout'
import { Ref, FootNotes, FootNote } from 'app/(post)/components/footnotes' import { Ref, FootNotes, FootNote } from 'app/(post)/components/footnotes'
import { Blockquote as blockquote } from 'app/(post)/components/blockquote' import { Blockquote as blockquote } from 'app/(post)/components/blockquote'
export function useMDXComponents(components: { const components = {
[component: string]: React.ComponentType
}) {
return {
...components,
a, a,
h1, h1,
h2, h2,
...@@ -40,5 +36,8 @@ export function useMDXComponents(components: { ...@@ -40,5 +36,8 @@ export function useMDXComponents(components: {
Ref, Ref,
FootNotes, FootNotes,
FootNote, FootNote,
} }
export function useMDXComponents() {
return components
} }
export const useMDXComponents = (components) => components
export function useMDXComponents(components) {
return {
...components,
}
}
export function useMDXComponents(components: unknown) {
return components
}
import { Marker } from './components/marker' import { Marker } from './components/marker'
export function useMDXComponents(components) { const components = {
return {
...components,
Marker, Marker,
} }
export function useMDXComponents() {
return components
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment