Skip to content

fix(agent-dashboard): restore focus to the opener when the drawer closes - #34398

Merged
nwparker merged 1 commit into
mainfrom
bugbash/pr-25640
Oct 27, 2026
Merged

nwparker merged 1 commit into
mainfrom
bugbash/pr-25640

Conversation

@nwparker

@nwparker nwparker commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​192 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​192
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​50 $\color{#cf222e}{\Huge{\mathbf{−}}}$​10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​40

ELI5

When you opened the Agents side panel and then closed it with Escape or the Close button, your keyboard focus "fell off" the app. Nothing was selected, so typing went nowhere until you clicked something. Now the app remembers what you were using when you opened the panel, like the sidebar button or a terminal, and puts you back there when you close it. If you closed it by clicking somewhere else, or by jumping to an agent, it leaves you where you went.

What Changed

  • Before: closing the in-window Agents drawer with Escape or Close left document.activeElement on <body>.
  • After: focus returns to the element that opened the drawer.
  • Mechanism (AgentDashboardDrawer.tsx):
    • A layout effect records the focused element on each open transition. If focus is already inside the sheet, it keeps the earlier opener, which covers reopening during the exit animation.
    • onCloseAutoFocus focuses that opener only when focus would otherwise be lost (active element is body) and the close was not a focus handoff.
    • Three kinds of close count as handoffs and leave focus where it went: outside pointer dismissal, a successful agent reveal, and switching to the pop-out. A failed reveal still restores focus.
  • New AgentDashboardDrawer.focus.test.tsx has 9 cases. It uses the real Radix Sheet and the real outside-dismiss hook.

Why

The drawer is opened from the store and has no Radix DialogTrigger. On close, Radix's non-modal close autofocus only focuses triggerRef, which is null here, and then calls preventDefault(). That also blocks FocusScope's own restore of the previously focused element, so focus lands on <body>. Restoring focus on every close would steal focus from an outside click or a revealed agent pane, so the drawer now tells the two kinds of close apart.

Linked Issue

Fixes #25570

Visual Proof

The fix changes where keyboard focus goes and has no visual or styling change. @kambarakun captured these screenshots in #25640 with the same sidebar → search → Escape sequence in a disposable Electron fixture. The focus outline exists only in the test. Before, focus is on BODY. After, the sidebar button has focus again.

Before After
Before: keyboard focus falls to the page body After: keyboard focus returns to Agent Dashboard

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Reproduced in a unit test. Run against main's drawer source, the new focus test fails 5 of 9 cases with "expected <body> to be <button>": Escape, Close, store dismissal followed by a new opener, reopening during the exit animation, and a failed reveal. All 9 pass with this change.

  • Real Electron, headless with ORCA_BACKGROUND_LAUNCH=1, using the contributor's e2e scenarios from a scratch copy. On main, 2 of 3 fail (no focus after Escape/Close, and after reopening during the exit animation). With this change, 3 of 3 pass. The e2e spec is not checked in.

  • 5 extra probes also pass on this branch: no focus steal, handoff flag resets, opener removed from the DOM, nothing focused at open, and reopen after pop-out. The neighbouring dashboard and outside-dismiss suites pass too.

  • oxlint, oxfmt --check, the anti-slop, code-quality and ast-grep gates, and check-changed-code-quality are clean on the touched files.

  • Platform: macOS. The change uses only DOM focus, so nothing is platform-specific.

Review

  • Root cause confirmed against the installed Radix dialog and focus-scope sources.
  • Compared with fix(agent-dashboard): restore focus when dismissing the drawer #25640, this version removes the canReturnFocus ancestor walk. Chromium's focus() already does nothing on disconnected, disabled, hidden or inert elements.
  • It also removes the stale-callback content tracking. Radix fires close autofocus only after the content unmounts, so the body check already covers a reopen during the exit animation.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Renderer focus policy only: no IPC, no persisted state, no new global listeners, no styling change.
  • Local, SSH and folder workspaces are unaffected, and so are mixed client/server versions.
  • Mobile has no in-window drawer, so it is unaffected.

Original fix by @kambarakun in #25640. Authorship is preserved on the commit.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

The store-driven Agents drawer has no Radix trigger, so dismissing it with
Escape or the close button dropped focus to document.body. Capture the
focused element on each open transition and return focus to it on close,
unless focus already moved elsewhere or the close hands focus off (outside
pointer dismissal, agent reveal, switching to the pop-out).

Co-authored-by: Neil <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The drawer records the element focused when it opens. On close, it restores focus to that element only when focus is absent or on body and no focus handoff occurred. Agent reveal, pop-out, and outside dismissal use handoff behavior. Tests cover focus restoration, handoffs, reopening, and stale close callbacks.

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Low · up to 2b24b

In two dismissal cases, keyboard focus can end up on the page instead of returning to the drawer opener. These bounded issues should be fixed or explicitly accepted before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the main change: focus is restored to the opener when the Agents drawer closes. It also names the scope, agent-dashboard, and uses the fix type. The title matches the changeset, which …
Description check Passed The description follows the repository template and fills in the main sections. ELI5, What Changed, Why, Linked Issue (Fixes #25570), Visual Proof, Testing, Review, Agent skill upstream boundary, Note…
Linked Issues check Passed Directly linked issue #25570 requires that closing the in-window Agents drawer while focus is inside it (Escape, close button, or a card action) returns focus to the opener if the opener still exists …
Out of Scope Changes check Passed The PR changes two files only, matching the summary: AgentDashboardDrawer.tsx (+50/-10) and new AgentDashboardDrawer.focus.test.tsx (+192). Every production change serves focus return on drawer close:…
Full details: Title check

Explanation

The title names the main change: focus is restored to the opener when the Agents drawer closes. It also names the scope, agent-dashboard, and uses the fix type. The title matches the changeset, which adds focus restoration to AgentDashboardDrawer.tsx and a focus test file.

Full details: Description check

Explanation

The description follows the repository template and fills in the main sections. ELI5, What Changed, Why, Linked Issue (Fixes #25570), Visual Proof, Testing, Review, Agent skill upstream boundary, Notes, and Checklist are all present with substantive content. The AI Disclosure section is left empty, which the template allows for internal contributors. The description does not show whether the author is a stablyai team member, so that section needs a check. The Visual Proof section links screenshots from another PR (#25640) rather than attaching screenshots to this PR. The template asks for before and after visuals for UI behavior changes, so the reviewer should confirm those images are accessible from this PR.

Full details: Linked Issues check

Explanation

Directly linked issue #25570 requires that closing the in-window Agents drawer while focus is inside it (Escape, close button, or a card action) returns focus to the opener if the opener still exists and the user has not moved focus elsewhere. Reviewed head AgentDashboardDrawer.tsx: (1) A useLayoutEffect on each open transition stores document.activeElement as focusReturnRef.opener when it is an HTMLElement outside the sheet and not body. A reopen while focus is already inside the sheet keeps the earlier opener. (2) handleCloseAutoFocus calls preventDefault and focuses the stored opener when handoff is false and document.activeElement is null or body. Radix unmount leaves focus on body, so this covers Escape (close()), the close button (onClose=close), and a failed agent reveal (onClose). (3) Outside-pointer dismissal (useWorkspaceKanbanOutsideDismiss via closeForFocusHandoff), a successful reveal (revealDashboardAgent returns true, then onFocusHandoff), and pop-out switching (onFocusHandoff) set handoff=true, so focus stays where the user moved it. This matches the issue's terminal-click exception. The reset of handoff on each open prevents stale flags. Store-driven closes with focus outside the sheet do not move focus, because activeElement is not body. The issue's card-action case maps to the reveal path, which is handled. Test file AgentDashboardDrawer.focus.test.tsx covers Escape, the close button, store dismissal with outside focus, outside pointerdown handoff, next-opener capture, stale close callback, reveal success and failure, and pop-out. The PR reports that 5 of 9 cases fail on main. Limits: the base-to-head production diff could not be read, so I checked head content and the supplied +50/-10 summary. I did not run the tests. The e2e spec is not checked in. The issue's suggested check for body or inside-sheet focus is narrowed to body only. This is equivalent in practice, because focus is already on body after unmount.

Full details: Out of Scope Changes check

Explanation

The PR changes two files only, matching the summary: AgentDashboardDrawer.tsx (+50/-10) and new AgentDashboardDrawer.focus.test.tsx (+192). Every production change serves focus return on drawer close: the opener-capture layout effect, the handoff flag and closeForFocusHandoff wrapper, the onCloseAutoFocus handler, and the handoff callback passed to AgentDashboardDrawerBody for reveal and pop-out. The handoff call on pop-out and the reveal branch change only focus behavior. The test file exercises only the drawer's focus behavior with mocked dependencies. No IPC, persisted state, styling, or unrelated component changes appear in the PR summary. The PR description's screenshots and the attribution to #25640 are documentation, not code, and do not affect scope.

Full details: Docstring Coverage

Explanation

Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 89f219e2-14bc-4f90-b595-5eb1513067d7
Commits

Reviewing files that changed from the base of the PR and between ffed50d and 2b24bdd.

Files selected for processing: 2
  • src/renderer/src/components/dashboard/AgentDashboardDrawer.focus.test.tsx
  • src/renderer/src/components/dashboard/AgentDashboardDrawer.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

Comment on lines +52 to +53
if (revealDashboardAgent(args)) {
onFocusHandoff()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

set -o pipefail
printf '%s\n' '--- drawer ---'
nl -ba src/renderer/src/components/dashboard/AgentDashboardDrawer.tsx | sed -n '1,180p'
printf '%s\n' '--- reveal symbols ---'
rg -n -F --glob '*.{ts,tsx}' -e 'function revealDashboardAgent' -e 'const revealDashboardAgent' -e 'revealDashboardAgent =' -e 'activateTabAndFocusPane' -e 'revealTerminalPane' -e 'interface DashboardRevealAgentArgs' -e 'type DashboardRevealAgentArgs' src
printf '%s\n' '--- dashboard snapshot declarations ---'
rg -n -F -- 'DashboardRevealAgentArgs' src/shared src/renderer
printf '%s\n' '--- focused reveal tests ---'
rg -n -F --glob '*AgentDashboardDrawer*' -e 'leafId' -e 'revealDashboardAgent' -e 'focus' src/renderer/src

Repository: stablyai/orca

Length of output: 34547


Script executed:

set -o pipefail
printf '%s\n' '--- reveal dashboard ---'
nl -ba src/renderer/src/components/dashboard/reveal-dashboard-agent.ts
printf '%s\n' '--- reveal terminal ---'
nl -ba src/renderer/src/lib/reveal-terminal-pane.ts
printf '%s\n' '--- activate and focus ---'
nl -ba src/renderer/src/lib/activate-tab-and-focus-pane.ts
printf '%s\n' '--- snapshot type ---'
nl -ba src/shared/dashboard-snapshot.ts | sed -n '176,198p'
printf '%s\n' '--- drawer null-leaf test and surrounding cases ---'
nl -ba src/renderer/src/components/dashboard/AgentDashboardDrawer.test.tsx | sed -n '115,195p'
printf '%s\n' '--- reveal helper tests ---'
nl -ba src/renderer/src/components/dashboard/reveal-dashboard-agent.test.ts | sed -n '1,90p'

Repository: stablyai/orca

Length of output: 9970


Script executed:

set -o pipefail
printf '%s\n' '--- owner tab activation ---'
nl -ba src/renderer/src/lib/terminal-tab-owner-activation.ts
printf '%s\n' '--- board reveal callback wiring ---'
rg -n -F --glob '*.{ts,tsx}' -e 'onRevealAgent' -e 'handleRevealAgent' src/renderer/src/components/dashboard-popout src/renderer/src/components/dashboard
printf '%s\n' '--- board component relevant block ---'
rg -n -F --glob '*.{ts,tsx}' -e 'function AgentKanbanBoard' -e 'export function AgentKanbanBoard' src/renderer/src/components/dashboard-popout
printf '%s\n' '--- drawer remaining source ---'
nl -ba src/renderer/src/components/dashboard/AgentDashboardDrawer.tsx | sed -n '180,290p'

Repository: stablyai/orca

Length of output: 8703


Script executed:

set -o pipefail
printf '%s\n' '--- board callback call site ---'
nl -ba src/renderer/src/components/dashboard-popout/AgentKanbanBoard.tsx | sed -n '270,310p'
printf '%s\n' '--- card reveal data flow ---'
rg -n -F --glob '*.{ts,tsx}' -e 'leafId:' -e 'leafId' src/renderer/src/components/dashboard src/renderer/src/components/dashboard-popout src/shared/dashboard-snapshot.ts | sed -n '1,220p'
printf '%s\n' '--- snapshot producer definitions ---'
rg -n -F --glob '*.{ts,tsx}' -e 'DashboardSnapshot' -e 'AgentCard' -e 'cards:' src/renderer/src/components/dashboard src/shared | sed -n '1,220p'

Repository: stablyai/orca

Length of output: 34491


Do not assume every successful reveal hands off focus.

When leafId is null, revealDashboardAgent returns true after workspace activation, but activateTabAndFocusPane returns before scheduling a pane-focus request. Local workspaces have no owner focus handoff. The drawer then marks the handoff as complete and suppresses opener restoration.

Treat the reveal as a focus handoff only when a replacement target receives focus, or restore opener focus when the null-leaf reveal leaves focus on body.

Comment on lines 149 to 159
},
[setOpen]
)
const boardRef = useRef<HTMLDivElement | null>(null)

useWorkspaceKanbanOutsideDismiss({
open,
boardRef,
preserveOpenForMenu: menuOpen,
onOpenChange: setOpen
onOpenChange: closeForFocusHandoff
})

useEffect(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

sed -n '90,165p' src/renderer/src/components/dashboard/AgentDashboardDrawer.tsx
sed -n '100,195p' src/renderer/src/components/dashboard/AgentDashboardDrawer.focus.test.tsx
rg -n 'useOutside|outsideDismiss|onPointerDownOutside|onInteractOutside' src/renderer/src/components/dashboard/AgentDashboardDrawer.tsx

Repository: stablyai/orca

Length of output: 6541


Script executed:

rg -n -F -e 'useWorkspaceKanbanOutsideDismiss' -- src/renderer/src || test "$?" -eq 1
f=$(rg -l -F -e 'export function useWorkspaceKanbanOutsideDismiss' -- src/renderer/src | head -n 1)
echo "FILE: $f"
if [ -n "$f" ]; then nl -ba "$f" | sed -n '1,200p'; fi

Repository: stablyai/orca

Length of output: 5098


Outside-pointer dismissal skips opener return when the click lands on a nonfocusable area.

closeForFocusHandoff sets handoff = true for every outside-pointer dismissal. When the clicked target cannot take focus, focus moves to body and nothing replaces it. handleCloseAutoFocus then skips the opener, so focus stays on body. The issue asks for focus to return to the opener unless the user moved focus elsewhere. A click on a nonfocusable area does not move focus to a control.

Pass close to the outside-dismiss hook instead. A focusable target keeps focus, so the existing body check already prevents a restore there.

Suggested fix
   useWorkspaceKanbanOutsideDismiss({
     open,
     boardRef,
     preserveOpenForMenu: menuOpen,
-    onOpenChange: closeForFocusHandoff
+    onOpenChange: close
   })
Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
},
[setOpen]
)
const boardRef = useRef<HTMLDivElement | null>(null)
useWorkspaceKanbanOutsideDismiss({
open,
boardRef,
preserveOpenForMenu: menuOpen,
onOpenChange: setOpen
onOpenChange: closeForFocusHandoff
})
useEffect(() => {
},
[setOpen]
)
useWorkspaceKanbanOutsideDismiss({
open,
boardRef,
preserveOpenForMenu: menuOpen,
onOpenChange: close
})
useEffect(() => {

@nwparker
nwparker merged commit d69d16a into main Oct 27, 2026
33 checks passed
@nwparker
nwparker deleted the bugbash/pr-25640 branch October 27, 2026 11:39
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.

[Bug]: Closing the Agents drawer drops keyboard focus to <body> instead of returning it to the opener

2 participants