|
| 1 | +--- |
| 2 | +title: Mermaid diagrams |
| 3 | +status: published |
| 4 | +author: guillaumerichard |
| 5 | +date: 4/28/2026 |
| 6 | +order: 8 |
| 7 | +keywords: |
| 8 | + - mermaid |
| 9 | + - diagram |
| 10 | + - flowchart |
| 11 | + - sequence |
| 12 | + - state |
| 13 | + - mindmap |
| 14 | + - integration |
| 15 | + - parse |
| 16 | + - render |
| 17 | + - visualization |
| 18 | +--- |
| 19 | + |
| 20 | +You can turn [Mermaid](https://mermaid.js.org/) diagram syntax into native, editable tldraw shapes with the **`@tldraw/mermaid`** package. Instead of rendering a static SVG, it parses Mermaid text and creates real geo shapes, arrows, frames, and groups on the canvas—so users can move, resize, restyle, and connect them like any other shape. |
| 21 | + |
| 22 | +## Quick start |
| 23 | + |
| 24 | +Install the package alongside `tldraw`: |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install @tldraw/mermaid |
| 28 | +``` |
| 29 | + |
| 30 | +Then call [`createMermaidDiagram`](?) with an [`Editor`](?) and a Mermaid source string: |
| 31 | + |
| 32 | +```tsx |
| 33 | +import { createMermaidDiagram } from '@tldraw/mermaid' |
| 34 | + |
| 35 | +await createMermaidDiagram( |
| 36 | + editor, |
| 37 | + ` |
| 38 | + flowchart TD |
| 39 | + A[Start] --> B{Decision} |
| 40 | + B -->|Yes| C[Do something] |
| 41 | + B -->|No| D[Do something else] |
| 42 | +` |
| 43 | +) |
| 44 | +``` |
| 45 | + |
| 46 | +By default the diagram is centered on the current viewport. Pass a `blueprintRender.position` to place it at a specific page point, and set `centerOnPosition: false` to anchor its top-left corner instead of its center. |
| 47 | + |
| 48 | +## Supported diagram types |
| 49 | + |
| 50 | +| Diagram type | Mermaid keyword | What you get | |
| 51 | +| ---------------- | -------------------- | ------------------------------------------------------------------- | |
| 52 | +| Flowchart | `flowchart`, `graph` | Geo shapes, arrows, subgraph frames | |
| 53 | +| Sequence diagram | `sequenceDiagram` | Actor shapes, lifelines, signal arrows, fragment frames | |
| 54 | +| State diagram | `stateDiagram-v2` | State shapes, transitions, compound state frames, fork/join, choice | |
| 55 | +| Mindmap | `mindmap` | Colored geo shapes, parent-child edges, tree hierarchy | |
| 56 | + |
| 57 | +For unsupported types (pie, gantt, class, ER, etc.) pass an `onUnsupportedDiagram` callback—for example, to fall back to importing Mermaid's rendered SVG: |
| 58 | + |
| 59 | +```tsx |
| 60 | +await createMermaidDiagram(editor, text, { |
| 61 | + onUnsupportedDiagram(svgString) { |
| 62 | + editor.putExternalContent({ type: 'svg-text', text: svgString }) |
| 63 | + }, |
| 64 | +}) |
| 65 | +``` |
| 66 | + |
| 67 | +Without a callback, `createMermaidDiagram` throws a [`MermaidDiagramError`](?) for unsupported types and parse failures. |
| 68 | + |
| 69 | +## Handling pasted Mermaid text |
| 70 | + |
| 71 | +The most common integration is converting Mermaid text when users paste it onto the canvas. Register an external content handler that sniffs for a Mermaid keyword and dynamically imports the package: |
| 72 | + |
| 73 | +```tsx |
| 74 | +import { useEffect } from 'react' |
| 75 | +import { defaultHandleExternalTextContent, useEditor } from 'tldraw' |
| 76 | + |
| 77 | +const MERMAID_KEYWORD = |
| 78 | + /^\s*(flowchart|graph|sequenceDiagram|stateDiagram|classDiagram|erDiagram|gantt|pie|gitGraph|mindmap)/ |
| 79 | + |
| 80 | +export function MermaidPasteHandler() { |
| 81 | + const editor = useEditor() |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + editor.registerExternalContentHandler('text', async (content) => { |
| 85 | + if (!MERMAID_KEYWORD.test(content.text)) { |
| 86 | + await defaultHandleExternalTextContent(editor, content) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + const { createMermaidDiagram } = await import('@tldraw/mermaid') |
| 92 | + await createMermaidDiagram(editor, content.text, { |
| 93 | + async onUnsupportedDiagram(svgString) { |
| 94 | + await editor.putExternalContent({ type: 'svg-text', text: svgString }) |
| 95 | + }, |
| 96 | + }) |
| 97 | + } catch { |
| 98 | + await defaultHandleExternalTextContent(editor, content) |
| 99 | + } |
| 100 | + }) |
| 101 | + }, [editor]) |
| 102 | + |
| 103 | + return null |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +Drop `<MermaidPasteHandler />` inside your [`Tldraw`](?) component and pasting Mermaid text will render it as shapes. |
| 108 | + |
| 109 | +<Callout type="info"> |
| 110 | + The `mermaid` dependency is ~2 MB. The pattern above pre-screens with a lightweight regex and |
| 111 | + lazy-loads `@tldraw/mermaid` only when the text matches, so users who never paste Mermaid don't |
| 112 | + pay the cost. |
| 113 | +</Callout> |
| 114 | + |
| 115 | +## Customizing node shapes |
| 116 | + |
| 117 | +By default, blueprint nodes are materialized as tldraw geo shapes. If you want to render them as your own custom shape type instead—for example, sticky notes for mindmap leaves, or a bespoke "actor" shape for sequence diagrams—pass `mapNodeToRenderSpec` on `blueprintRender`: |
| 118 | + |
| 119 | +```tsx |
| 120 | +await createMermaidDiagram(editor, text, { |
| 121 | + blueprintRender: { |
| 122 | + mapNodeToRenderSpec(input) { |
| 123 | + if (input.diagramKind === 'mindmap') { |
| 124 | + return { variant: 'shape', type: 'note', props: {} } |
| 125 | + } |
| 126 | + // Return undefined to keep the package default for this node. |
| 127 | + return undefined |
| 128 | + }, |
| 129 | + }, |
| 130 | +}) |
| 131 | +``` |
| 132 | + |
| 133 | +The callback receives `diagramKind`, `nodeId`, `kind`, and the full [`MermaidBlueprintNode`](?), and returns a [`MermaidBlueprintNodeRenderSpec`](?)—either a `geo` variant or a custom `shape` type that's registered on the editor. See [`createMermaidDiagram`](?) and [`renderBlueprint`](?) in the reference for the full API. |
| 134 | + |
| 135 | +## Examples |
| 136 | + |
| 137 | +- [Hundreds of mermaid diagrams](/examples/use-cases/hundred-mermaids) — A runnable demo rendering many diagram types at once |
| 138 | +- [Customizing mermaid diagrams](/examples/use-cases/custom-shape-mermaids) - Converting mermaid diagram nodes into custom shapes |
| 139 | + |
| 140 | +## Related |
| 141 | + |
| 142 | +- [`createMermaidDiagram`](?), [`renderBlueprint`](?), [`MermaidDiagramError`](?) — Entry points and errors |
| 143 | +- [`DiagramMermaidBlueprint`](?), [`MermaidBlueprintNode`](?), [`MermaidBlueprintNodeRenderSpec`](?) — The data model you can customize |
| 144 | +- [Shapes](/docs/shapes) — Define a custom shape you can map Mermaid nodes to |
| 145 | +- [External content handlers](/sdk-features/external-content) — More on `registerExternalContentHandler` for the paste integration |
0 commit comments