Omkar Arora

← Dev log#045 min read

Building interactive demos in MDX

Live previews, side-by-side comparisons and step-by-step walkthroughs inside MDX posts, each built from a small React client component.

Revised

On this page · 11

A code sample shows what to type. A demo lets the reader press the button and see what happens. This post covers the demo components this blog uses, and every one of them is running on this page.

Why Interactive Demos Matter

Interactive demos offer several key benefits:

  • Better Engagement: Readers can experiment with code directly in your blog post
  • Hands-on Learning: Visual understanding through interaction beats reading alone
  • Reduced Context Switching: No need to open a separate CodeSandbox or IDE
  • Immediate Feedback: See results instantly as you modify code
  • Better Retention: Active learning leads to better understanding

Prerequisites

This builds on the setup in How I built my blog with MDX, Next.js and Tailwind CSS. You need:

  • A Next.js blog with MDX support (using next-mdx-remote)
  • React components set up for MDX
  • Tailwind CSS for styling

Setting Up Interactive Components in MDX

The key to interactive demos in MDX is creating client components (components that use React hooks and browser APIs) and registering them in your MDX component map.

Understanding Client vs Server Components

Since we're using next-mdx-remote with the App Router, we need to mark interactive components with "use client" to enable React hooks and browser APIs.

Component Organization

I organize my interactive demo components in src/components/mdx/:

code
src/components/mdx/
├── LivePreview.tsx          # Live code preview component
├── CodePlayground.tsx       # Interactive code editor
├── InteractiveDemo.tsx      # Generic wrapper for demos
├── ComparisonDemo.tsx       # Side-by-side comparisons
├── StepByStepDemo.tsx       # Progressive disclosure
└── demos/
    ├── CounterDemo.tsx      # Example: Counter component
    ├── FormValidationDemo.tsx
    ├── ApiSimulatorDemo.tsx
    └── AnimationDemo.tsx

Interactive Demo Types

There are four kinds on this blog:

1. Live Code Preview

The LivePreview component displays code alongside a live React component preview. Perfect for demonstrating React components, hooks, and patterns.

Live

Counter Component

A simple counter demonstrating useState hook

Code
const Counter = () => {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
    </div>
  );
};

Preview

0

Here's how to use it in your MDX:

tsx
<LivePreview
	code={`const MyComponent = () => {
      return <div>Hello World</div>;
    };`}
	preview={<MyComponent />}
	title="My Demo"
	description="This is what it does"
/>

2. Interactive Demo Wrapper

The InteractiveDemo component is a simple wrapper for any interactive content. Use it to create consistent, styled containers for your demos.

Demo

Try It Yourself

Click the buttons to see the counter in action

0

Usage:

tsx
<InteractiveDemo title="Demo Title" description="What this demo shows">
	<YourComponent />
</InteractiveDemo>

3. Comparison Demos

Use ComparisonDemo to show side-by-side comparisons of different approaches, before/after states, or different implementations.

Compare

Compare two different approaches to the same problem

Class Component

The older class-based approach, with a render method

class MyComponent extends React.Component {
  render() {
    return <div>Hello</div>;
  }
}
Show code
class MyComponent extends React.Component {
  render() {
    return <div>Hello</div>;
  }
}

Function Component

Modern functional approach with hooks

const MyComponent = () => {
  return <div>Hello</div>;
};
Show code
const MyComponent = () => {
  return <div>Hello</div>;
};

4. Step-by-Step Demos

The StepByStepDemo component is perfect for progressive disclosure, walking readers through complex processes step by step.

Steps

Understanding React Hooks

Follow along to learn how useState works

Step 1: Import useState

First, import the useState hook from React:

code
import { useState } from 'react';
1 of 3

Practical Examples

Three demos built with the wrapper above:

Example 1: Form Validation Demo

Perfect for demonstrating form validation patterns, error handling, and user feedback.

Demo

Form Validation

Try submitting the form with invalid data to see validation in action

This demo shows:

  • Real-time validation feedback
  • Multiple validation rules
  • Error state management
  • User-friendly error messages

Example 2: API Call Simulator

Demonstrate loading states, error handling, and async operations.

Demo

API Call Simulation

Click the buttons to see different API response scenarios

Key features demonstrated:

  • Loading states
  • Success handling
  • Error handling
  • Async/await patterns

Example 3: Animation Demo

Show CSS animations, transitions, and interactive controls.

Demo

Animation Controls

Control the animation speed and playback

0.5x3x

This demonstrates:

  • CSS animations
  • Interactive controls
  • State-driven animations
  • User-controlled playback

Implementation Details

Component Architecture

Each interactive component follows a consistent structure:

tsx
"use client";
 
import { useState } from "react";
 
export function MyDemo() {
	const [state, setState] = useState(initialValue);
 
	return <div className="styled-container">{/* Interactive content */}</div>;
}

Key points:

  • Always use "use client" directive
  • Use React hooks for interactivity
  • Style with Tailwind CSS classes
  • Support dark mode with theme-aware classes

Styling with Tailwind CSS

All components use Tailwind's theme-aware classes for consistent styling:

tsx
// Background colors adapt to theme
className = "bg-background text-foreground";
 
// Borders use theme colors
className = "border-border";
 
// Muted colors for secondary content
className = "text-muted-foreground bg-muted";

State Management in Demos

For simple demos, local state with useState is sufficient:

tsx
const [count, setCount] = useState(0);

For more complex demos, you might use:

  • useReducer for complex state logic
  • Context API for shared state between demos
  • Custom hooks for reusable logic

Registering Components in MDX

To use these components in your MDX files, register them in your MDX components map:

tsx
// src/components/mdx/mdx.tsx
import { LivePreview } from "./LivePreview";
import { InteractiveDemo } from "./InteractiveDemo";
import { CounterDemo } from "./demos/CounterDemo";
// ... other imports
 
let components = {
	// ... existing components
	LivePreview,
	InteractiveDemo,
	CounterDemo,
	// ... other demo components
};
 
export function CustomMDX(props: MDXRemoteProps) {
	return (
		<MDXRemote
			{...props}
			components={{ ...components, ...(props.components || {}) }}
		/>
	);
}

Advanced Techniques

Lazy Loading Demos

For performance, you can lazy load heavy demo components:

tsx
import dynamic from "next/dynamic";
 
// The demos use named exports, so pick the one you want from the module.
const HeavyDemo = dynamic(
	() => import("./demos/HeavyDemo").then((mod) => mod.HeavyDemo),
	{ loading: () => <p>Loading demo...</p> }
);

Example used in this post:

tsx
import { LazyAnimationDemo } from "@/components/mdx/LazyAnimationDemo";
 
<LazyAnimationDemo />;
Demo

Lazy-loaded Animation Demo

This demo component is loaded on demand. This demo uses an artificial delay to show how heavier components are loaded on demand.

Loading animation demo...

This component loads on demand to keep the page fast.

Embedding External Tools

You can also embed external code playgrounds:

tsx
// CodeSandbox embed
<iframe
	src="https://codesandbox.io/embed/..."
	style={{ width: "100%", height: "500px" }}
	title="CodeSandbox demo"
/>

Example used in this post. The iframe runs on CodeSandbox's own origin, so its code never runs in your page; allow grants only the browser features it needs (here, copying to the clipboard):

Best Practices

When to Use Interactive Demos

Use interactive demos when:

  • ✅ Demonstrating concepts that benefit from experimentation
  • ✅ Showing before/after comparisons
  • ✅ Teaching through hands-on examples
  • ✅ Making complex topics more accessible

Avoid when:

  • ❌ Simple code snippets are sufficient
  • ❌ The demo would significantly slow page load
  • ❌ The concept doesn't benefit from interaction

Performance Considerations

  • Lazy load heavy demos
  • Code split large demo components
  • Optimize images and assets in demos
  • Monitor bundle size impact

Mobile Responsiveness

Ensure all demos work well on mobile:

  • Use responsive Tailwind classes
  • Test touch interactions
  • Consider mobile-first layouts
  • Provide fallbacks for mobile limitations

Accessibility

Make demos accessible:

  • Use semantic HTML
  • Add ARIA labels where needed
  • Ensure keyboard navigation works
  • Test with screen readers
  • Provide text alternatives

Troubleshooting

Component Not Rendering

If a component doesn't render in MDX:

  1. Check the "use client" directive: Client components need this
  2. Verify component registration: Ensure it's in the components map
  3. Check imports: Make sure all dependencies are imported
  4. Review console errors: Check browser console for errors
  5. Check that the props arrive: next-mdx-remote 6 strips JavaScript expressions out of MDX by default, so code={...} and steps={[...]} reach the component as undefined, with no error. Text props such as title="..." still work, which makes it easy to miss. Pass blockJS: false in the MDXRemote options if the posts are your own files.

That last one caught me. When I came back to this post, the live preview, comparison and step-by-step demos on this page were all rendering empty.

Styling Issues

If styles aren't applying:

  1. Check Tailwind classes: Ensure classes are valid
  2. Verify theme variables: Use theme-aware color classes
  3. Check CSS specificity: May need !important in rare cases
  4. Review dark mode: Test both light and dark themes

State Not Updating

If state isn't updating:

  1. Verify "use client": Required for hooks
  2. Check event handlers: Ensure they're properly bound
  3. Review state updates: Use functional updates if needed
  4. Check for re-renders: Component might be unmounting

Conclusion

A demo is worth building when pressing something teaches more than reading about it. Start with the plain wrapper and one small component. Add the comparison and step-by-step kinds when a post needs them.

Resources

Want to see the source code? Check out the GitHub repository for this blog.