MDX Notes
Build an Alert component for MDX — multiple variants (info, warning, error, success, tip), dismissible state, custom icons, and full accessibility.
Alerts (or callouts, or admonitions — pick your name) highlight the stuff readers shouldn't miss. Warnings, tips, errors, important notes. Plain text doesn't cut it for these — you need visual weight. Here's how to build a proper Alert component from scratch.
Why Alerts Matter in MDX
When you're writing docs or tutorials, some information is more important than the surrounding text. A well-styled alert says "hey, pay attention here" without you having to write that in words. Plus, they carry semantic meaning that screen readers can pick up on.
Component Source Code
Here's the full React implementation:
Usage in MDX
Import the component and drop it into your content:
import { Alert } from '../components/Alert';
# My Documentation Page
Here's some regular content in your MDX file.
<Alert variant="info" title="Note">
This is an informational alert that provides additional context
to the reader without interrupting the flow of content.
</Alert>
<Alert variant="warning" title="Caution">
Be careful when modifying configuration files. Always create
a backup before making changes.
</Alert>
<Alert variant="error" title="Breaking Change">
This API endpoint was removed in v3.0. Please migrate to the
new `/api/v3/users` endpoint immediately.
</Alert>
<Alert variant="success" title="Complete">
Your deployment was successful! The changes are now live
in production.
</Alert>
<Alert variant="tip" title="Pro Tip">
You can combine multiple MDX components together for richer
documentation experiences.
</Alert>Dismissible Alert Example
Let users close the alert once they've read it:
<Alert variant="info" title="First Time Here?" dismissible>
Welcome to our documentation! Check out the getting started
guide for a quick overview of key concepts.
</Alert>Custom Icon Example
Override the default emoji with whatever you want:
<Alert variant="info" title="Rocket Launch" icon="🚀">
Your application has been deployed to production successfully.
</Alert>Alerts with Rich MDX Content
You can put markdown, links, code, and lists inside alerts:
Props API Documentation
| Prop | Type | Default | Description | ||||
|---|---|---|---|---|---|---|---|
variant | `'info' \ | 'warning' \ | 'error' \ | 'success' \ | 'tip'` | 'info' | Visual style variant of the alert |
title | string | undefined | Optional title displayed in bold above content | ||||
children | ReactNode | required | The alert content (supports MDX/JSX) | ||||
dismissible | boolean | false | Whether the alert can be dismissed by the user | ||||
icon | `string \ | ReactNode` | variant default | Custom icon to override the default variant icon | |||
className | string | '' | Additional CSS classes for custom styling |
Styling with CSS Modules (Alternative to Tailwind)
Prefer plain CSS? Here's the equivalent:
/* Alert.module.css */
.alert {
border-left: 4px solid;
padding: 1rem;
margin: 1rem 0;
border-radius: 0 0.5rem 0.5rem 0;
display: flex;
align-items: flex-start;
}
.alert-info {
background-color: #eff6ff;
border-color: #60a5fa;
color: #1e40af;
}
.alert-warning {
background-color: #fffbeb;
border-color: #fbbf24;
color: #92400e;
}
.alert-error {
background-color: #fef2f2;
border-color: #f87171;
color: #991b1b;
}
.alert-success {
background-color: #f0fdf4;
border-color: #4ade80;
color: #166534;
}
.alert-tip {
background-color: #faf5ff;
border-color: #c084fc;
color: #6b21a8;
}
.alert-icon {
flex-shrink: 0;
margin-right: 0.75rem;
font-size: 1.25rem;
}
.alert-content {
flex: 1;
}
.alert-title {
font-weight: 600;
margin-bottom: 0.25rem;
}
.alert-dismiss {
margin-left: 0.75rem;
background: none;
border: none;
cursor: pointer;
opacity: 0.7;
transition: opacity 0.2s;
}
.alert-dismiss:hover {
opacity: 1;
}Icon Integration with React Icons
For production, you'll probably want real icons instead of emoji:
Accessibility Notes
Here's what makes this accessible and why each piece matters:
role="alert"— Tells screen readers this is an alert region. Content gets announced immediately when it appears.
aria-live="polite"— For non-critical alerts, screen readers wait for a pause before announcing. Usearia-live="assertive"for errors that need immediate attention.
aria-hidden="true"on icons — The icons are decorative. Screen readers skip them since the meaning comes from the text.
aria-label="Dismiss alert"on close button — The button only has an SVG icon, so it needs a text label for screen readers.
- Keyboard support — The dismiss button is a native
<button>, so it's focusable and works with Enter/Space automatically.
- Color is not the only indicator — Icons and text labels back up the color coding. Colorblind users can still tell alert types apart.
Advanced: Auto-Dismissing Alert
Want an alert that disappears after a few seconds?
MDXProvider Registration
Make alerts available globally so you don't need imports in every file:
Now authors can use shorthand without any imports:
<Tip title="Quick Shorthand">
When registered globally, you don't need imports!
</Tip>Summary
The Alert component covers the key patterns you'll reuse everywhere: variant-based styling, composable children, accessibility-first markup, and optional interactivity (dismissible state). Once you have this, you can build callouts, banners, toasts — same core pattern, different skin.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Building an Alert/Admonition Component.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this MDX Tutorial topic.
Search Terms
mdx-tutorial, mdx tutorial, mdx, tutorial, components, alert, component, building an alert/admonition component
Related MDX Tutorial Topics