Skip to content

Commit 42d90e5

Browse files
loganjLarry
andauthored
fix(messages): keep restored-channel focus off the composer (#1243)
Signed-off-by: Logan Johnson <[email protected]> Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent 152dc6c commit 42d90e5

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/features/messages/MessageComposer.test.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,17 @@ it("does not take focus when the composer is enabled after another control takes
569569
}
570570
});
571571

572+
it("does not take focus when the composer is enabled after a control that took focus unmounts", () => {
573+
const h = mount({ autoFocus: true, disabled: true });
574+
const other = document.createElement("button");
575+
document.body.append(other);
576+
other.focus();
577+
other.remove();
578+
expect(document.activeElement).toBe(document.body);
579+
h.retarget({ disabled: false });
580+
expect(h.input()).not.toHaveFocus();
581+
});
582+
572583
it("defers initial focus until an inert startup ancestor is revealed", async () => {
573584
document.body.setAttribute("inert", "");
574585
try {

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,15 @@ function Composer({
416416
// Disabling after the handoff ends it; re-enabling must not refocus.
417417
if (focusOnMount.current === input.current)
418418
focusOnMount.current = undefined;
419-
return;
419+
if (!focusOnMount.current) return;
420+
// Focus moved while the composer waits also ends it, even if that control
421+
// later unmounts: restoring an archived channel must not pull focus back.
422+
const moved = (event: FocusEvent) => {
423+
if (event.target !== focusOnMount.current)
424+
focusOnMount.current = undefined;
425+
};
426+
document.addEventListener("focusin", moved, true);
427+
return () => document.removeEventListener("focusin", moved, true);
420428
}
421429
if (!focusOnMount.current) return;
422430
const focus = () => {

0 commit comments

Comments
 (0)