react-super-mermaid 0.1.0 โ 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +338 -165
- package/dist/index.cjs +560 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -2
- package/dist/index.d.ts +30 -2
- package/dist/index.js +560 -17
- package/dist/index.js.map +1 -1
- package/llms.txt +41 -0
- package/package.json +84 -83
package/README.md
CHANGED
|
@@ -1,165 +1,338 @@
|
|
|
1
|
-
# react-super-mermaid
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
1
|
+
# react-super-mermaid
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/react-super-mermaid)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[](#)
|
|
6
|
+
|
|
7
|
+
> Drop-in React **Mermaid** viewer โ beautified themes (Colorful + Excalidraw **sketch**), pan/zoom, in-diagram search, and SVG/PNG export. Loads mermaid **externally** (injected, peer, or CDN) so your app stays light. TypeScript, zero-config styling.
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<img src="assets/hero-colorful.svg" alt="colorful theme preview" width="92%" />
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { MermaidViewer } from 'react-super-mermaid';
|
|
15
|
+
|
|
16
|
+
<MermaidViewer code={`flowchart LR\n A[Start] --> B{OK?} --> C[Done]`} toolbar />;
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Two signature themes
|
|
20
|
+
|
|
21
|
+
The same diagram, re-styled after mermaid renders โ no config:
|
|
22
|
+
|
|
23
|
+
| `theme="colorful"` | `theme="sketch"` |
|
|
24
|
+
| :---: | :---: |
|
|
25
|
+
| <img src="assets/hero-colorful.svg" alt="colorful" width="100%" /> | <img src="assets/hero-sketch.svg" alt="sketch" width="100%" /> |
|
|
26
|
+
| modern palette ยท soft shadows ยท slate edges | Excalidraw hand-drawn ยท wobble ยท handwriting font |
|
|
27
|
+
|
|
28
|
+
Plus mermaid's native `default` / `dark` / `neutral` / `forest` and `auto`.
|
|
29
|
+
|
|
30
|
+
> The previews above are styled with the package's real palettes. Run the demo to see live, interactive output (see [Demo](#demo)).
|
|
31
|
+
|
|
32
|
+
## Diagram types
|
|
33
|
+
|
|
34
|
+
Anything mermaid can draw, `<MermaidViewer>` renders โ with pan/zoom, search and export on top. The `colorful` theme adds a modern palette, soft shadows and slate edges to flowchart / sequence / class / state / ER, plus vibrant per-type colouring for pie / gantt / mindmap / timeline / journey; `sketch` brings the Excalidraw hand-drawn look. Every theme (including mermaid's native ones) gets a font-weight legibility boost so labels stay crisp. The blocks below render live on GitHub.
|
|
35
|
+
|
|
36
|
+
### Flowchart
|
|
37
|
+
|
|
38
|
+
```mermaid
|
|
39
|
+
flowchart LR
|
|
40
|
+
U[User] --> B{Logged in?}
|
|
41
|
+
B -- yes --> C[Lobby]
|
|
42
|
+
B -- no --> D[Login]
|
|
43
|
+
C --> E[(DB)]
|
|
44
|
+
D --> E
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Sequence
|
|
48
|
+
|
|
49
|
+
```mermaid
|
|
50
|
+
sequenceDiagram
|
|
51
|
+
participant U as User
|
|
52
|
+
participant API
|
|
53
|
+
participant DB
|
|
54
|
+
U->>API: request
|
|
55
|
+
API->>DB: query
|
|
56
|
+
DB-->>API: rows
|
|
57
|
+
API-->>U: response
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Class
|
|
61
|
+
|
|
62
|
+
```mermaid
|
|
63
|
+
classDiagram
|
|
64
|
+
class Animal {
|
|
65
|
+
+String name
|
|
66
|
+
+move()
|
|
67
|
+
}
|
|
68
|
+
class Dog {
|
|
69
|
+
+bark()
|
|
70
|
+
}
|
|
71
|
+
Animal <|-- Dog
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### State
|
|
75
|
+
|
|
76
|
+
```mermaid
|
|
77
|
+
stateDiagram-v2
|
|
78
|
+
[*] --> Idle
|
|
79
|
+
Idle --> Running : start
|
|
80
|
+
Running --> Idle : stop
|
|
81
|
+
Running --> [*]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Entity Relationship
|
|
85
|
+
|
|
86
|
+
```mermaid
|
|
87
|
+
erDiagram
|
|
88
|
+
CUSTOMER ||--o{ ORDER : places
|
|
89
|
+
ORDER ||--|{ LINE_ITEM : contains
|
|
90
|
+
PRODUCT ||--o{ LINE_ITEM : "ordered in"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Gantt
|
|
94
|
+
|
|
95
|
+
```mermaid
|
|
96
|
+
gantt
|
|
97
|
+
title Sprint
|
|
98
|
+
dateFormat YYYY-MM-DD
|
|
99
|
+
section Dev
|
|
100
|
+
Design :a1, 2026-01-01, 3d
|
|
101
|
+
Build :after a1, 5d
|
|
102
|
+
section QA
|
|
103
|
+
Test :2026-01-09, 3d
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Pie
|
|
107
|
+
|
|
108
|
+
```mermaid
|
|
109
|
+
pie title Traffic
|
|
110
|
+
"Direct" : 45
|
|
111
|
+
"Search" : 30
|
|
112
|
+
"Social" : 25
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Mindmap
|
|
116
|
+
|
|
117
|
+
```mermaid
|
|
118
|
+
mindmap
|
|
119
|
+
root((mermaid))
|
|
120
|
+
Diagrams
|
|
121
|
+
Flowchart
|
|
122
|
+
Sequence
|
|
123
|
+
Themes
|
|
124
|
+
Colorful
|
|
125
|
+
Sketch
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Timeline
|
|
129
|
+
|
|
130
|
+
```mermaid
|
|
131
|
+
timeline
|
|
132
|
+
title Releases
|
|
133
|
+
2024 : v1
|
|
134
|
+
2025 : v2 : v2.1
|
|
135
|
+
2026 : v3
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### User journey
|
|
139
|
+
|
|
140
|
+
```mermaid
|
|
141
|
+
journey
|
|
142
|
+
title My day
|
|
143
|
+
section Morning
|
|
144
|
+
Wake: 3: Me
|
|
145
|
+
Commute: 2: Me
|
|
146
|
+
section Work
|
|
147
|
+
Code: 5: Me
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Git graph
|
|
151
|
+
|
|
152
|
+
```mermaid
|
|
153
|
+
gitGraph
|
|
154
|
+
commit
|
|
155
|
+
branch dev
|
|
156
|
+
commit
|
|
157
|
+
checkout main
|
|
158
|
+
merge dev
|
|
159
|
+
commit
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Features
|
|
163
|
+
|
|
164
|
+
- ๐จ **Beautified themes** โ `colorful` (palette + shadows) and `sketch` (Excalidraw hand-drawn), plus native themes and `auto`.
|
|
165
|
+
- ๐งฐ **Toolbox or diagram-only** โ show the built-in toolbar, or just the chart with `toolbar={false}`.
|
|
166
|
+
- ๐ **In-diagram search** โ highlight + pan to matches (`/` or `Ctrl/Cmd+F`).
|
|
167
|
+
- ๐๏ธ **Pan & zoom** โ fit, actual size, keyboard `+ - 0 1 w` (via `svg-pan-zoom`).
|
|
168
|
+
- โถ **Fullscreen modal** โ open the diagram in a viewport-filling, RWD-friendly popup (`f` / `Esc`); body scroll locked, auto re-fit.
|
|
169
|
+
- โฆ **Background toggle** โ cycle the canvas between transparent / solid / dot-grid (`b`), handy for reading and exports.
|
|
170
|
+
- ๐ค **Export** โ SVG and high-res PNG/JPEG/WebP (1ร/2ร/4ร, optional transparent background).
|
|
171
|
+
- ๐ชถ **Lightweight & decoupled** โ `mermaid`, `svg-pan-zoom`, `react` are **optional peer deps**, never bundled. No Tailwind, no app coupling.
|
|
172
|
+
- ๐ **Load external mermaid** โ inject an instance, dynamic-import the peer, or pull from a CDN. Your bundle doesn't carry mermaid.
|
|
173
|
+
- ๐งฉ **TypeScript** โ full `.d.ts`. **SSR-safe** import (no top-level DOM access), ships a `'use client'` boundary for Next.js.
|
|
174
|
+
|
|
175
|
+
## Install
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
npm i react-super-mermaid
|
|
179
|
+
# mermaid + svg-pan-zoom are OPTIONAL peers โ install them to bundle/import locally:
|
|
180
|
+
npm i mermaid svg-pan-zoom
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
For **no-build / `<script type="module">`** pages you can load mermaid entirely from a CDN (see below) and install nothing. In a **bundler** (Vite / webpack / Next.js), install `mermaid` as a peer **or** inject an instance.
|
|
184
|
+
|
|
185
|
+
## Quick start
|
|
186
|
+
|
|
187
|
+
### With toolbox
|
|
188
|
+
|
|
189
|
+
This input:
|
|
190
|
+
|
|
191
|
+
```mermaid
|
|
192
|
+
flowchart LR
|
|
193
|
+
U[User] --> B{Logged in?}
|
|
194
|
+
B -- yes --> C[Lobby]
|
|
195
|
+
B -- no --> D[Login]
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
```tsx
|
|
199
|
+
import { MermaidViewer } from 'react-super-mermaid';
|
|
200
|
+
|
|
201
|
+
export default function App() {
|
|
202
|
+
return (
|
|
203
|
+
<div style={{ height: 480 }}>
|
|
204
|
+
<MermaidViewer code={flow} toolbar theme="colorful" />
|
|
205
|
+
</div>
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Diagram only (no toolbox)
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
import { MermaidDiagram } from 'react-super-mermaid';
|
|
214
|
+
// equivalent to <MermaidViewer toolbar={false} ... />
|
|
215
|
+
|
|
216
|
+
<MermaidDiagram code={code} />;
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
> Give the viewer a sized parent (it fills `height: 100%`).
|
|
220
|
+
|
|
221
|
+
## Load external mermaid
|
|
222
|
+
|
|
223
|
+
```tsx
|
|
224
|
+
// (a) inject an instance you already imported
|
|
225
|
+
import mermaid from 'mermaid';
|
|
226
|
+
<MermaidViewer code={code} mermaid={{ instance: mermaid }} />;
|
|
227
|
+
|
|
228
|
+
// (b) default: dynamic import('mermaid') from your installed peer
|
|
229
|
+
<MermaidViewer code={code} />;
|
|
230
|
+
|
|
231
|
+
// (c) CDN โ install nothing (best for no-build pages)
|
|
232
|
+
<MermaidViewer
|
|
233
|
+
code={code}
|
|
234
|
+
mermaid={{ cdnUrl: 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs' }}
|
|
235
|
+
/>;
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Resolution order is **injected โ peer import โ CDN**, memoized so mermaid loads once per page.
|
|
239
|
+
|
|
240
|
+
## `MermaidViewer` props
|
|
241
|
+
|
|
242
|
+
| Prop | Type | Default | Notes |
|
|
243
|
+
| --- | --- | --- | --- |
|
|
244
|
+
| `code` | `string` | โ | **required** mermaid source |
|
|
245
|
+
| `theme` | `'colorful' \| 'sketch' \| 'auto' \| 'default' \| 'dark' \| 'neutral' \| 'forest'` | `'colorful'` | toolbar can change it live |
|
|
246
|
+
| `dark` | `boolean` | auto (`prefers-color-scheme`) | |
|
|
247
|
+
| `toolbar` | `boolean` | `true` | `false` โ diagram only |
|
|
248
|
+
| `panZoom` | `boolean` | `true` | |
|
|
249
|
+
| `search` | `boolean` | `true` | toolbar search |
|
|
250
|
+
| `exportable` | `boolean` | `true` | toolbar SVG/PNG export |
|
|
251
|
+
| `background` | `boolean` | `true` | toolbar background toggle (transparent / solid / grid) |
|
|
252
|
+
| `backgroundMode` | `'transparent' \| 'solid' \| 'grid'` | `'transparent'` | initial / controlled canvas background |
|
|
253
|
+
| `fullscreen` | `boolean` | `true` | toolbar fullscreen button โ opens a viewport-filling modal (RWD) |
|
|
254
|
+
| `onFullscreenChange` | `(fullscreen: boolean) => void` | โ | fired on enter / exit fullscreen |
|
|
255
|
+
| `keyboard` | `boolean` | `true` | shortcuts when the viewer is focused |
|
|
256
|
+
| `seed` | `number` | `42` | sketch wobble seed |
|
|
257
|
+
| `fontUrl` | `string` | jsDelivr Virgil | sketch handwriting font |
|
|
258
|
+
| `mermaid` | `{ instance?, cdnUrl? }` | โ | how to obtain mermaid |
|
|
259
|
+
| `svgPanZoom` | `{ instance?, cdnUrl? }` | โ | how to obtain svg-pan-zoom |
|
|
260
|
+
| `mermaidConfig` | `object` | โ | passthrough to `mermaid.initialize` |
|
|
261
|
+
| `injectStyles` | `boolean` | `true` | inject the package's CSS once |
|
|
262
|
+
| `className` / `style` | โ | โ | on the root element |
|
|
263
|
+
| `onRender` | `(svg) => void` | โ | after each successful render |
|
|
264
|
+
| `onError` | `(err) => void` | โ | render/export errors |
|
|
265
|
+
|
|
266
|
+
### Imperative control (ref)
|
|
267
|
+
|
|
268
|
+
Useful in diagram-only mode to build your own buttons:
|
|
269
|
+
|
|
270
|
+
```tsx
|
|
271
|
+
import { useRef } from 'react';
|
|
272
|
+
import { MermaidDiagram, type MermaidViewerHandle } from 'react-super-mermaid';
|
|
273
|
+
|
|
274
|
+
const ref = useRef<MermaidViewerHandle>(null);
|
|
275
|
+
<MermaidDiagram ref={ref} code={code} />;
|
|
276
|
+
|
|
277
|
+
ref.current?.zoomIn();
|
|
278
|
+
ref.current?.fit();
|
|
279
|
+
ref.current?.search('Bob');
|
|
280
|
+
await ref.current?.downloadPng('diagram.png', { scale: 4 });
|
|
281
|
+
const svgString = ref.current?.exportSvg();
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Handle: `zoomIn / zoomOut / fit / reset / actualSize / getZoomPercent / search / next / prev / clearSearch / exportSvg / exportPng / downloadSvg / downloadPng / getSvg / enterFullscreen / exitFullscreen / toggleFullscreen / isFullscreen / setBackground / cycleBackground / getBackground`.
|
|
285
|
+
|
|
286
|
+
## Themes & the sketch font
|
|
287
|
+
|
|
288
|
+
The `sketch` theme uses Excalidraw's **Virgil** handwriting font, fetched at runtime from this package's jsDelivr asset (so it isn't bundled into your JS; the `colorful` default needs no font at all). Override it:
|
|
289
|
+
|
|
290
|
+
```tsx
|
|
291
|
+
<MermaidViewer code={code} theme="sketch" fontUrl="/fonts/Virgil.woff2" />
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
If the font can't load, sketch falls back to `KaiTi / Comic Sans MS / cursive` โ rendering never breaks.
|
|
295
|
+
|
|
296
|
+
## Styling
|
|
297
|
+
|
|
298
|
+
Styles are injected automatically (`injectStyles` default `true`) โ no CSS import needed. Override via CSS variables on `.rsm-root`:
|
|
299
|
+
|
|
300
|
+
```css
|
|
301
|
+
.rsm-root {
|
|
302
|
+
--rsm-accent: #7c3aed;
|
|
303
|
+
--rsm-border: #e2e8f0;
|
|
304
|
+
--rsm-radius: 12px;
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Keyboard shortcuts
|
|
309
|
+
|
|
310
|
+
Focus the viewer, then: `/` or `Ctrl/Cmd+F` search ยท `+`/`-` zoom ยท `0` fit ยท `1` actual size ยท `w` fit width ยท `f` toggle fullscreen ยท `b` cycle background ยท `Esc` close search / exit fullscreen.
|
|
311
|
+
|
|
312
|
+
## Next.js / SSR
|
|
313
|
+
|
|
314
|
+
The package imports cleanly on the server (no top-level DOM access) and ships a `'use client'` boundary, so you can use it directly in the App Router. mermaid and svg-pan-zoom are only touched at runtime via dynamic import.
|
|
315
|
+
|
|
316
|
+
## Low-level (framework-agnostic) API
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
import { renderDiagram, loadMermaid, colorizeDiagram, sketchifyDiagram } from 'react-super-mermaid';
|
|
320
|
+
|
|
321
|
+
await renderDiagram({ code, container: '#out', theme: 'colorful' });
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## Demo
|
|
325
|
+
|
|
326
|
+
A runnable demo (Vite + React, installs the package from npm) lives in [`example/`](./example):
|
|
327
|
+
|
|
328
|
+
```bash
|
|
329
|
+
cd example
|
|
330
|
+
npm install
|
|
331
|
+
npm run dev
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
It showcases the toolbox, diagram-only mode with custom ref-driven buttons, every theme, and multiple diagram types.
|
|
335
|
+
|
|
336
|
+
## License
|
|
337
|
+
|
|
338
|
+
MIT ยฉ [markku636](https://github.com/markku636)
|