Unsaved
Ready
Loading Monaco Editor...
Preview
Styling
State Diagram
All diagram types

Mermaid State Diagram Syntax & Examples

A state diagram lists the modes a thing can be in and the events that move it from one mode to another.

Examples

Copy an example, or open it in the live editor with this diagram already loaded.

Order lifecycle

Unpaid, paid, and refunded, plus a decline that can be retried. [*] marks where an order enters and where it is finished.

stateDiagram-v2
    [*] --> Unpaid
    Unpaid --> Paid : charge succeeds
    Unpaid --> Failed : charge declined
    Failed --> Unpaid : retry
    Paid --> Refunded : refund issued
    Paid --> [*]
    Refunded --> [*]

Connection status

A small machine for a realtime client. The labels are the events, not the next state.

stateDiagram-v2
    [*] --> Disconnected
    Disconnected --> Connecting : connect
    Connecting --> Connected : socket open
    Connecting --> Disconnected : timeout
    Connected --> Reconnecting : socket closed
    Reconnecting --> Connected : socket open
    Reconnecting --> Disconnected : give up

Nested review state

A composite state hides the steps inside review. The note sits beside the parent state.

stateDiagram-v2
    [*] --> Draft
    Draft --> InReview : submit
    state InReview {
        [*] --> Reading
        Reading --> Deciding
        Deciding --> [*]
    }
    InReview --> Draft : request changes
    InReview --> Published : approve
    note right of InReview : Reviewers work inside this state
    Published --> [*]

What is a State Diagram?

A state diagram, also called a state machine diagram, is a map of modes. Each node is a state. Each arrow is a transition, usually labeled with the event that causes it. [*] is the start, and an arrow back to [*] is the end. stateDiagram-v2 is the syntax to use: it supports notes, choices, and composite states. Reach for a state diagram when an order, a connection, or a screen has a small set of legal modes and illegal combinations would be a bug.

When to use it

Use a state diagram for order lifecycles, connection status, and UI modes. Use a flowchart when you are describing a procedure with branches, not the mode of an object. Use a sequence diagram when the interesting part is the messages, not the resulting state.

Edit this state diagram in the live editor, read the Mermaid cheat sheet, or compare it with Flowchart, Sequence Diagram and Class Diagram.

Common Use Cases

  • •UI state management - Model component states and transitions
  • •Order processing - Track order lifecycle (pending → shipped → delivered)
  • •Authentication - Visualize login states and session handling
  • •Game development - Define character or game states
  • •Embedded systems - Design hardware state machines

Syntax Quick Reference

ElementSyntaxDescription
Initial state[*]Starting point
StateStateNameDefine a state
TransitionState1 --> State2State change
Labeled transition--> State : EventTransition with trigger
Composite statestate Name { }Nested states
Fork/Join<<fork>>Parallel states

Syntax tips

  • •Start the file with stateDiagram-v2. The v2 syntax is what supports notes and composite states reliably.
  • •[*] --> Draft is the initial transition. Draft --> [*] is a final transition. You can have more than one final arrow.
  • •Put the event after a colon: Unpaid --> Paid : charge succeeds. The event is the label, not a new state.
  • •state Name { } creates a composite state. Give it its own [*] if the nested machine has a start.
  • •note right of Name : text adds a comment without creating a transition.

Common mistakes

Starting with stateDiagram instead of stateDiagram-v2 and then using notes or composite states.

Use stateDiagram-v2 for every new diagram on this page. It is the syntax the nested example depends on.

Naming the arrow with the destination, so the label repeats the next state.

The label should be the trigger: pay, approve, timeout. The node it points at is already the next state.

Drawing a process checklist as a state machine.

If the boxes are steps a person performs, use a flowchart. States are modes the system is in between events.

Export Options

State machines are useful in design docs and README files. SVG keeps the transition labels crisp. Free PNG exports include a watermark.

Got Mermaid code from ChatGPT? Paste it here to render and export instantly.

Get the Free Mermaid Syntax Cheat Sheet

Free Mermaid Syntax Cheat Sheet

All 21 diagram types in one PDF reference.

Frequently Asked Questions

Ready to create your own state diagram?

Open the editor with this example pre-loaded and start customizing.

State Diagram templates

Related Diagram Types

All diagram types