react-super-mermaid 0.1.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/LICENSE +21 -0
- package/README.md +165 -0
- package/dist/Virgil.woff2 +0 -0
- package/dist/index.cjs +1673 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +301 -0
- package/dist/index.d.ts +301 -0
- package/dist/index.js +1651 -0
- package/dist/index.js.map +1 -0
- package/package.json +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 markku636
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
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
|
|
Binary file
|