Skip to content

Commit

Permalink
preact: remove deprecated, MDXContext, withMDXComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 18, 2023
1 parent 5afa48e commit 4f92422
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 113 deletions.
7 changes: 1 addition & 6 deletions packages/preact/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export {
MDXContext,
MDXProvider,
useMDXComponents,
withMDXComponents
} from './lib/index.js'
export {MDXProvider, useMDXComponents} from './lib/index.js'
41 changes: 1 addition & 40 deletions packages/preact/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @typedef {import('mdx/types.js').MDXComponents} Components
* @typedef {import('preact').ComponentChildren} ComponentChildren
* @typedef {import('preact').Context<Components>} Context
*/

/**
Expand All @@ -25,45 +24,7 @@
import {createContext, h} from 'preact'
import {useContext} from 'preact/hooks'

/**
* @type {Context}
* Context.
* @deprecated
* This export is marked as a legacy feature.
* That means it’s no longer recommended for use as it might be removed
* in a future major release.
*
* Please use `useMDXComponents` to get context based components and
* `MDXProvider` to set context based components instead.
*/
export const MDXContext = createContext({})

/**
* @param {import('preact').ComponentType<any>} Component
* Component.
* @deprecated
* This export is marked as a legacy feature.
* That means it’s no longer recommended for use as it might be removed
* in a future major release.
*
* Please use `useMDXComponents` to get context based components instead.
* @returns
* Bound component.
*/
export function withMDXComponents(Component) {
return boundMDXComponent

/**
* @param {Record<string, unknown> & {components?: Components | null | undefined}} props
* Props.
* @returns {JSX.Element}
* Element.
*/
function boundMDXComponent(props) {
const allComponents = useMDXComponents(props.components)
return h(Component, {...props, allComponents})
}
}
const MDXContext = createContext({})

/**
* Get current components from the MDX Context.
Expand Down
25 changes: 2 additions & 23 deletions packages/preact/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Preact context for MDX.
* [API](#api)
* [`MDXProvider(props?)`](#mdxproviderprops)
* [`useMDXComponents(components?)`](#usemdxcomponentscomponents)
* [`MDXContext`](#mdxcontext)
* [`withMDXComponents(Component)`](#withmdxcomponentscomponent)
* [Types](#types)
* [Security](#security)
* [Contribute](#contribute)
Expand Down Expand Up @@ -92,8 +90,8 @@ provider.

## API

This package exports the following identifiers: `MDXContext`, `MDXProvider`,
`useMDXComponents`, and `withMDXComponents`.
This package exports the following identifiers: `MDXProvider` and
`useMDXComponents`.
There is no default export.

### `MDXProvider(props?)`
Expand Down Expand Up @@ -135,25 +133,6 @@ Components`).

`Components`.

### `MDXContext`

> 🪦 **Deprecated**: This export is not recommended for use as it exposes
> internals which should be hidden.
> It might be removed in a future major release.
> Please use `useMDXComponents` to get context based components and
> `MDXProvider` to set context based components instead.
The Preact Context for MDX (`Preact.Context`).

### `withMDXComponents(Component)`

> 🪦 **Deprecated**: This export is not recommended for use.
> It might be removed in a future major release.
> Please use `useMDXComponents` to get context based components instead.
Create a HOC of `Components` which is given the current context based MDX
components.

## Types

This package is fully typed with [TypeScript][].
Expand Down
46 changes: 2 additions & 44 deletions packages/preact/test/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import assert from 'node:assert/strict'
import {test} from 'node:test'
import {evaluate} from '@mdx-js/mdx'
import {MDXProvider, useMDXComponents, withMDXComponents} from '@mdx-js/preact'
import {MDXProvider, useMDXComponents} from '@mdx-js/preact'
import * as runtime_ from 'preact/jsx-runtime'
import {render} from 'preact-render-to-string'

Expand All @@ -16,10 +16,8 @@ const runtime = /** @type {RuntimeProduction} */ (runtime_)
test('@mdx-js/preact', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('@mdx-js/preact')).sort(), [
'MDXContext',
'MDXProvider',
'useMDXComponents',
'withMDXComponents'
'useMDXComponents'
])
})

Expand Down Expand Up @@ -209,44 +207,4 @@ test('@mdx-js/preact', async function (t) {
)
}
)

await t.test('should support `withComponents`', async function () {
const {default: Content} = await evaluate('# hi\n## hello', {
...runtime,
useMDXComponents
})
// Unknown props.
// type-coverage:ignore-next-line
const With = withMDXComponents(function (props) {
// Unknown props.
// type-coverage:ignore-next-line
return props.children
})

// Bug: this should use the `h2` component too, logically?
// As `withMDXComponents` is deprecated, and it would probably be a breaking
// change, we can just remove it later.
assert.equal(
render(
<MDXProvider
components={{
h1(props) {
return <h1 style={{color: 'tomato'}} {...props} />
}
}}
>
<With
components={{
h2(props) {
return <h2 style={{color: 'papayawhip'}} {...props} />
}
}}
>
<Content />
</With>
</MDXProvider>
),
'<h1 style="color:tomato;">hi</h1>\n<h2>hello</h2>'
)
})
})

0 comments on commit 4f92422

Please sign in to comment.