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 how a FlowState holds the current step, a history stack, and collected data, and how each step's next function picks the following step.
Why a fixed total breaks
A progress indicator such as "Step 2 of 4" works when every user takes the same path. In a branching flow, the number of remaining steps depends on answers that may not be given yet.
Take the account flow from the earlier chapters. A personal account goes account then review. A business account goes account, company, then review. On the first screen you cannot know whether the total is 2 or 3 until the user picks an account type.
If you show "Step 1 of 3" and the user then takes the short path, the indicator ends at "Step 2 of 2". The total changed under the user, and it looks like a bug.
Options for progress display
There are several ways to handle this. Each trades accuracy against honesty.
| Approach | Example | Weakness |
|---|---|---|
| Current step only | "Step 2" | Gives no sense of how much is left |
| Worst-case total | "Step 2 of 3" | Overstates the work on short paths |
| Recomputed total | "Step 2 of 2" | Total jumps after a branch |
| Section names | "Account, Company, Review" | Needs a stable grouping of steps |
Choose by how much the path varies. If branches differ by one or two steps, show the current step only. If they differ a lot, group steps into stable sections and show the section.
Counting from history
The current step number does not need a stored counter. The history stack already records the steps the user has passed through, so the number is its length plus one.
function progressLabel(state: FlowState): string {
return "Step " + (state.history.length + 1);
}This stays correct when the user goes back. The back function removes the last history entry, so the label drops by one. It also stays correct after a branch. A user who skipped company has a shorter history, and the label counts only the steps they actually visited.
Do not count by the position of a step in the steps object. That position ignores skipped steps and would show "Step 3" to a user who has seen only two screens.
Known remaining steps
Sometimes you can tell how many steps remain once a branch is resolved. After the user chooses an account type, the rest of the path is fixed. You can walk the next functions forward from the current step, using the data collected so far, to count the steps ahead.
function remainingSteps(state: FlowState, steps: Record<string, Step>): number {
let count = 0;
let id = steps[state.current].next(state.data);
const seen = new Set<string>([state.current]);
while (id !== null && !seen.has(id)) {
seen.add(id);
count += 1;
id = steps[id].next(state.data);
}
return count;
}The seen set stops the loop if a bad next function ever creates a cycle.
This count is a projection, not a promise. On the account step before the user has chosen a type, d.accountType is undefined, so next returns "review" and the count is 1. The user may then pick business, and the real count becomes 2. Only show a total when the answers that decide the branch have already been given. Otherwise fall back to the current-step label.
function progressText(state: FlowState, steps: Record<string, Step>): string {
const current = state.history.length + 1;
const branchDecided = state.current !== "account";
if (!branchDecided) return "Step " + current;
return "Step " + current + " of " + (current + remainingSteps(state, steps));
}Here the branch is known to be decided once the user has left the account step. This check is specific to this example flow. In your own flow, record which steps decide a branch, for instance with a flag on the Step type.
Keeping the display consistent
Whatever you choose, follow these rules:
- Never let the number go down when the user moves forward.
- Never let the total change without the user having made a choice that explains it.
- Use the same label in the visible text and in what assistive technology reads out. The accessibility chapter covers this.
- Count only screens the user sees. A step with no input, skipped automatically, should not be counted.
In the diagram, the label on Account shows only "Step 1". Totals appear after the branch is decided.
Exercise
A user is on the company step of the business path, having visited account first. What does progressLabel return, and what does progressText return?
Show answer
progressLabel returns "Step 2", because the history holds one entry (account) and 1 + 1 = 2. progressText returns "Step 2 of 3". The branch is decided because the current step is not account, and remainingSteps counts one step ahead (review), so the total is 2 + 1 = 3.
Exercise
Why is it a poor choice to derive the step number from the position of the step in the steps object?
Show answer
Position ignores branching. A user who skips company goes straight to review, which sits third in the object, so they would see "Step 3" after seeing only two screens. Counting history entries reflects the screens actually visited.