react-super-mermaid 0.1.0 โ†’ 0.3.1

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 CHANGED
@@ -1,165 +1,338 @@
1
- # react-super-mermaid
2
-
3
- > 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.
4
-
5
- ```tsx
6
- import { MermaidViewer } from 'react-super-mermaid';
7
-
8
- <MermaidViewer code={`flowchart LR\n A[Start] --> B{OK?} --> C[Done]`} toolbar />;
9
- ```
10
-
11
- ## Features
12
-
13
- - ๐ŸŽจ **Beautified themes** โ€” `colorful` (modern palette + soft shadows) and `sketch` (Excalidraw hand-drawn), plus mermaid's native `default / dark / neutral / forest` and `auto`.
14
- - ๐Ÿงฐ **Toolbox or diagram-only** โ€” show a built-in toolbar (theme / zoom / search / export), or just the chart with `toolbar={false}`.
15
- - ๐Ÿ” **In-diagram search** โ€” highlight + pan to matches (`/` or `Ctrl/Cmd+F`).
16
- - ๐Ÿ–๏ธ **Pan & zoom** โ€” fit, actual size, keyboard `+ - 0 1 w` (via `svg-pan-zoom`).
17
- - ๐Ÿ“ค **Export** โ€” SVG and high-res PNG/JPEG/WebP (1ร—/2ร—/4ร—, optional transparent background).
18
- - ๐Ÿชถ **Lightweight & decoupled** โ€” `mermaid`, `svg-pan-zoom`, `react` are **optional peer deps**, never bundled. No Tailwind, no app coupling.
19
- - ๐ŸŒ **Load external mermaid** โ€” inject an instance, dynamic-import the peer, or pull from a CDN. Your bundle doesn't have to carry mermaid.
20
- - ๐Ÿงฉ **TypeScript** โ€” full `.d.ts`. **SSR-safe** import (no top-level DOM access).
21
-
22
- ## Install
23
-
24
- ```bash
25
- npm i react-super-mermaid
26
- # mermaid is an OPTIONAL peer โ€” install it if you want it bundled/imported locally:
27
- npm i mermaid svg-pan-zoom
28
- ```
29
-
30
- `mermaid` and `svg-pan-zoom` are optional peers. 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.
31
-
32
- ## Quick start
33
-
34
- ### With toolbox
35
-
36
- ```tsx
37
- import { MermaidViewer } from 'react-super-mermaid';
38
-
39
- export default function App() {
40
- return (
41
- <div style={{ height: 480 }}>
42
- <MermaidViewer code={`sequenceDiagram\n Alice->>Bob: Hi\n Bob-->>Alice: Hello`} toolbar theme="colorful" />
43
- </div>
44
- );
45
- }
46
- ```
47
-
48
- ### Diagram only (no toolbox)
49
-
50
- ```tsx
51
- import { MermaidDiagram } from 'react-super-mermaid';
52
- // equivalent to <MermaidViewer toolbar={false} ... />
53
-
54
- <MermaidDiagram code={code} />;
55
- ```
56
-
57
- > Give the viewer a sized parent (it fills `height: 100%`).
58
-
59
- ## Load external mermaid
60
-
61
- The package never hard-codes a mermaid import you can't control. Pick whichever fits:
62
-
63
- ```tsx
64
- // (a) inject an instance you already imported
65
- import mermaid from 'mermaid';
66
- <MermaidViewer code={code} mermaid={{ instance: mermaid }} />;
67
-
68
- // (b) default: dynamic import('mermaid') from your installed peer
69
- <MermaidViewer code={code} />;
70
-
71
- // (c) CDN โ€” install nothing
72
- <MermaidViewer
73
- code={code}
74
- mermaid={{ cdnUrl: 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs' }}
75
- />;
76
- ```
77
-
78
- Resolution order is **injected โ†’ peer import โ†’ CDN**, memoized so mermaid loads once per page.
79
-
80
- ## `MermaidViewer` props
81
-
82
- | Prop | Type | Default | Notes |
83
- | --- | --- | --- | --- |
84
- | `code` | `string` | โ€” | **required** mermaid source |
85
- | `theme` | `'colorful' \| 'sketch' \| 'auto' \| 'default' \| 'dark' \| 'neutral' \| 'forest'` | `'colorful'` | toolbar can change it live |
86
- | `dark` | `boolean` | auto (`prefers-color-scheme`) | |
87
- | `toolbar` | `boolean` | `true` | `false` โ†’ diagram only |
88
- | `panZoom` | `boolean` | `true` | |
89
- | `search` | `boolean` | `true` | toolbar search |
90
- | `exportable` | `boolean` | `true` | toolbar SVG/PNG export |
91
- | `keyboard` | `boolean` | `true` | shortcuts when the viewer is focused |
92
- | `seed` | `number` | `42` | sketch wobble seed |
93
- | `fontUrl` | `string` | jsDelivr Virgil | sketch handwriting font (see below) |
94
- | `mermaid` | `{ instance? , cdnUrl? }` | โ€” | how to obtain mermaid |
95
- | `svgPanZoom` | `{ instance?, cdnUrl? }` | โ€” | how to obtain svg-pan-zoom |
96
- | `mermaidConfig` | `object` | โ€” | passthrough to `mermaid.initialize` |
97
- | `injectStyles` | `boolean` | `true` | inject the package's CSS once |
98
- | `className` / `style` | โ€” | โ€” | on the root element |
99
- | `onRender` | `(svg) => void` | โ€” | after each successful render |
100
- | `onError` | `(err) => void` | โ€” | render/export errors |
101
-
102
- ### Imperative control (ref)
103
-
104
- Useful in diagram-only mode to build your own buttons:
105
-
106
- ```tsx
107
- import { useRef } from 'react';
108
- import { MermaidDiagram, type MermaidViewerHandle } from 'react-super-mermaid';
109
-
110
- const ref = useRef<MermaidViewerHandle>(null);
111
- <MermaidDiagram ref={ref} code={code} />;
112
-
113
- ref.current?.zoomIn();
114
- ref.current?.fit();
115
- ref.current?.search('Bob');
116
- await ref.current?.downloadPng('diagram.png', { scale: 4 });
117
- const svgString = ref.current?.exportSvg();
118
- ```
119
-
120
- Handle: `zoomIn / zoomOut / fit / reset / actualSize / getZoomPercent / search / next / prev / clearSearch / exportSvg / exportPng / downloadSvg / downloadPng / getSvg`.
121
-
122
- ## Themes & the sketch font
123
-
124
- The `sketch` theme uses Excalidraw's **Virgil** handwriting font. By default it is fetched at runtime from this package's own jsDelivr asset, so it isn't bundled into your JS (the `colorful` default needs no font at all). Override it:
125
-
126
- ```tsx
127
- // host your own copy
128
- <MermaidViewer code={code} theme="sketch" fontUrl="/fonts/Virgil.woff2" />
129
- ```
130
-
131
- If the font can't load, sketch falls back to `KaiTi / Comic Sans MS / cursive` โ€” rendering never breaks.
132
-
133
- ## Styling
134
-
135
- Styles are injected automatically (`injectStyles` default `true`) โ€” no CSS import needed. Override the look via CSS variables on `.rsm-root`:
136
-
137
- ```css
138
- .rsm-root {
139
- --rsm-accent: #7c3aed;
140
- --rsm-border: #e2e8f0;
141
- --rsm-radius: 12px;
142
- }
143
- ```
144
-
145
- ## Keyboard shortcuts
146
-
147
- Focus the viewer, then: `/` or `Ctrl/Cmd+F` search ยท `+`/`-` zoom ยท `0` fit ยท `1` actual size ยท `w` fit width ยท `Esc` close search.
148
-
149
- ## Next.js / SSR
150
-
151
- 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.
152
-
153
- ## Low-level (framework-agnostic) API
154
-
155
- For non-React or advanced use, the core functions are exported too:
156
-
157
- ```ts
158
- import { renderDiagram, loadMermaid, colorizeDiagram, sketchifyDiagram } from 'react-super-mermaid';
159
-
160
- await renderDiagram({ code, container: '#out', theme: 'colorful' });
161
- ```
162
-
163
- ## License
164
-
165
- MIT
1
+ # react-super-mermaid
2
+
3
+ [![npm version](https://img.shields.io/npm/v/react-super-mermaid.svg)](https://www.npmjs.com/package/react-super-mermaid)
4
+ [![license](https://img.shields.io/npm/l/react-super-mermaid.svg)](./LICENSE)
5
+ [![types](https://img.shields.io/npm/types/react-super-mermaid.svg)](#)
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)