-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMermaidDiagram.jsx
More file actions
82 lines (74 loc) · 2.19 KB
/
MermaidDiagram.jsx
File metadata and controls
82 lines (74 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useEffect, useState } from 'react';
import mermaid from 'mermaid';
const MermaidDiagram = ({ chart, theme = 'dark', className }) => {
const [svg, setSvg] = useState('');
const [error, setError] = useState(null);
useEffect(() => {
mermaid.initialize({
startOnLoad: false,
theme: theme,
securityLevel: 'loose',
fontFamily: "'JetBrains Mono', monospace",
useMaxWidth: false,
htmlLabels: true,
flowchart: {
padding: 15,
useMaxWidth: false,
htmlLabels: true,
},
themeVariables: {
fontSize: '16px',
},
});
}, [theme]);
useEffect(() => {
const renderChart = async () => {
if (!chart) return;
try {
const id = `mermaid-${Math.random().toString(36).substr(2, 9)}`;
// mermaid.render returns an object { svg } in newer versions
const { svg } = await mermaid.render(id, chart);
setSvg(svg);
setError(null);
} catch (err) {
console.error('Mermaid render error:', err);
// Mermaid might leave error text in the DOM, so we can also show a friendly message
setError('Failed to render diagram. Check syntax.');
}
};
renderChart();
}, [chart]);
if (error) {
return (
<div className="p-4 my-4 border border-red-500/20 bg-red-500/10 rounded text-red-400 text-sm font-mono">
{error}
<pre className="mt-2 text-xs opacity-50 overflow-auto">{chart}</pre>
</div>
);
}
const containerClass = className || "mermaid-container my-8 bg-gray-900/30 p-6 rounded-lg overflow-x-auto";
return (
<div className={containerClass}>
<style>
{`
.mermaid-container svg {
overflow: visible !important;
max-width: none !important;
height: auto !important;
}
.mermaid-container foreignObject {
overflow: visible !important;
}
.mermaid-container .label {
color: inherit;
}
`}
</style>
<div
className="flex justify-center min-w-full"
dangerouslySetInnerHTML={{ __html: svg }}
/>
</div>
);
};
export default MermaidDiagram;