# Mermaid State Diagram Cheat Sheet

States, the moves between them, and the branching in between.

## States and transitions

A transition is an arrow. A colon after the arrow is the event that causes it.

```mermaid
stateDiagram-v2
  [*] --> Idle
  Idle --> Running : start
  Running --> Idle : stop
```

![A state machine with a start point flowing to Idle, then to Running when started, and back to Idle when stopped.](/diagrams/state/transitions.svg)

## Start and end

The same [*] marker does both: the arrow into it is the end, the arrow out of it is the start.

```mermaid
stateDiagram-v2
  [*] --> Draft
  Draft --> Published
  Published --> [*]
```

![A start point, then a box labelled Draft, then a box labelled Published, then an end point.](/diagrams/state/start-end.svg)

## A choice point

A diamond that asks a question. Each outgoing arrow is one answer.

```mermaid
stateDiagram-v2
  [*] --> Check
  Check --> Decide <<choice>>
  Decide --> Accepted : if valid
  Decide --> Rejected : if invalid
```

![A start point leading to a box labelled Check, then to a diamond labelled Decide, which splits into Accepted and Rejected.](/diagrams/state/choice.svg)

## A state inside a state

Curly braces open a composite state. Its internal states live inside.

```mermaid
stateDiagram-v2
  [*] --> Active
  state Active {
    [*] --> NumLockOff
    NumLockOff --> NumLockOn : press
    NumLockOn --> NumLockOff : press
  }
  Active --> [*]
```

![A state box named Active containing two smaller states, NumLockOff and NumLockOn, connected by a labelled arrow.](/diagrams/state/composite.svg)

## Fork and join

A fork splits the flow, a join waits for every branch to arrive.

```mermaid
stateDiagram-v2
  [*] --> Split <<fork>>
  Split --> A
  Split --> B
  A --> Merge <<join>>
  B --> Merge
  Merge --> [*]
```

![A start point leading to a horizontal bar that splits into two branches, which both lead to a second horizontal bar and then to an end point.](/diagrams/state/fork-join.svg)

## Notes

A note attaches to one state, or floats over two.

```mermaid
stateDiagram-v2
  [*] --> Draft
  Draft --> Review
  note right of Draft
    Still editable
  end note
```

![A state machine with a note box attached to the right of the Draft state.](/diagrams/state/notes.svg)

## Direction

The same keyword as flowcharts. Default is top to bottom.

```mermaid
stateDiagram-v2
  direction LR
  [*] --> Idle
  Idle --> Running : start
```

![A start point, an Idle box and a Running box laid out left to right.](/diagrams/state/direction.svg)

## Draw it in Mernyx.

Open a .mmd file, move a node on the canvas, and the source comes with it. Native on macOS, no browser involved.
