Skip to main content
Theme UI provides first-class support for MDX, allowing you to style markdown content with your theme.

Installation

Install the required packages:
As of Theme UI v0.15, MDX support is opt-in. You must explicitly install and configure @theme-ui/mdx.

Basic Setup

Theme UI works with both MDX v2 (recommended) and MDX v1.

With MDX v2

Configuration Explained

Here’s what each part does:
  1. useMDXComponents - Merges custom components with context
  2. useThemedStylesWithMdx - Wraps components with theme styles
  3. MDXProvider - Provides components to MDX content
  4. ThemeUIProvider - Provides the theme context

useThemedStylesWithMdx Hook

The useThemedStylesWithMdx hook applies theme styles to MDX components.

Implementation

Usage

Themed Components

The Themed object provides components that automatically use styles from theme.styles.

Available Components

In Theme UI v0.15+, Themed is no longer a component itself (previously it was an alias for Themed.div). Use Themed.div explicitly if needed.

Using Themed Outside MDX

Themed components can be used in regular JSX to match MDX styling:

sx Prop Support

Themed components accept the sx prop for customization:

Styling MDX Elements

Define styles for MDX elements in theme.styles:

Custom MDX Components

Provide custom components to replace default MDX elements:

Using Custom Components in MDX

Style these in your theme:

Helper Functions

themed() Helper

Extract styles from theme.styles:

defaultMdxComponents

Access the default component mapping:

Migration from v0.14

Theme UI v0.15 made MDX opt-in. Here’s how to migrate:

Key Changes

  1. Import Themed from @theme-ui/mdx, not theme-ui
  2. Use useThemedStylesWithMdx hook
  3. Wrap your app with MDXProvider
  4. Themed is no longer a component (was alias for Themed.div)

Complete Example

Here’s a complete setup with custom components and theming:

Framework Integration

See the Gatsby Plugin guide for Gatsby-specific setup. For Next.js with MDX, combine the above setup with @next/mdx: