
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
react-x-mermaid
Advanced tools
A small, lightweight React wrapper component and hook for rendering Mermaid diagrams from Mermaid syntax strings.
A small, lightweight React wrapper component and hook for rendering Mermaid diagrams from Mermaid syntax strings.
This package exposes a React component and a hook so you can render and control Mermaid diagrams in React apps with minimal setup.
Mermaid React component that accepts a Mermaid diagram string.useMermaid hook for programmatic control and rendering.Install from npm:
npm install react-x-mermaid mermaid
# or
yarn add react-x-mermaid mermaid
Render a diagram using the Mermaid component:
import "./App.css";
import RenderMermaid from "react-x-mermaid";
function App() {
const d1 = `
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
`;
return (
<div className="app">
<RenderMermaid mermaidCode={d1} mermaidConfig={{ theme: "dark" }} />
</div>
);
}
export default App;
Or use the useMermaid hook for manual rendering and lifecycle control:
import { useMermaid } from "react-x-mermaid";
function AdvanceMermaidRenderer({ chart }: { chart: string }) {
// hook also gives you svg along with ref and error which it generate which you can use to download and save locally if required
const { ref, error } = useMermaid(chart, {
theme: "dark", // mermaid config
});
if (error) {
// show error or render code in <pre> and <code> tags if you need as a fallback.
return <div className="mermaid__error">{error}</div>;
}
return <div className="mermaid-renderer" ref={ref} />;
}
export default AdvanceMermaidRenderer;
// App.tsx;
import "./App.css";
import AdvanceMermaidRenderer from "./AdvanceMermaidRenderer";
function App() {
const d1 = `
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
`;
return (
<div className="app">
<AdvanceMermaidRenderer chart={d1} />
</div>
);
}
export default App;
useMermaid HookA React hook that provides programmatic control over Mermaid diagram rendering with error handling and lifecycle management.
useMermaid(chart: string, config?: MermaidConfig) => {
ref: RefObject<HTMLDivElement | null>;
svg: string;
error: string | null;
}
| Parameter | Type | Required | Description |
|---|---|---|---|
chart | string | Yes | The Mermaid diagram syntax string to render |
config | MermaidConfig | No | Optional Mermaid configuration object (defaults to internal config) |
| Property | Type | Description |
|---|---|---|
ref | RefObject<HTMLDivElement | null> | React ref to attach to a DOM element where the diagram will be rendered |
svg | string | The rendered SVG string of the diagram (useful for downloading or further processing) |
error | string | null | Error message if diagram rendering fails, null if successful |
Basic Usage:
import { useMermaid } from "react-x-mermaid";
function MyDiagram() {
const chart = `
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
`;
const { ref, svg, error } = useMermaid(chart);
if (error) {
return <div>Error: {error}</div>;
}
return <div ref={ref} />;
}
With Custom Configuration:
import { useMermaid } from "react-x-mermaid";
function ThemedDiagram() {
const chart = "graph TD; A-->B;";
const { ref, error } = useMermaid(chart, {
theme: "dark",
flowchart: {
useMaxWidth: true,
htmlLabels: true,
},
});
return <div ref={ref} />;
}
Using SVG for Download:
import { useMermaid } from "react-x-mermaid";
function DownloadableDiagram() {
const chart = "graph TD; A-->B;";
const { ref, svg, error } = useMermaid(chart);
const downloadSVG = () => {
if (svg) {
const blob = new Blob([svg], { type: "image/svg+xml" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "diagram.svg";
a.click();
}
};
return (
<div>
<div ref={ref} />
{svg && <button onClick={downloadSVG}>Download SVG</button>}
</div>
);
}
chart or config parameters changeerror propertyrefThe hook gracefully handles various error scenarios:
When an error occurs, the error property contains the error message, and the svg property is cleared.
Mermaid ComponentA React component that renders Mermaid diagrams with built-in error handling, copy functionality, and SVG download capabilities.
interface RenderMermaidProps {
mermaidCode: string;
errorComponent?: React.ComponentType<{ error: string; mermaidCode: string }>;
disableDownload?: boolean;
disableCopy?: boolean;
downloadComponent?: React.ComponentType<{ onClick: () => void }>;
copyComponent?: React.ComponentType<{ onClick: () => void }>;
mermaidConfig?: MermaidConfig;
renderCode?: React.ComponentType<{ code: string }>;
}
const Mermaid: React.FC<RenderMermaidProps> = (props) => JSX.Element;
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
mermaidCode | string | Yes | - | The Mermaid diagram syntax string to render |
errorComponent | React.ComponentType<{ error: string; mermaidCode: string }> | No | - | Custom component to render when diagram fails to load |
disableDownload | boolean | No | false | When true, hides the download SVG button |
disableCopy | boolean | No | false | When true, hides the copy code button |
downloadComponent | React.ComponentType<{ onClick: () => void }> | No | - | Custom component for the download button (replaces default button) |
copyComponent | React.ComponentType<{ onClick: () => void }> | No | - | Custom component for the copy button (replaces default button) |
mermaidConfig | MermaidConfig | No | - | Mermaid configuration object passed to mermaid.initialize() |
renderCode | React.ComponentType<{ code: string }> | No | - | Custom component for rendering the raw Mermaid code (used in error state) |
React.memo for performance optimizationBasic Usage:
import Mermaid from "react-x-mermaid";
function App() {
const diagram = `
graph TD;
A[Start] --> B{Decision};
B -->|Yes| C[Action 1];
B -->|No| D[Action 2];
C --> E[End];
D --> E;
`;
return <Mermaid mermaidCode={diagram} />;
}
With Custom Configuration:
import Mermaid from "react-x-mermaid";
function ThemedDiagram() {
const diagram = "graph TD; A-->B;";
return (
<Mermaid
mermaidCode={diagram}
mermaidConfig={{
theme: "dark",
flowchart: {
useMaxWidth: true,
htmlLabels: true,
},
}}
/>
);
}
With Disabled Actions:
import Mermaid from "react-x-mermaid";
function SimpleDiagram() {
const diagram = "graph TD; A-->B;";
return (
<Mermaid mermaidCode={diagram} disableDownload={true} disableCopy={true} />
);
}
With Custom Error Component:
import Mermaid from "react-x-mermaid";
function CustomErrorComponent({ error, mermaidCode }) {
return (
<div style={{ padding: "20px", border: "1px solid red" }}>
<h3>Diagram Error</h3>
<p>{error}</p>
<details>
<summary>View Code</summary>
<pre>{mermaidCode}</pre>
</details>
</div>
);
}
function DiagramWithCustomError() {
const diagram = "invalid mermaid syntax";
return (
<Mermaid mermaidCode={diagram} errorComponent={CustomErrorComponent} />
);
}
With Custom Action Buttons:
import Mermaid from "react-x-mermaid";
function CustomCopyButton({ onClick }) {
return (
<button onClick={onClick} style={{ background: "blue", color: "white" }}>
📋 Copy Code
</button>
);
}
function CustomDownloadButton({ onClick }) {
return (
<button onClick={onClick} style={{ background: "green", color: "white" }}>
💾 Download
</button>
);
}
function DiagramWithCustomButtons() {
const diagram = "graph TD; A-->B;";
return (
<Mermaid
mermaidCode={diagram}
copyComponent={CustomCopyButton}
downloadComponent={CustomDownloadButton}
/>
);
}
With Custom Code Renderer:
import Mermaid from "react-x-mermaid";
function SyntaxHighlightedCode({ code }) {
return (
<div style={{ background: "#f5f5f5", padding: "10px" }}>
<h4>Mermaid Code:</h4>
<pre style={{ fontSize: "12px" }}>{code}</pre>
</div>
);
}
function DiagramWithCustomCodeRenderer() {
const diagram = "graph TD; A-->B;";
return <Mermaid mermaidCode={diagram} renderCode={SyntaxHighlightedCode} />;
}
The component applies the following CSS classes that you can style:
.mermaid-renderer - Main container.mermaid-actions - Container for action buttons.mermaid-action-button - Default action buttons.btn-copy - Copy button.btn-download - Download button.mermaid-diagram - Container for the rendered diagram.mermaid-error-container - Error state container.mermaid-code-renderer - Default code renderermermaidCode or mermaidConfig changesnavigator.clipboard.writeText() for copy functionalityContributions are welcome. Please open issues or PRs for bugs and improvements. Keep changes small and add tests for new behavior.
MIT
Keywords react react-componentmuimaterial-uimaterial design
FAQs
A small, lightweight React wrapper component and hook for rendering Mermaid diagrams from Mermaid syntax strings.
The npm package react-x-mermaid receives a total of 625 weekly downloads. As such, react-x-mermaid popularity was classified as not popular.
We found that react-x-mermaid demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.