Skip to content

Commit 188b68d

Browse files
mchwangclaude
andcommitted
Move focus off a hidden merge control only after the render sets its final state (#194 review round 21)
Focus went to a still-disabled Merge PR (without a runner) and fell to the page. renderMerge now records the focused control, renders, then picks Merge PR only when shown and enabled, otherwise Refresh. The AGENTS.md rule now says the fallback must be shown and enabled and a control's visibility is decided once per update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5910440 commit 188b68d

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ For features with background jobs, polling, retries, cancellation, or shutdown:
3232
- Preserve completed historical results, but visibly mark them stale when their snapshot, plan revision, assignment, or referenced code no longer matches.
3333
- When polling updates one part of the screen, update only that state. Preserve scroll position unless the user was already following the bottom.
3434
- While a request is in flight, do not disable the control that has keyboard focus; disabling it drops focus to the page. Mark it `aria-disabled`, ignore repeat activation with an in-flight guard, and test that focus stays on the control after the response.
35-
- When a background update (a poll, a reload, an answer) hides or removes the control that has keyboard focus, move focus to a named fallback that is still shown, and test that focus lands there for each way the control can be hidden.
35+
- When a background update (a poll, a reload, an answer) hides or removes the control that has keyboard focus, move focus to a named fallback that is still shown and enabled once the update has set every control's final state, and test that focus lands there for each way the control can be hidden. Decide a focusable control's visibility once per update: hiding it and showing it again blurs it.
3636
- When a row or control's visual selection determines the current content or input, expose the same state with the appropriate accessibility attribute, such as `aria-current` or `aria-selected`, and test it across navigation.
3737

3838
## Required race regressions

‎test/browser/review.spec.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,14 +1033,18 @@ test('keeps focus on Merge status while the poll follows a running job with no b
10331033
await page.waitForTimeout(200);
10341034
await expect(page.locator('#merge-details')).toBeFocused();
10351035
});
1036-
test('moves focus off the blockers button to Merge PR when a reload clears the last blocker (D16)',async({page})=>{
1036+
for (const runner of [true,false] as const) test(`moves focus off the blockers button to Merge PR when a reload clears the last blocker, ${runner?'with':'without'} a runner (D16)`,async({page})=>{
10371037
const config={...app.service.config,demo:false};await app.close();removeDemoOutOfScope(config);
10381038
const gateway:MergeGateway={inspect:async()=>{const snapshot=(await app.service.load()).snapshot;return {base:snapshot.base,head:snapshot.head,pullRequestState:'OPEN',mergeable:'MERGEABLE',rulesKnown:true,atomicBaseGuard:true,mergeQueue:false,requiredChecks:[],alreadyFixed:'clear',pullRequest:7,draft:false};},merge:async()=>{throw new Error('The browser test answers every merge request itself.');}};
10391039
app=await startServer(config,0,undefined,gateway);
10401040
let view=await app.service.load();for(const segment of view.segments.filter(value=>value.row==='Unplanned'||value.row==='Ambiguous'))view=await app.service.act({action:'accept',key:segment.key,token:view.token});for(const item of view.items)view=await app.service.act({action:'approve',item:item.id,confirmNoChange:item.count===0,token:view.token});
10411041
let reloaded=false;
10421042
await page.route('**/api/review',async route=>{const response=await route.fetch(),body=await response.json();
1043-
body.merge=runnerMerge(body.merge,{ready:true,action:'merge',job:null,blockers:reloaded?[]:[...body.merge.blockers,...jobResolvedBlockers]});await route.fulfill({response,json:body});});
1043+
// With a runner Merge PR is offered (aria-disabled only while a job runs); without one it is disabled until the last
1044+
// blocker clears, so the fallback must wait for its final enabled state.
1045+
body.merge=runner?runnerMerge(body.merge,{ready:true,action:'merge',job:null,blockers:reloaded?[]:[...body.merge.blockers,...jobResolvedBlockers]})
1046+
:{...body.merge,ready:reloaded,action:reloaded?'merge':null,blockers:reloaded?[]:[...body.merge.blockers,...jobResolvedBlockers]};
1047+
await route.fulfill({response,json:body});});
10441048
await page.goto(app.url);
10451049
await expect(page.locator('#merge-details')).toBeVisible({timeout:10_000});
10461050
await page.locator('#merge-details').focus();
@@ -1050,6 +1054,7 @@ test('moves focus off the blockers button to Merge PR when a reload clears the l
10501054
await answered;
10511055
await expect(page.locator('#merge-details')).toBeHidden();
10521056
await expect(page.locator('#merge')).toBeVisible();
1057+
await expect(page.locator('#merge')).toBeEnabled();
10531058
await expect(page.locator('#merge')).toBeFocused();
10541059
});
10551060
for (const control of ['merge','merge-details'] as const) for (const reload of ['unavailable','failed'] as const) test(`moves focus off ${control==='merge'?'Merge PR':'the blockers button'} to Refresh when a reload ${reload==='failed'?'fails':'makes merging unavailable'} (D16)`,async({page})=>{

‎web/public/app.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,24 @@ async function refresh() {
176176
if (!data) renderPlans();
177177
}
178178
}
179+
/** Where focus goes when the focused merge control hides: Merge PR while it is shown and enabled, otherwise Refresh. */
180+
function mergeFocusFallback() {
181+
return !$("merge").hidden && !$("merge").disabled ? $("merge") : $("reload");
182+
}
179183
function renderMerge() {
180-
const merge = data?.merge;
181-
// Read before hiding: hiding the focused control blurs it at once. A reload that hides the focused merge control (or its
182-
// blockers button) moves focus to what is still shown (AGENTS.md).
184+
// Read before rendering: hiding the focused control blurs it at once. A render that hides the focused merge control (or
185+
// its blockers button) moves focus to what is still shown and enabled, once the controls' final state is set (AGENTS.md).
183186
const focused = document.activeElement;
187+
renderMergeControls();
188+
renderMergeSteps();
189+
if ((focused === $("merge") || focused === $("merge-details")) && focused.hidden) mergeFocusFallback().focus();
190+
}
191+
function renderMergeControls() {
192+
const merge = data?.merge;
184193
$("merge").hidden = !merge?.available;
185194
// Decided once: a running job shows Merge status even with no blockers, and hiding then showing the focused button
186195
// would blur it (and move focus) on every poll.
187196
$("merge-details").hidden = !merge?.available || (!merge.blockers.length && !merge.queue && merge.job?.state !== "running");
188-
if ((focused === $("merge") || focused === $("merge-details")) && focused.hidden) ($("merge").hidden ? $("reload") : $("merge")).focus();
189-
renderMergeSteps();
190197
if (!merge?.available) return;
191198
// Once the attempt this retained key started has ended, a retry is a new action and needs a new key.
192199
if (mergeAction && merge.queue?.actionId === mergeAction.actionId && ["merged", "removed", "failed"].includes(merge.queue.state)) mergeAction = null;
@@ -382,7 +389,7 @@ function renderMergeSteps() {
382389
if (mergeStepTimer && !(shown && job.state === "running")) { clearInterval(mergeStepTimer); mergeStepTimer = null; }
383390
// Hiding a focused Cancel (or the whole bar) would drop keyboard focus to the page: it moves to the merge control
384391
// instead, or to Refresh when that control is hidden too.
385-
if ((!shown || job.state !== "running") && document.activeElement === $("merge-cancel")) ($("merge").hidden ? $("reload") : $("merge")).focus();
392+
if ((!shown || job.state !== "running") && document.activeElement === $("merge-cancel")) mergeFocusFallback().focus();
386393
if (!shown) return;
387394
// Rebuilt only when a step changes; the timer below rewrites the elapsed times alone, so a screen reader keeps its place.
388395
const key = JSON.stringify(job.steps);

0 commit comments

Comments
 (0)