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:useMDXComponents- Merges custom components with contextuseThemedStylesWithMdx- Wraps components with theme stylesMDXProvider- Provides components to MDX contentThemeUIProvider- Provides the theme context
useThemedStylesWithMdx Hook
TheuseThemedStylesWithMdx hook applies theme styles to MDX components.
Implementation
Usage
Themed Components
TheThemed object provides components that automatically use styles from theme.styles.
Available Components
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 intheme.styles:
Custom MDX Components
Provide custom components to replace default MDX elements:Using Custom Components in MDX
Helper Functions
themed() Helper
Extract styles fromtheme.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
- Import
Themedfrom@theme-ui/mdx, nottheme-ui - Use
useThemedStylesWithMdxhook - Wrap your app with
MDXProvider Themedis no longer a component (was alias forThemed.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:
