Using MDX in Astro
An example of using interactive components in markdown with MDX
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:
- Import and use components in your markdown
- Add interactive elements like charts, forms, or animations
- Create reusable content blocks with components
- 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.
- ✅ Write markdown as usual
- ✅ Drop in components when you need them
- ✅ Keep your content maintainable and readable
- ✅ No need to write HTML for complex layouts
Regular Markdown Still Works
You can still use all your favorite markdown features:
- Lists
- Bold and italic
code blocks- Links and images
- And more!
// 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
This is perfect for:
- 📊 Displaying analytics or metrics
- 📈 Showing progress over time
- 💪 Fitness tracking (weight, reps, distance)
- 📚 Reading stats (books per month, pages read)
- ✈️ Travel data (countries visited, trips per year)
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? 🚀