Sub topic 8
Showing Progress When the Path Varies
This chapter assumes you have read the earlier chapters on flow state, navigation, and branching. You should know the FlowState shape (current, history, data, errors) and how each Step declares its own next function. The code is framework-neutral TypeScript, so you can wire it to any UI layer.
Why progress is harder in a branching flow
A linear flow with five steps has an obvious indicator: "Step 3 of 5". Once a flow branches, the number of remaining steps depends on answers the user has not given yet. A business account sees a company step that a personal account skips. If you print "Step 1 of 2" and the user then picks "business", the total becomes 3. The indicator has changed its own answer.
Users read progress indicators as promises. They use them to decide whether to continue now or come back later. An indicator that grows, shrinks, or counts backward breaks that trust, and it can push people to abandon a flow they would have finished.
You have three honest options:
- Show only the current position, with no total.
- Show a total only once the remaining path is known.
- Show a rough estimate and label it as one.
This chapter builds the first two and explains when the third is acceptable.
What a progress indicator should communicate
A progress indicator answers two questions: where am I, and how much is left? The first question can always be answered exactly. The second can only be answered when the path is settled.
The flow state you already have contains the exact answer to the first question. history holds every step the user has completed on the way here, so the current position is history.length + 1. This number stays correct after a branch, and it also stays correct after Back, because back removes the last history entry.
The second question depends on future decisions. Any design must handle the period where the answer is unknown.
Option 1: position without a total
The simplest indicator uses only the history length.
type Data = Record<string, string>;
type FlowState = {
current: string;
history: string[];
data: Data;
errors: string[];
};
function progressLabel(state: FlowState): string {
return "Step " + (state.history.length + 1);
}This label is always accurate. It cannot contradict itself, because it reports only what has already happened. It never promises an end, so it gives the user no sense of how much effort remains.
Use this option when the flow is short, when the number of branches is large, or when the path length depends on data you cannot predict, such as a "Add another dependent?" loop.
Option 2: projecting the remaining path
Because each step declares its own next function, you can ask the flow where it would go from here, using the data collected so far. Following those answers step by step gives a projected path. The loop below needs a guard against cycles and against unknown step ids, since a mistake in one next function should not freeze the interface.
type Step = {
id: string;
validate(data: Data): string[];
next(data: Data): string | null;
};
function projectPath(
state: FlowState,
steps: Record<string, Step>
): string[] {
const path: string[] = [state.current];
const seen = new Set<string>(path);
let id = steps[state.current].next(state.data);
while (id !== null) {
if (seen.has(id) || steps[id] === undefined) break;
path.push(id);
seen.add(id);
id = steps[id].next(state.data);
}
return path;
}projectPath returns the current step plus every step the flow would visit if the data stayed as it is. The number of remaining steps is path.length - 1.
The projection is only as good as the data. At the first step, the user may not have chosen an account type. The account step's next function sees undefined, treats it as "not business", and projects a two-step path. That is the contradiction we are trying to avoid. The projection is wrong because an answer is missing, not because the projection code is wrong.
Knowing when the path is settled
A projection is trustworthy only when every step along it has the data it needs to decide where to go next. Record that dependency in data, next to the steps.
const decidingFields: Record<string, string[]> = {
account: ["accountType"],
company: [],
review: [],
};
function isSettled(
state: FlowState,
steps: Record<string, Step>
): boolean {
const path = projectPath(state, steps);
return path.every((id) =>
(decidingFields[id] ?? []).every((field) => {
const value = state.data[field];
return value !== undefined && value !== "";
})
);
}A step with an empty list always knows its next step, such as company, which always leads to review. A step such as account is settled only once accountType has a value.
Now combine the pieces into one label function.
function progressLabelWithTotal(
state: FlowState,
steps: Record<string, Step>
): string {
const position = state.history.length + 1;
if (!isSettled(state, steps)) return "Step " + position;
const remaining = projectPath(state, steps).length - 1;
return "Step " + position + " of " + (position + remaining);
}Before the user chooses an account type, the label reads "Step 1". After they choose "business", it reads "Step 1 of 3". After they choose "personal", it reads "Step 1 of 2". The total appears only when it can be trusted.
Here is a small walkthrough that you can run with the earlier steps definition from the branching chapter:
const steps: Record<string, Step> = {
account: {
id: "account",
validate: (d) =>
/^\S+@\S+\.\S+$/.test(d.email ?? "") ? [] : ["Enter a valid email address."],
next: (d) => (d.accountType === "business" ? "company" : "review"),
},
company: {
id: "company",
validate: (d) => ((d.company ?? "").trim() ? [] : ["Enter your company name."]),
next: () => "review",
},
review: {
id: "review",
validate: () => [],
next: () => null,
},
};
const start: FlowState = {
current: "account",
history: [],
data: {},
errors: [],
};
console.log(progressLabelWithTotal(start, steps));
// "Step 1"
const business: FlowState = { ...start, data: { accountType: "business" } };
console.log(progressLabelWithTotal(business, steps));
// "Step 1 of 3"
const personal: FlowState = { ...start, data: { accountType: "personal" } };
console.log(progressLabelWithTotal(personal, steps));
// "Step 1 of 2"What happens when the user changes an answer
The total depends on data, so it can change when the user changes an answer. Suppose a user reaches the review step through the business path, then goes back to the account step and switches to "personal". The history shrinks as they go back, and the total changes from 3 to 2.
This change is acceptable because the user caused it, and it happens in response to an action they just took. What users find confusing is a total that changes while they are doing nothing related to it. Make sure of two things:
- The label updates immediately when the deciding answer changes, not only after the user presses Next.
- Data from a branch the user abandoned is not silently submitted. If the user switched from business to personal, decide whether to clear
companyor to ignore it at submission. The review chapter covers this.
Option 3: an estimate
Some flows cannot be settled until late. A tax form may branch on the answers of five different steps. In that case you can show a rough total and mark it as approximate, for example "About 6 steps". If you do this, follow these rules:
- Show a range or the word "about", never a precise number you may change.
- Never reduce the estimate and then raise it again. Pick the longest plausible path early, and let the number shrink as branches are ruled out. Users forgive a flow that turns out shorter, but not one that turns out longer.
- Fall back to the position-only label if you cannot compute a sensible estimate.
A shrinking estimate can be built from the same tools. Take the longest path that any combination of answers could produce, and recompute it as data arrives. Doing this well requires walking the whole step graph, not just following the current next functions, so reserve it for flows where the cost is justified.
Choosing between the options
| Situation | Best indicator | Reason |
|---|---|---|
| Short flow, one early branch | Position with total once settled | The total is cheap to compute and quickly becomes reliable |
| Flow with loops, such as repeatable sections | Position only | The total depends on a count the user has not given |
| Long flow with several late branches | Estimate that only shrinks | Users want a sense of length, and a shrinking number never disappoints |
| Flow that must be accurate for legal or support reasons | Named steps instead of numbers | A list of section names avoids numeric promises |
Presenting the indicator
How you present the progress matters as much as how you compute it.
- Put the text label first. A bar or dots are supplements, not replacements. A text label such as "Step 2 of 3" can be read by every user and every assistive technology.
- Avoid a progress bar whose fill is
position / totalunless the total is settled. A bar that jumps backward is more jarring than a number that changes. - When the total is unknown, use a bar-free design, or a simple label with the step name, such as "Step 2: Company details".
- Name the current step. A title such as "Company details" gives context that a number cannot.
- Keep the label in a consistent place so users can find it on every step.
The next chapters cover how to announce changes to this label for screen reader users.
Common mistakes
- Computing the total at the start. Counting all steps in the
stepsrecord ignores branches, so a personal account sees "Step 1 of 3" and then ends at step 2. - Using the index of the current step in the steps record. The record's order has nothing to do with the user's path. Use the history length.
- Forgetting the cycle guard. A
nextfunction that returns an earlier id turns the projection loop into an infinite loop and freezes the tab. - Treating
undefinedas a real answer. Projection with missing data produces a plausible but wrong path. Check that deciding fields are present before showing a total. - Counting the review step inconsistently. Decide once whether review is a numbered step, and apply that choice everywhere. Many flows count it, since it takes user effort.
Summary
The current position is always exact, and it comes from history.length + 1. The total is a prediction, so show it only when every step on the projected path has the data it needs. Until then, show the position alone, or an estimate that never grows. Keep next functions pure so projection is safe, and always show a text label so every user can read the progress.
Exercise
A flow has steps start, details, extra, and review. The start step goes to extra if needsExtra is "yes" and to details otherwise. details goes to review. extra goes to details. A user is on start and has not yet answered needsExtra. What should the progress label show, and what should it show after the user answers "yes"? Which entry would you add to decidingFields?
Show answer
Before the answer, the label should show only "Step 1", because the path length is unknown: projecting with a missing answer would give start, details, review (3 steps), which becomes 4 after "yes". After the answer "yes", the path is start, extra, details, review, so the label is "Step 1 of 4". Add start: ["needsExtra"] to decidingFields, with empty lists for details, extra, and review.
Exercise
Why should a next function used by projectPath avoid side effects, and why does projectPath keep a seen set?
Show answer
projectPath calls next functions speculatively, with data that may be incomplete, and may call them on every render. A side effect such as a network request or a state write would run at times the user never intended. The seen set stops the loop if a next function returns an id that was already visited, which would otherwise repeat forever and freeze the interface.