Skip to content

fix(ui): keep keyboard focus inside open dialogs - #5632

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/modal-focus-trap
Oct 23, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/modal-focus-trap

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Tab could walk out of an open dialog and onto the page hidden behind it. In Settings, 26 presses landed on the sidebar's resize handle under the overlay, so a keyboard or screen-reader user ended up operating a page they couldn't see. Now Tab and Shift+Tab wrap inside every modal in the Agent UI, including when its content changes while open. Escape and returning focus to the opener work exactly as before.

Test plan

  • npx vitest run src/hooks/__tests__/useFocusTrap.test.tsx: wraps forward and back, skips disabled and hidden elements, re-reads changed content, pulls focus in from outside, only the innermost of nested traps acts, inactive does nothing
  • npx vitest run src/components/settings/__tests__/SettingsDialogFocus.test.tsx: Tab from the last control in Settings returns to the first section button
  • MemoryDashboard.a11y.test.tsx still passes on the shared hook
  • Disabling the hook makes all 8 trap tests fail
  • Full npx vitest run (146 files, 1532 tests) and npx tsc --noEmit pass
  • Electron jest size check (reasonably sized component files): none of the touched files is close to the cap. On a Windows CRLF checkout the check fails on main for ChatView.tsx (101,751 bytes in git, 103,725 on disk), which this PR doesn't touch. Confirm it is green in CI.
  • Desktop app: open Settings (Ctrl+,) and hold Tab. Focus cycles inside the dialog and never reaches the sidebar. Esc still closes it and returns focus to the opener.

Tab could leave an open modal and land on the page behind it: in Settings,
26 presses reached the sidebar's resize handle under the overlay, so keyboard
and screen-reader users ended up operating a page they couldn't see.

Memory's beta-confirm trap moves into a shared useFocusTrap hook, and every
modal that sets aria-modal now uses it: Settings, Document library, Scheduled
tasks, File browser, Mobile access, Version picker, Context panel, both change
reviews, the commit/PR dialog, the folder picker, and the session panel while
it is an overlay. Tab and Shift+Tab wrap at either end, the focusable list is
re-read on each key press, and when dialogs nest only the innermost one acts.
Escape and focus-return are unchanged.
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes. The only blocker is proof that this works in the real app. The code itself looks ready to merge.

This PR stops Tab and Shift+Tab from leaving an open dialog and landing on the page behind it. It does that with one shared hook, wired into every modal in the Agent UI, and the old hand-rolled trap in Memory now uses the same hook. The hook handles nested dialogs, disabled and hidden controls, and content that changes while a dialog is open, and the tests cover each of those cases. I found no bugs.

  • Show it working in the real app. All the evidence so far comes from jsdom, which doesn't calculate real visibility or tab order. The one desktop check in the test plan is still unticked. Please attach a short Playwright run against gaia chat --ui: open Settings, hold Tab, and show a screenshot with focus still inside the dialog. That ticks the box and satisfies the rubric.

Real-world evidence

N/A: no evidence bundle was produced for this run, and this is an Agent UI change whose rendered check needs a real browser. I couldn't run the new vitest suites here because the webui dependencies aren't installed. So this verdict rests on reading the code plus the test results the PR reports (146 files, 1532 tests, tsc clean). None of it was run in a real browser.

🔍 Technical details

Issues

🟡 No real-browser evidence for an Agent UI change. Per the rubric, an Agent UI change needs a Playwright before→after screenshot embedded in the PR description. The jsdom tests are good at gating the logic, but getComputedStyle and the native tab order behave differently in Chromium/Electron. The unticked "Desktop app: open Settings (Ctrl+,) and hold Tab" item is exactly that gap.

🟢 Content inside a closed <details> counts as tabbable (hooks/useFocusTrap.ts:31-37). A browser doesn't render the children of a closed <details>, but their computed display isn't none, so isShown keeps them. Suppose one of those children is a button that comes last in the dialog. On the real last control, atEnd would then be false, the hook would step aside, and the browser would tab out of the dialog. Nothing breaks today: Settings → Models has a <details> (settings/ModelSettings.tsx:347), but the unavailable rows inside it render no buttons. Still, this is the same escape the PR is fixing, so it's cheap to close off. checkVisibility() covers closed details, display:none and visibility (Chromium 105+, so it works in Electron). It may need a jsdom guard in the tests:

function isShown(el: HTMLElement, container: HTMLElement): boolean {
    if (typeof el.checkVisibility === 'function' && !el.checkVisibility({ visibilityProperty: true })) return false;
    if (getComputedStyle(el).visibility === 'hidden') return false;
    for (let n: HTMLElement | null = el; n && n !== container.parentElement; n = n.parentElement) {
        if (n.hidden || n.hasAttribute('inert') || getComputedStyle(n).display === 'none') return false;
    }
    return true;
}

🟢 Every Tab press re-scans the whole dialog (hooks/useFocusTrap.ts:40-55). Each press walks every candidate up through its ancestors calling getComputedStyle, and the radio-group filter runs in O(n²). For the File browser or Document library with hundreds of rows, that's thousands of style reads per keypress. It's probably fine in practice, but worth checking with a large folder during the desktop test.

Strengths

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Checked live in the desktop app. Settings held focus through 60 Tabs and 30 Shift+Tabs; before this change, focus escaped to the sidebar after 26. The Document library and Scheduled tasks held it through 40 Tabs each. Escape still closes Settings and puts focus back in the message box.

Controls inside a closed <details> counted as tabbable, so a hidden last button could make the trap step aside and let Tab leave the dialog. Only the summary of a closed <details> is a stop now.

Each Tab press is also cheaper: checkVisibility() replaces the per-ancestor style walk where the browser has it, and the radio-group filter is one pass instead of O(n^2).
@kovtcharov

Copy link
Copy Markdown
Contributor Author

Both 🟢 items from the last review are fixed.

  • Closed <details>: controls inside a closed <details> no longer count as Tab stops; only its summary does. So a hidden button can no longer make the trap step aside and let Tab out of the dialog. There's a new test that fails without the fix.
  • Cost per Tab press: where the browser has checkVisibility() (Chromium/Electron do), one native call replaces the style read on every ancestor. jsdom keeps the old walk. The radio-group filter is now one pass. The trap still re-reads the dialog on every press, so content that changes while it's open is still handled.
🔍 Technical details
  • New tests: closed and nested <details>, content of an open <details>, both radio-group rules (checked radio, first when none checked), and the checkVisibility path, which is stubbed because jsdom doesn't implement it.
  • inert is still checked only inside the dialog, now with closest() instead of in the walk.
  • npx vitest run: 146 files, 1537 tests pass. npx tsc --noEmit is clean.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 23, 2026
Merged via the queue into amd:main with commit 0f6f91c Oct 23, 2026
29 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/modal-focus-trap branch October 23, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants