Mermaid Diagram
Render interactive diagrams from Mermaid syntax with automatic loading and error states
Mermaid Diagram
Renders diagrams from Mermaid syntax with automatic loading and error state management. Supports flowcharts, sequence diagrams, class diagrams, state diagrams, Gantt charts, and all other Mermaid diagram types.
Installation
import { MermaidDiagram } from '@geckoui/geckoui';Basic Usage
'use client';
import { MermaidDiagram, Spinner } from '@geckoui/geckoui';
function Example() {
const diagram = `
flowchart LR
Start[User Login] --> Auth{Authenticated?}
Auth -->|Yes| Dashboard[Show Dashboard]
Auth -->|No| Login[Login Page]
Login --> Auth
`;
return <MermaidDiagram placeholder={<Spinner />}>{diagram}</MermaidDiagram>;
}Props API
| Prop | Type | Default | Description |
|---|---|---|---|
children | string | - | Mermaid diagram syntax (required) |
placeholder | ReactNode | FC | - | Component to show during loading |
renderError | ReactNode | FC<{message: string}> | <InputError> | Custom error component |
className | string | - | Additional CSS classes for SVG |
width | string | number | - | SVG width (overrides default) |
height | string | number | - | SVG height (overrides default) |
Additional: Accepts all standard SVG props except id, viewBox, and dangerouslySetInnerHTML.
Performance
The component automatically handles rendering performance:
- Async rendering: Diagrams render without blocking UI
- Loading states: Shows placeholder during processing
- Error boundaries: Catches and displays syntax errors
- DOM cleanup: Removes temporary elements after rendering
- Validation: Validates syntax before rendering
Browser Support
Requires browsers that support:
- SVG rendering
- ES6+ JavaScript
- Async/await
Works in all modern browsers (Chrome, Firefox, Safari, Edge).
Troubleshooting
Diagram not rendering
- Check syntax validity
- Ensure
childrenis a string - Look for console errors
- Verify Mermaid version compatibility
Syntax errors
Use Mermaid's Live Editor to validate syntax.
Performance issues
For complex diagrams:
- Simplify node count
- Split into multiple diagrams
- Use appropriate diagram type
Styling issues
The SVG inherits the original Mermaid classes. Use className to add custom styles:
<MermaidDiagram className="my-custom-styles">{diagram}</MermaidDiagram>TypeScript
import type { MermaidDiagramProps } from '@geckoui/geckoui';
const MyDiagram: React.FC<{ syntax: string }> = ({ syntax }) => {
return <MermaidDiagram placeholder={<Spinner />}>{syntax}</MermaidDiagram>;
};Accessibility
- SVG output is screen reader accessible
- Use descriptive node labels
- Provide text alternatives for complex diagrams
- Ensure sufficient color contrast
- Add ARIA labels if needed:
<MermaidDiagram aria-label="System architecture diagram">{diagram}</MermaidDiagram>Related Components
- Markdown - Render markdown with embedded diagrams
Related Hooks
- useMermaidDiagram - Underlying hook for custom implementations