Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): r3-h2 restore keyboard focus when the active workspace tab is closed

Diego Zacarias4a1c52c7f37b
diff --git a/desktop-app/ui/src/components/WorkspaceTabStrip/WorkspaceTabStrip.test.tsx b/desktop-app/ui/src/components/WorkspaceTabStrip/WorkspaceTabStrip.test.tsx
index 340d49d11..761edc855 100644
--- a/desktop-app/ui/src/components/WorkspaceTabStrip/WorkspaceTabStrip.test.tsx
+++ b/desktop-app/ui/src/components/WorkspaceTabStrip/WorkspaceTabStrip.test.tsx
@@ -7,6 +7,7 @@ import { readFileSync } from 'node:fs'
import path from 'node:path'
import { createSessionFsmStore, projectSessionState } from '@hooks/domain/sessionFsm'
import {
+ closeWorkspaceTab,
createEmptyWorkspaceTabsState,
createWorkspaceTabsState,
newChatTab,
@@ -452,4 +453,77 @@ describe('WorkspaceTabStrip — reorder (drag & keyboard)', () => {
expect(document.activeElement).toBe(screen.getByRole('button', { name: 'Bravo' }))
expect(document.activeElement).toBe(bravo)
})
+
+ // R3-H2: unlike a reorder (which keeps focus because the keyed node survives), a
+ // close removes the focused control, so a keyboard user who closes the ACTIVE tab
+ // would drop to <body>. Focus must follow the workspace to the tab that took over.
+ it('moves keyboard focus to the surviving active tab when the active tab is closed (R3-H2, T3)', () => {
+ // Drive the real store reducer: closing active 'b' (index 1) promotes 'c'.
+ let state: WorkspaceTabsState = { tabs: threeTabs, activeTabId: 'b' }
+ const onClose = (id: string) => {
+ state = closeWorkspaceTab(state, id)
+ }
+ const { rerender } = render(
+ <WorkspaceTabStrip
+ tabs={state.tabs}
+ activeTabId={state.activeTabId}
+ onSelect={vi.fn()}
+ onClose={onClose}
+ onReorder={vi.fn()}
+ />
+ )
+ const closeActive = screen.getByRole('button', { name: 'Close Bravo' })
+ closeActive.focus()
+ expect(document.activeElement).toBe(closeActive)
+ // Keyboard activation of the close control: Enter/Space fire a click with
+ // `detail === 0` (a pointer click reports >= 1).
+ fireEvent.click(closeActive, { detail: 0 })
+ rerender(
+ <WorkspaceTabStrip
+ tabs={state.tabs}
+ activeTabId={state.activeTabId}
+ onSelect={vi.fn()}
+ onClose={onClose}
+ onReorder={vi.fn()}
+ />
+ )
+ expect(state.tabs.map(t => t.id)).toEqual(['a', 'c'])
+ expect(state.activeTabId).toBe('c')
+ // Observable result: focus landed on the now-active tab's control, not <body>.
+ const nowActive = screen.getByRole('button', { name: 'Charlie' })
+ expect(document.activeElement).toBe(nowActive)
+ })
+
+ it('keeps keyboard focus in the strip when the last tab is closed (R3-H2)', () => {
+ let state: WorkspaceTabsState = { tabs: [threeTabs[0]!], activeTabId: 'a' }
+ const onClose = (id: string) => {
+ state = closeWorkspaceTab(state, id)
+ }
+ const { container, rerender } = render(
+ <WorkspaceTabStrip
+ tabs={state.tabs}
+ activeTabId={state.activeTabId}
+ onSelect={vi.fn()}
+ onClose={onClose}
+ onReorder={vi.fn()}
+ />
+ )
+ const closeActive = screen.getByRole('button', { name: 'Close Alpha' })
+ closeActive.focus()
+ fireEvent.click(closeActive, { detail: 0 })
+ rerender(
+ <WorkspaceTabStrip
+ tabs={state.tabs}
+ activeTabId={state.activeTabId}
+ onSelect={vi.fn()}
+ onClose={onClose}
+ onReorder={vi.fn()}
+ />
+ )
+ expect(state.tabs).toHaveLength(0)
+ expect(state.activeTabId).toBeNull()
+ // No tab survives, so focus stays in the strip rather than dropping to <body>.
+ const strip = container.querySelector('.chat-view-tabs') as HTMLElement
+ expect(document.activeElement).toBe(strip)
+ })
})
diff --git a/desktop-app/ui/src/components/WorkspaceTabStrip/index.tsx b/desktop-app/ui/src/components/WorkspaceTabStrip/index.tsx
index 093f23717..0e85818f5 100644
--- a/desktop-app/ui/src/components/WorkspaceTabStrip/index.tsx
+++ b/desktop-app/ui/src/components/WorkspaceTabStrip/index.tsx
@@ -1,4 +1,4 @@
-import { useContext, useState } from 'react'
+import { useContext, useLayoutEffect, useRef, useState } from 'react'
import { makeTaskKey } from '@contexts/AgentTaskTrackerContext/types'
import { ChatListContext } from '@contexts/ChatListContext'
import { Button } from '@components/Common'
@@ -39,12 +39,42 @@ export function WorkspaceTabStrip({
const chatList = useContext(ChatListContext)
const [draggingId, setDraggingId] = useState<string | null>(null)
const [dropTarget, setDropTarget] = useState<DropTarget | null>(null)
+ const stripRef = useRef<HTMLDivElement | null>(null)
+ // Set when a KEYBOARD close removes the active tab's control: the close, unlike a
+ // reorder, drops the keyed node the user was on, so focus would fall to <body>.
+ // Consumed by the layout effect once the neighbour becomes active this commit.
+ const focusAfterActiveCloseRef = useRef(false)
const clearDrag = () => {
setDraggingId(null)
setDropTarget(null)
}
+ // Restore keyboard focus after a close removed the active tab. The reorder path
+ // keeps focus for free (the moved node survives, keyed by id); the close does not,
+ // so move focus to whichever tab took over — or to the strip itself when the
+ // workspace is now empty — rather than letting it drop to <body>.
+ useLayoutEffect(() => {
+ if (!focusAfterActiveCloseRef.current) return
+ focusAfterActiveCloseRef.current = false
+ const strip = stripRef.current
+ if (!strip) return
+ const nextActive = strip.querySelector<HTMLElement>(
+ '.chat-view-tab.is-active .chat-view-tab__select'
+ )
+ ;(nextActive ?? strip).focus()
+ }, [activeTabId])
+
+ const handleCloseClick = (event: React.MouseEvent<HTMLButtonElement>, tab: WorkspaceTab) => {
+ // Enter/Space on a button reports `detail === 0`; a pointer click reports >= 1.
+ // Only manage focus for keyboard users closing the ACTIVE tab: a pointer user
+ // does not need it, and forcing focus on click could surprise them.
+ if (event.detail === 0 && tab.id === activeTabId) {
+ focusAfterActiveCloseRef.current = true
+ }
+ onClose(tab.id)
+ }
+
const handleDrop = (event: React.DragEvent<HTMLElement>, target: WorkspaceTab) => {
event.preventDefault()
const fromId = event.dataTransfer.getData('text/plain') || draggingId
@@ -77,7 +107,13 @@ export function WorkspaceTabStrip({
}
return (
- <div className="chat-view-tabs" role="toolbar" aria-label="Workspace tabs">
+ <div
+ className="chat-view-tabs"
+ role="toolbar"
+ aria-label="Workspace tabs"
+ ref={stripRef}
+ tabIndex={-1}
+ >
<div className="chat-view-tabs__scroller">
<div className="chat-view-tabs__list">
{tabs.map((tab, index) => {
@@ -138,7 +174,7 @@ export function WorkspaceTabStrip({
aria-label={`Close ${tab.title}`}
className="chat-view-tab__close"
color="neutral"
- onClick={() => onClose(tab.id)}
+ onClick={event => handleCloseClick(event, tab)}
size="xs"
variant="ghost"
>