Mermaid

A component used to render Mermaid.js diagrams inside MDX content. Supports flowchart, sequence, class, state, gantt, pie, and ER diagrams.

Mermaid lets you create diagrams and visualizations using plain text. DocuBook renders Mermaid diagrams natively inside MDX — just write a fenced code block with the mermaid language identifier.

For the full list of diagram types and syntax reference, see the official Mermaid documentation.

Diagram Types

Diagram TypeIdentifierMermaid Docs
Flowchartgraph TD / flowchart TDFlowchart
Sequence DiagramsequenceDiagramSequence Diagram
Class DiagramclassDiagramClass Diagram
State DiagramstateDiagram-v2State Diagram
Gantt ChartganttGantt
Pie ChartpiePie Chart
ER DiagramerDiagramEntity Relationship

Flowchart

graph TD
  A[Start] --> B{Is it working?}
  B -->|Yes| C[Great!]
  B -->|No| D[Debug]
  D --> B

Sequence Diagram

sequenceDiagram
  participant U as User
  participant S as Server
  participant D as Database
  U->>S: Request data
  S->>D: Query
  D-->>S: Results
  S-->>U: Response

Class Diagram

classDiagram
  class Animal {
    +String name
    +int age
    +makeSound() void
  }
  class Dog {
    +fetch() void
  }
  class Cat {
    +purr() void
  }
  Animal <|-- Dog
  Animal <|-- Cat

State Diagram

stateDiagram-v2
  [*] --> Idle
  Idle --> Processing: Start
  Processing --> Success: Complete
  Processing --> Error: Fail
  Error --> Idle: Retry
  Success --> [*]

Gantt Chart

gantt
  title Project Timeline
  dateFormat  YYYY-MM-DD
  section Planning
  Research          :done,  a1, 2026-01-01, 14d
  Design            :done,  a2, 2026-01-15, 10d
  section Development
  Frontend          :active, b1, 2026-01-25, 20d
  Backend           :        b2, 2026-01-25, 20d
  Testing           :        b3, 2026-02-14, 10d

Pie Chart

pie title Browser Usage
  "Chrome" : 65
  "Safari" : 18
  "Firefox" : 10
  "Edge" : 7

ER Diagram

erDiagram
  USER ||--o{ ORDER : places
  ORDER ||--|{ LINE_ITEM : contains
  PRODUCT ||--o{ LINE_ITEM : includes
  USER {
    string id PK
    string name
    string email
  }
  ORDER {
    int id PK
    string status
    date created_at
  }

Pan and Zoom

Every rendered diagram gets a GFM-style control cluster in the bottom-right corner, so large diagrams stay explorable without mouse-drag interaction:

  • Arrow buttons — pan the diagram up, down, left, or right in fixed steps
  • Zoom in / zoom out — scale the diagram between 0.4× and 4×
  • Reset — restore the original position and scale
  • Full screen — open the diagram in a lightbox overlay; close it with the same button or Escape

The diagram container is keyboard accessible: focus it with Tab, then press Enter to open the fullscreen view. Inside fullscreen, pan with arrow keys, zoom with + / -, and reset with 0.

Output Markdown

Primary syntax — fenced code block:

markdown
```mermaid
graph TD
  A[Start] --> B{Is it working?}
  B -->|Yes| C[Great!]
  B -->|No| D[Debug]
  D --> B
```

Notes

  • Diagrams are rendered client-side. During SSR, a <pre class="mermaid"> placeholder is rendered instead.
  • Pan, zoom, and fullscreen: A fullscreen button appears after the diagram renders. Click or press Enter to open the fullscreen lightbox. Inside fullscreen, use pan/zoom buttons, arrow keys, scroll-wheel zoom, or click-and-drag to explore the diagram. Press Escape to exit.
  • Theme synchronization: The component automatically detects dark/light theme changes and re-renders diagrams.
  • Lazy loading: Off-screen diagrams are only rendered when scrolled into view (200px margin).
  • Error fallback: If a diagram has invalid Mermaid syntax, the raw code is shown with an error message.
  • Per-diagram theme overrides can be set via %%{init: {"theme": "forest"}}%% directives inside the chart definition.
  • All standard Mermaid diagram types are supported: flowchart, sequenceDiagram, classDiagram, stateDiagram, gantt, pie, erDiagram, gitGraph, journey, etc.

Last updated Aug 13, 2026