Skip to content

fix(web): window focus no longer pulls keyboard focus off sidebar links into the composer - #5

Merged
ryanilano merged 2 commits into
mainfrom
a11y/composer-focus-steal
Nov 20, 2026
Merged

ryanilano merged 2 commits into
mainfrom
a11y/composer-focus-steal

Conversation

@ryanilano

Copy link
Copy Markdown
Owner

Problem

On chat routes a keyboard user cannot Tab to the sidebar or header. Tabbing past the last control moves focus to the browser chrome; Tabbing back in lands on the first focusable element (the sidebar's "T3 Code home" link). The window then gets a focus event, and the listener in ChatView.tsx moves focus into the composer, because shouldRefocusComposerOnWindowFocus treats a plain link or button as yielding to the composer. The same happens when a link has keyboard focus and the user goes to the address bar (Ctrl+L) or another app (Cmd+Tab) and comes back.

WCAG 2.2 failures: SC 2.1.1 Keyboard (A) and SC 2.4.3 Focus Order (A).

Fix

  • ChatView.tsx: the window-focus effect also listens for window blur and records the focused element and whether it matched :focus-visible.
  • ChatView.logic.ts: on return, keyboard focus (:focus-visible) stays where it is when either:
    • the window blurred with nothing focused (Tab out to the browser chrome clears focus first), or
    • it is the same element that had keyboard focus when the window blurred.

Everything else behaves as before, so the refocus from pingdotgg#10463 still works:

  • Body focus on return refocuses the composer.
  • A clicked button, such as a pull request tab, still yields after Cmd+Tab. In Chromium a lone Cmd, Alt or Ctrl press does not make a clicked button match :focus-visible; checked with Playwright.
  • Focus moved by script while the window is away (for example a dialog closing onto its trigger) still yields to the composer.
  • Until the first window blur, the old behaviour applies.

Test evidence

  • apps/web/src/components/ChatView.logic.browser.test.ts: one new case. It fails on main at the Tab-in assertion and passes with the fix. File: 5/5 passed. ChatView.logic.test.ts: 61/61 passed.
  • check:changed: format, lint, knip, typecheck and react compiler pass. The test step fails one case, ChatView.browser.test.tsx > a shortcut conditioned on rightPanelOpen sees the thread's open right panel, which times out at 15 s on unmodified main as well.
  • Real browser (Chromium, dev server, synthetic thread), document.activeElement after the window focus event, same result on 3 runs each:
Case Before (main) After
Tab in from chrome onto the sidebar "T3 Code home" link composer "T3 Code home" link
Link has keyboard focus, window blurs and returns composer "T3 Code home" link
Return with nothing focused (body) composer composer
Return with the composer focused composer composer
Case Before (main) After
Tab in onto the first sidebar link Tab in onto the first sidebar link, before Tab in onto the first sidebar link, after
Link has keyboard focus, window leaves and returns Link has keyboard focus, window leaves and returns, before Link has keyboard focus, window leaves and returns, after
Nothing focused on return (composer still refocuses) Nothing focused on return (composer still refocuses), before Nothing focused on return (composer still refocuses), after

Surfaces

Web and desktop share this code path. Mobile and coarse-pointer layouts skip the listener entirely, so they are unchanged.

Follow-ups

  • The script cannot drive a real Tab out of headless Chromium's browser chrome. It clears focus, dispatches window blur, Tabs from a sentinel at the top of the body, then dispatches window focus. A manual check in desktop Electron and a headed browser would confirm the real event order.
  • Remaining edge: if the window blurs with nothing focused and script then focuses an element that matches :focus-visible before the window returns, that focus is kept.
  • Pre-existing: window blur does not cancel a pending refocus from a window focus in the last two frames, so a very quick return and leave can still move focus into the composer in the background.
  • Pre-existing: :focus-visible in matches() and closest() throws on Safari before 15.4. The existing selector already depends on it.
  • The rightPanelOpen shortcut browser test times out on main in this environment; worth a separate look.

Built with Claude Opus 5.5 in Claude Code.

…ks into the composer

Cause: on chat routes the window focus listener refocuses the composer
unless focus sits somewhere deliberate. A plain link or button counted as
not deliberate, so when a keyboard user Tabbed out to the browser chrome
and Tabbed back in, focus landed on the first sidebar link, the window
focus event fired, and the composer took focus. Tab could never reach the
sidebar or header (WCAG 2.1.1 Keyboard, 2.4.3 Focus Order).

Fix: remember document.activeElement at window blur. On window focus,
leave focus alone when it moved to a different element while the window
was away and that element matches :focus-visible (keyboard focus). Window
activation restores the same element, so the original behaviour stays:
the composer is refocused from the body or a clicked button, including
after a stray key makes the clicked button match :focus-visible.

Tests: new case in ChatView.logic.browser.test.ts fails on main (Tabbed
link returned true) and passes with the fix; the file passes 5/5 and
ChatView.logic.test.ts passes 61/61. Real-browser before/after against a
dev server: Tab-in focus on the sidebar "T3 Code home" link moved to the
composer before, stays on the link after; body focus still returns to
the composer.
… does not

Review follow-up to the window-focus composer fix.

The first version kept focus only when it landed on a different element
than the one focused at window blur. That missed keyboard users whose
focus returns to the same element (Ctrl+L to the address bar or Cmd+Tab
away, then back), and it treated any script focus while away, such as a
dialog returning focus to its trigger, as a Tab-in.

Now window blur records the focused element and whether it matched
:focus-visible. On return, keyboard focus (:focus-visible) stays put when
the window blurred with nothing focused (Tab out to the browser chrome
clears focus first) or when it is the element that had keyboard focus at
blur. Anything else still yields to the composer. Before any blur is
seen, the old behaviour applies. In Chromium a lone Cmd, Alt or Ctrl
press does not make a clicked button match :focus-visible, so a clicked
pull request tab still yields after Cmd+Tab.

Tests: the browser test is reduced to one case with four assertions; it
fails on main and passes here (5/5 in the file; ChatView.logic.test.ts
61/61). Real-browser runs: Tab-in to the sidebar link and a keyboard
round trip on that link both moved focus to the composer before and stay
on the link after; body focus still refocuses the composer.
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:M labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown

Thread transfer impact

⚠️ The latest CI run did not produce a thread transfer result for 7eddb07.

This comment will update automatically after the next completed run.

@ryanilano
ryanilano merged commit cb6804b into main Nov 20, 2026
47 of 50 checks passed
@ryanilano
ryanilano deleted the a11y/composer-focus-steal branch November 22, 2026 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant