Ditch the Drag-and-Drop: A Hands-On Guide to Mermaid JS

ArjanCodes////3 min read

Overview

Software diagrams often become a maintenance nightmare. Traditional drag-and-drop tools are frustrating because arrows break when you move boxes, and version controlling an image file is nearly impossible. solves this by treating diagrams as code. By using a simple Markdown-like syntax, you can generate professional diagrams that stay in sync with your documentation. This approach ensures your visualizations are as easy to update as your source code, making them a first-class citizen in your development workflow.

Prerequisites

To follow this tutorial, you should have a basic understanding of syntax and general software design concepts like flow logic and object relationships. You will need a text editor—ideally —or access to a web browser to use the online editor.

Key Libraries & Tools

  • : A JavaScript-based charting and diagramming tool that renders text definitions into visualizations.
  • : The recommended IDE for local development.
  • Markdown Preview Mermaid Support: A vital extension that enables real-time rendering of Mermaid blocks within Markdown files.
  • Mermaid.live: The official web-based editor used for quick prototyping and exporting diagrams to PNG or SVG formats.

Code Walkthrough

1. Modeling Logic with Flowcharts

Flowcharts are perfect for mapping user interactions or complex conditional logic. You define the direction (Top-Bottom or Left-Right) and then link nodes using arrows.

flowchart TD
    A[Start] --> B{Existing User?}
    B -- No --> C[Enter Name]
    B -- Yes --> D[Send Magic Link]
    C --> D

In this snippet, TD sets the orientation. The square brackets [] create standard blocks, while curly braces {} produce diamond decision nodes. Labels placed between characters (like -- No -->) attach text directly to the transition lines.

2. Visualizing Interaction with Sequence Diagrams

When you need to show how different services—like a client, an provider, and a server—talk to each other over time, use a sequence diagram.

sequence_diagram
    autonumber
    participant Client
    participant Server
    Client->>Server: Request Resource
    activate Server
    Server-->>Client: Return Data
    deactivate Server

The autonumber keyword is a lifesaver for documentation, as it allows you to refer to specific steps in your written text. The activate and deactivate commands create visual focus on which component is currently processing a request.

3. Structural Mapping with Class Diagrams

Class diagrams help visualize the blueprint of your system. You can define members, methods, and visibility (using + for public, - for private, and # for protected).

classDiagram
    class PaymentProcessor {
        <<interface>>
        -String apiKey
        +process(Order order)
    }
    class Stripe {
        +process(Order order)
    }
    PaymentProcessor <|-- Stripe

Here, <<interface>> provides metadata about the class type, and <|-- represents inheritance. Mermaid also supports aggregation (o--) and composition (*--) to show how objects relate to one another.

Syntax Notes

Mermaid syntax is highly declarative. You don't tell the tool where to draw a line; you tell it what the relationship is. Notable patterns include:

  • Node IDs vs. Labels: Using S[Start] allows you to reference the node as S in your code while displaying "Start" in the diagram.
  • Styling Nodes: Different brackets change the shape (e.g., ([Text]) for rounded corners or [[Text]] for subroutines).
  • Directionality: Common codes include LR (Left to Right), RL (Right to Left), BT (Bottom to Top), and TD (Top Down).

Practical Examples

  • Onboarding Documentation: Use flowcharts to show new developers how data travels through your microservices.
  • API Documentation: Include sequence diagrams in your README files to show exactly how third-party developers should call your endpoints.
  • Database Design: Utilize Entity-Relationship (ER) diagrams to map out your schema before writing a single migration script.

Tips & Gotchas

  • Exporting Constraints: While is great for live previews, exporting to PDF can be tricky. Use Mermaid.live when you need high-quality image exports for slide decks or reports.
  • Layout Limitations: You lose granular control over node placement. If your diagram looks cluttered, try breaking it into smaller, more modular sub-diagrams rather than fighting the layout engine.
  • Version Control: Always keep your Mermaid code in your .md files. This allows teammates to see exactly what changed in a diagram during a code review by looking at the text diff.
Topic DensityMention share of the most discussed topics · 10 mentions across 6 distinct topics
40%· products
20%· products
10%· products
10%· products
10%· products
10%· products
End of Article
Source video
Ditch the Drag-and-Drop: A Hands-On Guide to Mermaid JS

Mermaid JS: Finally There's A Great UML & Diagram Drawing Tool

Watch

ArjanCodes // 28:35

On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!

What they talk about
AI and Agentic Coding News
Who and what they mention most
Python
33.3%5
Python
20.0%3
Python
20.0%3
Pydantic
13.3%2
3 min read0%
3 min read