Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(composer): check for Escape when the attachment refocus frame runs

Absterrg07f489a94cda2
diff --git a/src/components/Composer.escape.test.ts b/src/components/Composer.escape.test.ts
index 2f706f2ae4..9ad58d0ed7 100644
--- a/src/components/Composer.escape.test.ts
+++ b/src/components/Composer.escape.test.ts
@@ -149,6 +149,18 @@ describe("Escape in the composer", () => {
expect(document.activeElement).not.toBe(input);
});
+ it("stays out of the composer when Escape comes between a finished paste and its refocus", async () => {
+ const input = render();
+ await nextFrame();
+ focusWith(input, "");
+ pasteFile(input);
+ fixture.finishIntake();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ pressEscape(input);
+ await nextFrame();
+ expect(document.activeElement).not.toBe(input);
+ });
+
it("still returns to the composer when a pasted file finishes without Escape", async () => {
const input = render();
await nextFrame();
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 1e5e6dac0a..ce77ed3109 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -242,10 +242,10 @@ export function Composer({
const caretRef = useRef(0);
caretRef.current = caret;
const escapeBlurs = useRef(0);
- const refocusInput = useCallback(() => {
+ const refocusInput = useCallback((escapesBefore: number) => {
requestAnimationFrame(() => {
const input = inputRef.current;
- if (!input || input.disabled || !composerShouldRefocus(document.activeElement, input)) return;
+ if (escapeBlurs.current !== escapesBefore || !input || input.disabled || !composerShouldRefocus(document.activeElement, input)) return;
const at = Math.min(caretRef.current, input.value.length);
input.focus();
input.setSelectionRange(at, at);
@@ -504,7 +504,7 @@ export function Composer({
} finally {
changeDraftAttachmentPending(draftId, false);
}
- if (escapeBlurs.current === escapesBefore) refocusInput();
+ refocusInput(escapesBefore);
};
const hasContent = Boolean(effectiveText.trim()) || attachments.length > 0;