4.2 State Diagrams
Explain state machines.
stateDiagramkeyword.States, transitions, start/end.
1. Explain State Machines
So far, youβve learned how to use Mermaid for flowcharts, sequence diagrams, and class diagrams, which help you map out steps, interactions, and structure. But what if you need to show how something changes over time, or how it behaves based on its current state? Thatβs where state machines β and state diagrams β come in.
β
What is a State Machine?
A state machine (or finite state machine, FSM) is a way to describe how a system or object can:
Exist in one state at a time
Transition from one state to another
React to events or actions that trigger those changes
A state machine is like a map of all possible conditions an object can be in, plus the rules for moving between them.
β
Why Use State Machines?
State machines are used when:
A system has clearly defined statuses.
The systemβs behavior changes depending on its current state.
Certain actions are only valid in specific states.
Theyβre common in:
UI design (button states: idle β hovered β clicked β disabled)
Authentication flows (logged out β logging in β logged in β session expired)
IoT devices (on β standby β off)
Game development (player states: idle β running β jumping)
β
Key Concepts
State: A condition your system can be in. Example: Draft, Published, Archived.
Transition: A rule or trigger that moves the system from one state to another. Example: Submit for review, Approve, Reject.
Event: The thing that causes the transition β like a user action or system event.
Start and End States: State machines usually have a defined starting state and, optionally, an end state to show where the process finishes.
β
How State Machines Help
State machines help you:
Prevent invalid states (How did this get approved without being reviewed?)
Design clear rules (A task canβt go from Archived back to Draft.)
Communicate expected behavior to developers, testers, or stakeholders.
They reduce bugs, because everyone can see exactly which states exist and what transitions are allowed.
β
Example in Real Life
Think of a traffic light:
States: Green, Yellow, Red.
Transitions: Timer expires β Next state.
The light can only be in one state at a time, and the transition rules define how it cycles safely.
In short:
π A state machine is a simple but powerful way to describe how a system moves between conditions β and state diagrams help you visualize those changes clearly, step by step.
2. stateDiagram Keyword
stateDiagram KeywordNow that you understand what a state machine is and why itβs useful, letβs see how to draw one in Mermaid.
Mermaid makes this easy with the stateDiagram keyword.
When you start a diagram with stateDiagram, you tell Mermaid:
βRender this as a state machine diagram, showing states and transitions.β
β
How to Start a State Diagram
A state diagram always begins with:
Below that, you define:
States (the different conditions your system can be in)
Transitions (arrows that connect states)
Start and End states (optional, but helpful for showing where the process begins and finishes)
β
Basic Syntax
The simplest syntax looks like this:
[ * ]represents the start or end state.-->defines a transition from one state to another.
β
Example: Simple Workflow
Hereβs a simple state diagram for a document:
How this works:
The document starts in the
Draftstate.It moves to
Reviewwhen submitted.If approved, it transitions to
Approved.[ * ]at the end shows the process is finished.
β
Adding Labels
You can label transitions to explain what triggers a state change:
Here:
The transition from
DrafttoReviewhappens when someone Submits.From
ReviewtoApprovedif someone Approves.Or back to
Draftif Rejected.
β
Nested States
State diagrams can also have nested states for more complex flows:
This shows:
Shippedis a composite state with its own inner states: In Transit β Out for Delivery β Delivered.
β
When to Use stateDiagram
stateDiagramUse stateDiagram when you want to:
Show how an item or system changes over time.
Make valid states and transitions obvious.
Prevent confusion about invalid jumps (e.g., skipping steps).
In short:
π The
stateDiagramkeyword is your starting point for mapping out conditions, events, and flows β helping you explain exactly how something moves from state to state.
3. States, Transitions, Start/End
When building a state diagram with Mermaid, three elements work together to describe how something moves and changes over time: states, transitions, and start/end points. Letβs break each part down step by step.
β
1οΈβ£ States
A state represents a specific condition your system, object, or process can be in at any moment. Each state has:
A name (like
Draft,Review, orApproved)A unique meaning β only one state is βactiveβ at a time in a simple finite state machine.
In Mermaid, you define states by simply writing their names:
States donβt need brackets or extra symbols β theyβre just named steps in your process.
β
2οΈβ£ Transitions
A transition is the path your system takes to move from one state to another.
Transitions are shown with arrows: -->.
You can:
Use
-->to draw the arrow.Add a label to explain what event triggers the change.
Example:
This means:
The
Draftgoes toReviewwhen you Submit.Reviewgoes toApprovedif you Approve.Or
Reviewgoes back toDraftif you Reject.
β
3οΈβ£ Start and End States
Every good state diagram should show:
Where the process begins (the start state)
Where it finishes (the end state)
Mermaid uses [ * ] (asterisk in brackets) to show both:
[ * ] --> StateNamemeans start here.StateName --> [ * ]means end here.
Example:
This means:
The process starts in
Draft.When it reaches
Approved, itβs done.
β
Putting It All Together
Hereβs a complete example combining all three:
How it works:
The workflow starts in
Draft.It can move to
Reviewwhen submitted.From
Review:If approved, it finishes in
Approved.If rejected, it loops back to
Draft.
The
[ * ]at the end shows the final end state.
β
Why It Matters
States show what your thing can be.
Transitions show how it moves or changes.
Start and end points keep your diagram clear and realistic β everyone can see where it begins and what counts as βdone.β
This makes state diagrams great for modeling:
Content workflows (Draft β Review β Published)
Login states (Logged Out β Logging In β Logged In)
Device modes (Off β Standby β On)
In short:
π States define where you are. π Transitions define how you get there. π Start and end states show where the story begins and ends β giving you a clean, easy-to-follow map of how things change over time.
Last updated