Skip to content

Commit 623da63

Browse files
committed
Merge main into Harnesses docs
Signed-off-by: OpenAI Codex <[email protected]>
2 parents 37b490c + 67fe94c commit 623da63

5 files changed

Lines changed: 127 additions & 4 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,64 @@ 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+
h.retarget({ channelId: "keyboard-navigation" });
395+
expect(h.input()).toHaveFocus();
396+
} finally {
397+
other.remove();
398+
}
399+
});
400+
401+
it("lets an explicit focus restoration in the mount commit win", () => {
402+
const h = mount();
403+
h.unmount();
404+
const target = document.createElement("button");
405+
document.body.append(target);
406+
function RestoreFocus() {
407+
useLayoutEffect(() => target.focus(), []);
408+
return null;
409+
}
410+
try {
411+
render(
412+
<>
413+
<MessageComposer
414+
session={h.session}
415+
scope="scope"
416+
channelId="channel"
417+
channelName="General"
418+
autoFocus
419+
/>
420+
<RestoreFocus />
421+
</>,
422+
);
423+
expect(target).toHaveFocus();
424+
} finally {
425+
target.remove();
426+
}
427+
});
428+
429+
it("leaves focus alone unless an enabled composer opts into mount focus", () => {
430+
const h = mount();
431+
expect(h.input()).not.toHaveFocus();
432+
h.retarget({
433+
channelId: "disabled-channel",
434+
disabled: true,
435+
autoFocus: true,
436+
});
437+
expect(h.input()).not.toHaveFocus();
438+
});
439+
382440
it("keeps unpublished completions invisible but lets Escape revoke pending work", () => {
383441
const h = mount();
384442
const input = h.input();

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 24 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,27 @@ function Composer({
202205
const valueRef = useRef(value);
203206
const caret = useRef<number | undefined>(undefined);
204207
const input = useRef<ComposerInputElement>(null);
208+
const focusOnMount = useRef(
209+
autoFocus && !disabled && typeof document !== "undefined"
210+
? document.activeElement
211+
: undefined,
212+
);
213+
useEffect(() => {
214+
// A navigation/dialog owner may restore focus during this commit. Let that
215+
// explicit handoff win over the conversation's default initial focus.
216+
const previous = focusOnMount.current;
217+
if (
218+
previous &&
219+
(previous === document.activeElement ||
220+
(!previous.isConnected && document.activeElement === document.body))
221+
) {
222+
const editor = input.current;
223+
if (!editor) return;
224+
const end = editor.value.length;
225+
editor.setSelectionRange(end, end);
226+
editor.focus();
227+
}
228+
}, []);
205229
const nonmembers = useNonmemberMentions(session, channelId, () =>
206230
input.current?.focus(),
207231
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
await expect(input).toHaveText(`Draft for ${name}`);
27+
} else {
28+
await expect(input).toHaveText(`Draft for ${name}`);
29+
await page.keyboard.type(" continued");
30+
await expect(input).toHaveText(`Draft for ${name} continued`);
31+
}
32+
}
33+
const settings = page.getByRole("button", {
34+
name: "Channel settings",
35+
exact: true,
36+
});
37+
await settings.click();
38+
await expect(
39+
page.getByRole("button", { name: "Close channel settings" }),
40+
).toBeFocused();
41+
});

‎tests/browser/gifs.spec.mjs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,7 @@ test.describe("GIF send roundtrip", () => {
437437
});
438438

439439
await page.goto(app.origin);
440-
await page
441-
.getByRole("navigation", { name: "Pages", exact: true })
442-
.getByRole("button", { name: "Messages", exact: true })
443-
.click();
440+
await openPage(page, "Messages");
444441

445442
const draft = page.getByRole("textbox", {
446443
name: "Message #Alpha",

0 commit comments

Comments
 (0)