Skip to content

Commit 8aaa37c

Browse files
author
Smartie
committed
Fix composer focus when selecting channels and DMs
Signed-off-by: Smartie <fb3185faacfc3760b6b4fe0085a8878d8dd537f7a7671c7d3184fe1aa1b04df4@buzz.block.builderlab.xyz>
1 parent e5ea85e commit 8aaa37c

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎src/bundled/channels/ChannelsPage.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,9 @@ function ChannelWorkspace({
10041004
scope={scope}
10051005
channelId={current.id}
10061006
channelName={current.name}
1007+
autoFocus={
1008+
!current.readOnly && !requestedMessage && !requestedThread
1009+
}
10071010
onOpenLink={openLink}
10081011
canOpenLink={canOpenLink}
10091012
label={

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,34 @@ function mount(
379379
};
380380
}
381381

382+
it("autofocuses each selected conversation once without stealing focus on updates", () => {
383+
const h = mount({ autoFocus: true });
384+
expect(h.input()).toHaveFocus();
385+
const other = document.createElement("button");
386+
document.body.append(other);
387+
try {
388+
other.focus();
389+
h.retarget({ channelName: "Renamed", autoFocus: false });
390+
h.retarget({ autoFocus: true });
391+
expect(other).toHaveFocus();
392+
h.retarget({ channelId: "another-channel" });
393+
expect(h.input()).toHaveFocus();
394+
} finally {
395+
other.remove();
396+
}
397+
});
398+
399+
it("leaves focus alone unless an enabled composer opts into mount focus", () => {
400+
const h = mount();
401+
expect(h.input()).not.toHaveFocus();
402+
h.retarget({
403+
channelId: "disabled-channel",
404+
disabled: true,
405+
autoFocus: true,
406+
});
407+
expect(h.input()).not.toHaveFocus();
408+
});
409+
382410
it("keeps unpublished completions invisible but lets Escape revoke pending work", () => {
383411
const h = mount();
384412
const input = h.input();

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export type MessageComposerProps = {
9292
replyContext?: ReactNode;
9393
mediaTimeSeconds?: number;
9494
clearMediaTime?(): void;
95+
/** Focus once when this conversation mounts, not when overlays close. */
96+
autoFocus?: boolean;
9597
focusRequest?: number;
9698
hideMediaTimeIndicator?: boolean;
9799
disabled?: boolean;
@@ -138,6 +140,7 @@ function Composer({
138140
replyContext,
139141
mediaTimeSeconds,
140142
clearMediaTime,
143+
autoFocus = false,
141144
focusRequest,
142145
hideMediaTimeIndicator = false,
143146
disabled: requestedDisabled = false,
@@ -202,6 +205,10 @@ function Composer({
202205
const valueRef = useRef(value);
203206
const caret = useRef<number | undefined>(undefined);
204207
const input = useRef<ComposerInputElement>(null);
208+
const focusOnMount = useRef(autoFocus && !disabled);
209+
useEffect(() => {
210+
if (focusOnMount.current) input.current?.focus();
211+
}, []);
205212
const nonmembers = useNonmemberMentions(session, channelId, () =>
206213
input.current?.focus(),
207214
);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test, expect } from "./fixture.mjs";
2+
import { open } from "./timeline.mjs";
3+
4+
test.use({ productionBroker: true, dmLabels: true });
5+
6+
// Browser boundary: real sidebar navigation must hand focus to the mounted
7+
// ProseMirror editor, including after a warm return; typing must need no click.
8+
test("selecting a channel or DM focuses its composer and retains drafts", async ({
9+
page,
10+
app,
11+
}) => {
12+
await open(page, app);
13+
const sidebar = page.getByRole("navigation", { name: "Subscribed channels" });
14+
const conversation = page.getByRole("article", { name: "Conversation" });
15+
const input = conversation.getByRole("textbox");
16+
for (const [index, name] of [
17+
"Beta",
18+
"Alice Fixture",
19+
"Beta",
20+
"Alice Fixture",
21+
].entries()) {
22+
await sidebar.getByRole("button", { name, exact: true }).click();
23+
await expect(input).toBeFocused();
24+
if (index < 2) {
25+
await page.keyboard.type(`Draft for ${name}`);
26+
}
27+
await expect(input).toHaveText(`Draft for ${name}`);
28+
}
29+
const settings = page.getByRole("button", {
30+
name: "Channel settings",
31+
exact: true,
32+
});
33+
await settings.click();
34+
await expect(
35+
page.getByRole("button", { name: "Close channel settings" }),
36+
).toBeFocused();
37+
});

0 commit comments

Comments
 (0)