Using MDX in Astro

An example of using interactive components in markdown with MDX

mdxastrocomponents

Using MDX in Astro

MDX allows you to use JSX components directly in your markdown content. This is incredibly powerful for creating interactive and dynamic content!

What Can You Do With MDX?

With MDX, you can:

  1. Import and use components in your markdown
  2. Add interactive elements like charts, forms, or animations
  3. Create reusable content blocks with components
  4. Use JavaScript expressions in your content

Example: Callout Component

Here’s a custom callout component in action:

ℹ️

This is an info callout. You can use markdown inside components!

⚠️

⚠️ This is a warning callout. Perfect for important notes.

✅ This is a success callout. Great for highlighting achievements or completed tasks.

JavaScript Expressions

You can use JavaScript expressions inline:

The current year is 2026 and the current month is January.

Why Use MDX?

ℹ️

MDX combines the simplicity of Markdown with the power of React/JSX components, giving you the best of both worlds for content authoring.

Regular Markdown Still Works

You can still use all your favorite markdown features:

// Code blocks work perfectly
const greeting = 'Hello from MDX!';
console.log(greeting);

Interactive Components

Interactive Data Visualization

Here’s an interactive chart that visitors can customize. Switch between bar and line charts, toggle value labels, and see live statistics:

Monthly Page Views

Average: -
Max: -
Min: -

This is perfect for:

You can pass any data to this component and it will visualize it interactively!

Simple Counter Demo

Here’s a counter component with client-side JavaScript:

Count: 0

✓ Your count is automatically saved

And here’s a collapsible/toggle component:

Click to see the secret! 🎉

You found it! This content was hidden until you clicked. You can put any markdown content here:

  • Lists work
  • Formatting works
  • Everything works!

This uses the native HTML <details> element, so it’s accessible and works everywhere.

With Astro, you can add as much or as little interactivity as you need. Start with static content and add interactive components only where necessary!

Pretty cool, right? 🚀

Contact

If you'd like to get in touch connect with me on LinkedIn.

Connect on LinkedIn