File size: 7,126 Bytes
c36dde2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | # `@mdx-js/react`
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
React context for MDX.
<!-- more -->
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`MDXProvider(properties?)`](#mdxproviderproperties)
* [`useMDXComponents(components?)`](#usemdxcomponentscomponents)
* [`MergeComponents`](#mergecomponents)
* [`Props`](#props)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package is a *context* based components provider for combining React with
MDX.
## When should I use this?
This package is **not needed** for MDX to work with React.
See [¶ MDX provider in § Using MDX][use-provider] for when and how to use an MDX
provider.
If you use Next.js, **do not use this**.
Add an `mdx-components.tsx` (in `src/` or `/`) file instead.
See [Configuring MDX on `nextjs.org`][next-configuring-mdx] for more info.
## Install
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
```sh
npm install @mdx-js/react
```
In Deno with [`esm.sh`][esmsh]:
```tsx
import {MDXProvider} from 'https://esm.sh/@mdx-js/react@3'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import {MDXProvider} from 'https://esm.sh/@mdx-js/react@3?bundle'
</script>
```
## Use
```tsx
/**
* @import {MDXComponents} from 'mdx/types.js'
*/
import {MDXProvider} from '@mdx-js/react'
import Post from './post.mdx'
// ^-- Assumes an integration is used to compile MDX to JS, such as
// `@mdx-js/esbuild`, `@mdx-js/loader`, `@mdx-js/node-loader`, or
// `@mdx-js/rollup`, and that it is configured with
// `options.providerImportSource: '@mdx-js/react'`.
/** @type {MDXComponents} */
const components = {
em(properties) {
return <i {...properties} />
}
}
console.log(
<MDXProvider components={components}>
<Post />
</MDXProvider>
)
```
> 👉 **Note**: you don’t have to use `MDXProvider` and can pass components
> directly:
>
> ```diff
> -<MDXProvider components={components}>
> - <Post />
> -</MDXProvider>
> +<Post components={components} />
> ```
See [¶ React in § Getting started][start-react] for how to get started with MDX
and React.
See [¶ MDX provider in § Using MDX][use-provider] for how to use an MDX
provider.
## API
This package exports the identifiers [`MDXProvider`][api-mdx-provider] and
[`useMDXComponents`][api-use-mdx-components].
There is no default export.
### `MDXProvider(properties?)`
Provider for MDX context.
###### Parameters
* `properties` ([`Props`][api-props])
— configuration
##### Returns
Element (`ReactElement`).
### `useMDXComponents(components?)`
Get current components from the MDX Context.
###### Parameters
* `components` ([`MDXComponents` from `mdx/types.js`][mdx-types-components]
or [`MergeComponents`][api-merge-components], optional)
— additional components to use or a function that creates them
###### Returns
Current components ([`MDXComponents` from
`mdx/types.js`][mdx-types-components]).
### `MergeComponents`
Custom merge function (TypeScript type).
###### Parameters
* `components` ([`MDXComponents` from `mdx/types.js`][mdx-types-components])
— current components from the context
###### Returns
Additional components ([`MDXComponents` from
`mdx/types.js`][mdx-types-components]).
### `Props`
Configuration for `MDXProvider` (TypeScript type).
###### Fields
* `children` ([`ReactNode` from `react`][react-node],
optional)
— children
* `components` ([`MDXComponents` from `mdx/types.js`][mdx-types-components]
or [`MergeComponents`][api-merge-components], optional)
— additional components to use or a function that creates them
* `disableParentContext` (`boolean`, default: `false`)
— turn off outer component context
## Types
This package is fully typed with [TypeScript][].
It exports the additional types [`MergeComponents`][api-merge-components] and
[`Props`][api-props].
For types to work, make sure the TypeScript `JSX` namespace is typed.
This is done by installing and using the types of your framework, as in
[`@types/react`](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/react).
## Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, `@mdx-js/react@^3`,
compatible with Node.js 16.
## Security
See [§ Security][security] on our website for information.
## Contribute
See [§ Contribute][contribute] on our website for ways to get started.
See [§ Support][support] for ways to get help.
This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
## License
[MIT][] © Compositor and [Vercel][]
[api-mdx-provider]: #mdxproviderproperties
[api-merge-components]: #mergecomponents
[api-props]: #props
[api-use-mdx-components]: #usemdxcomponentscomponents
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[build]: https://github.com/mdx-js/mdx/actions
[build-badge]: https://github.com/mdx-js/mdx/workflows/main/badge.svg
[chat]: https://github.com/mdx-js/mdx/discussions
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[coc]: https://github.com/mdx-js/.github/blob/main/code-of-conduct.md
[collective]: https://opencollective.com/unified
[contribute]: https://mdxjs.com/community/contribute/
[coverage]: https://codecov.io/github/mdx-js/mdx
[coverage-badge]: https://img.shields.io/codecov/c/github/mdx-js/mdx/main.svg
[downloads]: https://www.npmjs.com/package/@mdx-js/react
[downloads-badge]: https://img.shields.io/npm/dm/@mdx-js/react.svg
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[mdx-types-components]: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/HEAD/types/mdx/types.d.ts#L65
[mit]: https://github.com/mdx-js/mdx/blob/main/packages/react/license
[next-configuring-mdx]: https://nextjs.org/docs/pages/building-your-application/configuring/mdx
[npm]: https://docs.npmjs.com/cli/install
[react-node]: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/HEAD/types/react/index.d.ts#L244
[security]: https://mdxjs.com/getting-started/#security
[size]: https://bundlejs.com/?q=@mdx-js/react
[size-badge]: https://img.shields.io/bundlejs/size/@mdx-js/react
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[start-react]: https://mdxjs.com/getting-started/#react
[support]: https://mdxjs.com/community/support/
[typescript]: https://www.typescriptlang.org
[use-provider]: https://mdxjs.com/docs/using-mdx/#mdx-provider
[vercel]: https://vercel.com
|