Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(chat): release hidden floating composer focus (#8998)

mrlonely19c5a23c2b3a
diff --git a/packages/views/chat/components/chat-window.tsx b/packages/views/chat/components/chat-window.tsx
index 8d02c6cca4..57d649160b 100644
--- a/packages/views/chat/components/chat-window.tsx
+++ b/packages/views/chat/components/chat-window.tsx
@@ -188,7 +188,8 @@ export function ChatWindow() {
} = useChatTaskActions(activeSessionId, enqueueLocalRestore);
// Nonce handed to ChatInput to pull focus into the compose box: when a new
// chat starts (⊕ or switching agent), and whenever the window itself opens.
- const { focusRequest, requestInputFocus } = useChatInputFocus(isOpen);
+ const windowRef = useRef<HTMLDivElement>(null);
+ const { focusRequest, requestInputFocus } = useChatInputFocus(isOpen, windowRef);
const [conversationStarterRequest, setConversationStarterRequest] = useState<{
id: number;
content: string;
@@ -760,7 +761,6 @@ export function ChatWindow() {
const isExpanded = useChatStore((s) => s.isExpanded);
- const windowRef = useRef<HTMLDivElement>(null);
const { renderWidth, renderHeight, isAtMax, boundsReady, isDragging, toggleExpand, startDrag } = useChatResize(windowRef);
// Show the list (vs empty state) as soon as there's anything to display —
@@ -835,6 +835,8 @@ export function ChatWindow() {
return (
<motion.div
ref={windowRef}
+ inert={!isOpen}
+ aria-hidden={!isOpen}
className={containerClass}
style={containerStyle}
initial={{ opacity: 0, scale: 0.95, ...motionSize }}
diff --git a/packages/views/chat/components/use-chat-input-focus.test.ts b/packages/views/chat/components/use-chat-input-focus.test.ts
index 9f35bd5603..370a1b33ba 100644
--- a/packages/views/chat/components/use-chat-input-focus.test.ts
+++ b/packages/views/chat/components/use-chat-input-focus.test.ts
@@ -1,8 +1,24 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it } from "vitest";
import { act, renderHook } from "@testing-library/react";
import { useChatInputFocus } from "./use-chat-input-focus";
describe("useChatInputFocus", () => {
+ const roots: HTMLElement[] = [];
+ afterEach(() => {
+ for (const root of roots.splice(0)) root.remove();
+ });
+
+ function makeWindow() {
+ const root = document.createElement("div");
+ const editor = document.createElement("div");
+ editor.contentEditable = "true";
+ editor.tabIndex = 0;
+ root.append(editor);
+ document.body.append(root);
+ roots.push(root);
+ return { root, editor };
+ }
+
it("stays inert on mount, whether the window starts closed or open", () => {
expect(renderHook(() => useChatInputFocus(false)).result.current.focusRequest).toBe(0);
// A persisted "open" preference must not steal focus from the page the user
@@ -39,4 +55,36 @@ describe("useChatInputFocus", () => {
act(() => result.current.requestInputFocus());
expect(result.current.focusRequest).toBe(2);
});
+
+ it("releases the hidden composer's focus when the window closes", () => {
+ const { root, editor } = makeWindow();
+ const windowRef = { current: root };
+ const { result, rerender } = renderHook(
+ ({ isOpen }: { isOpen: boolean }) => useChatInputFocus(isOpen, windowRef),
+ { initialProps: { isOpen: true } },
+ );
+ editor.focus();
+ expect(document.activeElement).toBe(editor);
+
+ rerender({ isOpen: false });
+ expect(document.activeElement).toBe(document.body);
+
+ rerender({ isOpen: true });
+ expect(result.current.focusRequest).toBe(1);
+ });
+
+ it("does not steal focus from a control outside the closing window", () => {
+ const { root } = makeWindow();
+ const outside = document.createElement("input");
+ document.body.append(outside);
+ roots.push(outside);
+ const windowRef = { current: root };
+ const { rerender } = renderHook(
+ ({ isOpen }: { isOpen: boolean }) => useChatInputFocus(isOpen, windowRef),
+ { initialProps: { isOpen: true } },
+ );
+ outside.focus();
+ rerender({ isOpen: false });
+ expect(document.activeElement).toBe(outside);
+ });
});
diff --git a/packages/views/chat/components/use-chat-input-focus.ts b/packages/views/chat/components/use-chat-input-focus.ts
index 6ba9a2eac9..fe11c7e7e5 100644
--- a/packages/views/chat/components/use-chat-input-focus.ts
+++ b/packages/views/chat/components/use-chat-input-focus.ts
@@ -1,6 +1,6 @@
"use client";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useCallback, useEffect, useLayoutEffect, useRef, useState, type RefObject } from "react";
/**
* Owns the floating window's composer-focus nonce.
@@ -20,13 +20,23 @@ import { useCallback, useEffect, useRef, useState } from "react";
* restored from storage, so treating mount as an open event would let a
* persisted "open" preference steal focus from whatever page the user loaded.
*/
-export function useChatInputFocus(isOpen: boolean): {
+export function useChatInputFocus(isOpen: boolean, windowRef?: RefObject<HTMLElement | null>): {
focusRequest: number;
requestInputFocus: () => void;
} {
const [focusRequest, setFocusRequest] = useState(0);
const requestInputFocus = useCallback(() => setFocusRequest((n) => n + 1), []);
+ // The window stays mounted to preserve drafts and its closing animation.
+ // Release its focus before the next keystroke can enter a hidden composer.
+ // Do not blur a dialog or page control the user has already moved to.
+ useLayoutEffect(() => {
+ if (isOpen) return;
+ const container = windowRef?.current;
+ const active = container?.ownerDocument.activeElement;
+ if (active instanceof HTMLElement && container?.contains(active)) active.blur();
+ }, [isOpen, windowRef]);
+
const wasOpenRef = useRef(isOpen);
useEffect(() => {
const wasOpen = wasOpenRef.current;
diff --git a/packages/views/layout/global-shortcuts.chat.test.tsx b/packages/views/layout/global-shortcuts.chat.test.tsx
index 179963ff3a..54edb5ab8f 100644
--- a/packages/views/layout/global-shortcuts.chat.test.tsx
+++ b/packages/views/layout/global-shortcuts.chat.test.tsx
@@ -1,6 +1,9 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { render } from "@testing-library/react";
import { configureShortcutPlatform } from "@multica/core/shortcuts";
+import { openCreateIssueWithPreference } from "@multica/core/issues/stores";
+import { useRef } from "react";
+import { useChatInputFocus } from "../chat/components/use-chat-input-focus";
import { GlobalShortcuts } from "./global-shortcuts";
// The floating chat overlay is reachable from the keyboard (MUL-5522). What
@@ -75,6 +78,30 @@ afterEach(() => {
});
describe("chat toggle shortcut", () => {
+ it("lets C create an issue after the focused floating composer closes", () => {
+ function Composer({ isOpen }: { isOpen: boolean }) {
+ const windowRef = useRef<HTMLDivElement>(null);
+ useChatInputFocus(isOpen, windowRef);
+ return <div ref={windowRef} inert={!isOpen}><div data-testid="composer" contentEditable tabIndex={0} /></div>;
+ }
+ const { getByTestId, rerender } = render(<><GlobalShortcuts /><Composer isOpen /></>);
+ const editor = getByTestId("composer");
+ editor.focus();
+ expect(document.activeElement).toBe(editor);
+
+ const pressCreate = () => document.activeElement?.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "c", bubbles: true, cancelable: true,
+ }));
+ pressCreate();
+ expect(openCreateIssueWithPreference).not.toHaveBeenCalled();
+
+ rerender(<><GlobalShortcuts /><Composer isOpen={false} /></>);
+ expect(document.activeElement).toBe(document.body);
+ expect(getByTestId("composer").parentElement).toHaveAttribute("inert");
+ pressCreate();
+ expect(openCreateIssueWithPreference).toHaveBeenCalledTimes(1);
+ });
+
it("toggles the floating window and consumes the chord", () => {
render(<GlobalShortcuts />);