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

Support MDX provider #399

Closed
4 tasks done
remcohaszing opened this issue Feb 22, 2024 · 1 comment · Fixed by #402
Closed
4 tasks done

Support MDX provider #399

remcohaszing opened this issue Feb 22, 2024 · 1 comment · Fixed by #402
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 👶 semver/patch This is a backwards-compatible fix 🦋 type/enhancement This is great to have

Comments

@remcohaszing
Copy link
Member

Initial checklist

Problem

#260 adds support for the components prop. Another common way to inject custom components is using an MDX provider.

Solution

Let the user define which components are provided via a reserved name, let’s call it MDXProvidedComponents. Much like Props, the language server assumes it will be available with some fallback logic. That means a user can write the following code to have the provided components available on a per-file basis. This is needed if multiple MDX providers are used.

// types.ts
import { ReactNode } from 'react'

interface PlanetProps {
  name: string
}

export interface MDXProvidedComponents {
  Planet: (props: PlanetProps) => ReactNode
}
# MDX Document

{/** @typedef {import('./types.js').MDXProvidedComponents} MDXProvidedComponents */}

<Planet name="Saturn" />

TypeScript also supports global types. This means that alternatively, the user can define the type globally and have it available in every document. This is the ideal solution if there’s only one MDX provider in the entire tree.

// types.ts
import { ReactNode } from 'react'

interface PlanetProps {
  name: string
}

declare global {
  interface MDXProvidedComponents {
    Planet: (props: PlanetProps) => ReactNode
  }
}
# MDX Document

<Planet name="Saturn" />

Alternatives

I looked into augmenting @types/mdx, but I don’t think that’s great. The MDXComponents is very loose, allows injecting any key. This is great for the developer DX we’re providing without this more advanced MDX language tooling, but we can do better if the user uses the MDX language server. I.e. it reduces the usefulness of autocomplete and validation.

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Feb 22, 2024
@remcohaszing remcohaszing added 🦋 type/enhancement This is great to have 🗄 area/interface This affects the public interface 👶 semver/patch This is a backwards-compatible fix labels Feb 22, 2024
remcohaszing added a commit that referenced this issue Feb 26, 2024
This adds support for components provided via the `providerImportSource`
option.

The user can specify which components are available by specifying the
`MDXProvidedComponents` type either locally inside the MDX file or for
all MDX files by declaring it globally.

Closes #399
remcohaszing added a commit that referenced this issue Feb 26, 2024
This adds support for components provided via the `providerImportSource`
option.

The user can specify which components are available by specifying the
`MDXProvidedComponents` type either locally inside the MDX file or for
all MDX files by declaring it globally.

Closes #399
remcohaszing added a commit that referenced this issue Feb 26, 2024
This adds support for components provided via the `providerImportSource`
option.

The user can specify which components are available by specifying the
`MDXProvidedComponents` type either locally inside the MDX file or for
all MDX files by declaring it globally.

Closes #399

This comment has been minimized.

@remcohaszing remcohaszing added the 💪 phase/solved Post is done label Feb 26, 2024
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 👶 semver/patch This is a backwards-compatible fix 🦋 type/enhancement This is great to have
Development

Successfully merging a pull request may close this issue.

1 participant