Mermaid state diagrams
Show the states something can be in and what moves it from one to the next. This guide covers the state diagram syntax, with examples you can load into the converter and download as a PDF.
Download PDF opens your browser's print dialog. Choose Save as PDF as the destination.
Your first state diagram
A state diagram, also called a state machine diagram, describes something that is always in exactly one of a fixed set of states: an order that's placed, paid, shipped or cancelled; a traffic light; a document waiting for approval. It shows every state and every allowed move between them, which makes it easy to spot a move that's missing or one that shouldn't be possible.
Start the block with stateDiagram-v2. Each line is a transition: one state, an arrow and
the next state. [*] stands for the start when it's on the left of an arrow, and for the end
when it's on the right.
```mermaid
stateDiagram-v2
[*] --> Green
Green --> Yellow
Yellow --> Red
Red --> Green
```
This traffic light has a start but no end, because it never stops. You may see
stateDiagram without the -v2 in older examples. On md2pdf.me the two draw
exactly the same; stateDiagram-v2 is the name Mermaid's documentation uses.
States and transitions
A state is created the first time it's mentioned. To say what causes a transition, add a colon and a label after it; the label is usually the event or action that triggers the move.
```mermaid
stateDiagram-v2
[*] --> Placed
Placed --> Paid : Payment received
Placed --> Cancelled : Customer cancels
Paid --> Shipped : Courier collects
Shipped --> Delivered : Signed for
Delivered --> [*]
Cancelled --> [*]
```
A state can have any number of arrows in and out, and a state can lead back to itself, as in
Waiting --> Waiting : Check again, for something that repeats until a condition
changes.
Names with spaces and descriptions
A state's name is a single word. For a label with spaces, give the state a short ID and its label in
quotes with state "Waiting for review" as Review, and use the ID in the transitions. To add a
line of detail under a state's name, write the state, a colon and the text on a line of its own.
```mermaid
stateDiagram-v2
state "Waiting for review" as Review
state "Changes requested" as Changes
Review : Assigned to an editor
[*] --> Review
Review --> Changes : Editor comments
Changes --> Review : Author resubmits
Review --> Published : Editor approves
Published --> [*]
```
States inside states
Some states have stages of their own. A video can be playing or paused while it's
open, and closing it ends both. A composite state groups those inner states in a box, with its own
start: write state, the name and the inner diagram in curly braces. Arrows into and out of
the box apply to the whole group.
```mermaid
stateDiagram-v2
[*] --> Closed
Closed --> Open : Open the video
state Open {
[*] --> Playing
Playing --> Paused : Pause
Paused --> Playing : Play
}
Open --> Closed : Close the video
```
Composite states can be nested inside other composite states. Two or three levels is usually the most that stays readable.
Choices
When where a state goes next depends on a condition, add a choice: a small diamond with one arrow in and
a labeled arrow out for each answer. Declare it with <<choice>>, then use it like
any other state.
```mermaid
stateDiagram-v2
state check <<choice>>
[*] --> Submitted
Submitted --> check
check --> Approved : Under $500
check --> Manager : $500 or more
Manager --> Approved : Manager signs off
Manager --> Rejected : Manager declines
Approved --> [*]
Rejected --> [*]
```
Forks, joins and parallel states
A fork splits one path into several that happen at the same time, and a join waits for all of them
before carrying on. Mermaid draws both as a thick bar. Declare them with <<fork>>
and <<join>>.
```mermaid
stateDiagram-v2
state split <<fork>>
state merge <<join>>
[*] --> Accepted
Accepted --> split
split --> Packing
split --> Invoicing
Packing --> merge
Invoicing --> merge
merge --> Dispatched
Dispatched --> [*]
```
For parts of one thing that change independently, such as a keyboard's Caps Lock and Num Lock, split
a composite state into regions with a line of --. Each region is its own small state
machine, drawn side by side inside the box.
```mermaid
stateDiagram-v2
[*] --> Keyboard
state Keyboard {
[*] --> CapsOff
CapsOff --> CapsOn : Caps Lock
CapsOn --> CapsOff : Caps Lock
--
[*] --> NumOff
NumOff --> NumOn : Num Lock
NumOn --> NumOff : Num Lock
}
```
Notes and direction
A note explains a state without being part of the machine. Write note right of or
note left of and the state's name, then the text on the following lines, and close it with
end note. State diagrams are laid out top to bottom; a direction LR line turns
them sideways, which suits a mostly linear life cycle.
```mermaid
stateDiagram-v2
direction LR
[*] --> Trial
Trial --> Paying : Adds a card
Trial --> Expired : 14 days pass
Paying --> Cancelled : Cancels
note right of Trial
Full features,
no card needed
end note
```
Common mistakes
When a state diagram has a mistake, the preview shows its code with the error above it, including the line Mermaid stopped at. These are the usual causes:
- Spaces in a state's name. There's no error, but in
Waiting for review --> Doneeach word becomes a state of its own. Give the state an ID withstate "Waiting for review" as Review. - A note that isn't closed. A note written over several lines needs
end noteafter it. A one-line note can go on the same line instead:note right of Trial : No card needed. - A typo in a state's name. There's no error, but
ShipedandShippedbecome two different states. If the diagram shows a state you didn't expect, look for a misspelling.
State diagrams in a PDF
md2pdf.me draws each diagram as a vector graphic, so the states and labels stay sharp however far you zoom into the PDF, and a diagram is never split across two pages. A diagram wider than the page's text is shrunk to fit it, and so is one taller than most of a page. State diagrams with many states laid out side by side can get small, so try both directions to see which reads better.
Write a sentence or two beside the diagram about what each state means. The diagram shows which moves are possible; the text can explain why, and what someone should do in each state.
A diagram's colors come from the style you choose. Classic, Academic and Résumé draw it in shades of grey for printing, Modern and Report in their blue accent, Worksheet in its own blue and purple, and GitHub in the same colors as github.com. The themed styles use their own colors: oxblood for Gothic, rust and navy for Sandwake, forest green for Elvish, teal and magenta for Cyberpunk, black and oxblood on newsprint grey for Newspaper, and ink and vermilion for Fiendish.
Other diagram types
- Flowcharts: steps, decisions and the arrows between them
- Sequence diagrams: messages between people or systems over time
- Gantt charts: project schedules
- Class diagrams: classes, fields and methods
- Entity relationship diagrams: database tables