mermaid-editor
A React component for editing Mermaid diagrams. You click and drag on the drawing, and the value stays plain Mermaid text. An edit changes only the lines it has to.
Mermaid diagrams are text, so every change means editing the syntax by hand. A drawing tool would lose the text. I wanted to edit on the drawing and keep the text as the source, with only the changed lines touched.
What it does
Edit on the drawing
Flowcharts are edited where you see them. Here is one edit, step by step.
- 1
Click a node
A panel opens with its label, shape and group. You can also connect it to another node or delete it.
- 2
Change it
Rename it to Checkout API. The drawing updates, and only that line of text changes.
- 3
Arrows too
Click an arrow to change its label, line and ends, or to reverse it. Drag from one node to another to add one.
Sequence diagrams
Add, rename and reorder participants. Add messages and notes, and move them up and down. Wrap steps in loop, alt, opt, par, critical or break blocks.
Only the lines it has to
Comments, styles and lines the editor does not understand stay exactly as written. Here the label changes and nothing else does.
%% checkout flow
flowchart LR
shop[Shop] --> api{{Orders API}}
api --> db[(Orders DB)]
classDef hot fill:#f96
class api hot%% checkout flow
flowchart LR
shop[Shop] --> api{{Checkout API}}
api --> db[(Orders DB)]
classDef hot fill:#f96
class api hotEvery other kind as text
State diagrams, class diagrams and the rest are edited as text, with a live drawing next to it.
Use the text operations on their own
The functions behind the editor work without React, in a script or on a server. Each takes Mermaid text and returns Mermaid text.
import { flowchart } from '@dinesh-gamage/mermaid-editor/core'
const next = flowchart.setNodeText(text, 'api', 'Checkout API')- 06
Themeable
Every color and size is a CSS custom property on
.mmde. Set them on the editor or on any parent. - 07
Mermaid places the nodes
Mermaid text holds no positions, so nodes cannot be placed by hand. Mermaid arranges the diagram.
Get started
- 1
Install
Mermaid, React and React DOM are peers, so your app keeps one copy of each.
$npm install @dinesh-gamage/mermaid-editor mermaid - 2
Add the editor
Pass Mermaid text in, and get the new text back after every edit.
import { useState } from 'react' import { MermaidEditor } from '@dinesh-gamage/mermaid-editor' function Example() { const [text, setText] = useState('flowchart LR\n web[Web site] --> api{{API}}') return <MermaidEditor value={text} onChange={setText} /> }