Skip to content

Commit 7651120

Browse files
morgmartCarl
andcommitted
Keep reading and modal focus with retained thread tabs
Resolve reading focus from the current pane after reparenting. Leave body-portalled media Escape to the viewer rather than closing its parent thread. Extend existing ownership and media-confirmation regressions. Co-authored-by: Carl <[email protected]> Signed-off-by: morgmart <[email protected]>
1 parent 6806935 commit 7651120

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

‎src/bundled/channels/useConversationTabs.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ export function useConversationTabs({
503503
event.key === "Escape" &&
504504
!event.nativeEvent.isComposing &&
505505
event.nativeEvent.keyCode !== 229 &&
506-
!event.defaultPrevented
506+
!event.defaultPrevented &&
507+
event.currentTarget.contains(event.target as Node)
507508
) {
508509
event.stopPropagation();
509510
closeConversationTab(tab.id);

‎src/features/messages/use-reading.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,27 @@ it("only the owning selected tab earns dwell; inactive and restored content does
249249
const count = h.reading.mock.calls.length;
250250
vi.advanceTimersByTime(300);
251251
expect(h.reading).toHaveBeenCalledTimes(count);
252+
// A retained thread moves without changing any hook dependencies. The new
253+
// pane's selected header must earn dwell; the former header must not.
254+
const destination = document.createElement("div");
255+
destination.setAttribute("role", "tabpanel");
256+
destination.setAttribute("aria-labelledby", "moved-thread-tab");
257+
const movedTab = document.createElement("button");
258+
movedTab.id = "moved-thread-tab";
259+
movedTab.setAttribute("role", "tab");
260+
movedTab.setAttribute("aria-selected", "true");
261+
document.body.append(movedTab, destination);
262+
if (surface) destination.append(surface);
263+
tab.focus();
264+
vi.advanceTimersByTime(300);
265+
expect(h.reading).toHaveBeenCalledTimes(count);
266+
movedTab.focus();
267+
const movedLease = h.leases.at(-1);
268+
expect(h.reading).toHaveBeenCalledTimes(count + 1);
269+
vi.advanceTimersByTime(299);
270+
expect(movedLease?.observe).not.toHaveBeenCalled();
271+
vi.advanceTimersByTime(1);
272+
expect(movedLease?.observe).toHaveBeenCalledExactlyOnceWith(["visible"]);
252273
});
253274
it("focus moving from the list to its composer keeps reading; another surface's composer cancels it", () => {
254275
const h = setup();

‎src/features/messages/use-reading.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export function useReading({
4848
const surface = element.closest("[data-reading-surface]");
4949
// A tabbed thread's header is outside its content, but aria-labelledby
5050
// identifies its owning tab. Never borrow focus from a sibling tab.
51-
const tabPanel = surface?.closest('[role="tabpanel"]');
5251
const focusedTab = (node: EventTarget | null) => {
52+
// Retained content can move panes without restarting this observation.
53+
const tabPanel = surface?.closest('[role="tabpanel"]');
5354
const label = tabPanel?.getAttribute("aria-labelledby");
5455
const tab = label ? document.getElementById(label) : null;
5556
return (

‎tests/browser/message-management.spec.mjs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ test("saving an attachment caption preserves its original metadata through the b
344344
});
345345

346346
// Native tab order and competing portalled modal handlers need a real browser.
347-
test("media comment deletion keeps keyboard focus in its confirmation", async ({
347+
test("thread media preserves its tab and draft while modal Escape restores focus", async ({
348348
page,
349349
app,
350350
}) => {
@@ -380,7 +380,21 @@ test("media comment deletion keeps keyboard focus in its confirmation", async ({
380380
const row = page.locator(
381381
`[data-channel-timeline] [data-message-id="${root.id}"]`,
382382
);
383-
const opener = row.getByRole("link", { name: "Open image attachment" });
383+
await row.hover();
384+
await row.getByRole("button", { name: "Reply", exact: true }).click();
385+
const thread = page.getByRole("complementary", {
386+
name: "Thread",
387+
exact: true,
388+
});
389+
const editor = thread.getByRole("textbox", {
390+
name: "Reply to thread",
391+
exact: true,
392+
});
393+
await editor.fill("Keep this reply while reviewing media");
394+
const retainedEditor = await editor.elementHandle();
395+
const opener = thread
396+
.locator(`[data-message-id="${root.id}"]`)
397+
.getByRole("link", { name: "Open image attachment" });
384398
await opener.focus();
385399
await opener.press("Enter");
386400
const viewer = page.getByRole("dialog", { name: "Image viewer" });
@@ -425,7 +439,12 @@ test("media comment deletion keeps keyboard focus in its confirmation", async ({
425439
// The viewer resumes keyboard ownership after confirmation dismissal.
426440
await page.keyboard.press("Escape");
427441
await expect(viewer).toHaveCount(0);
428-
await expect(
429-
row.getByRole("link", { name: "Open image attachment" }),
430-
).toBeFocused();
442+
await expect(opener).toBeFocused();
443+
await expect(editor).toHaveText("Keep this reply while reviewing media");
444+
expect(await retainedEditor.evaluate((element) => element.isConnected)).toBe(
445+
true,
446+
);
447+
// A subsequent Escape from the thread itself still closes that tab.
448+
await editor.press("Escape");
449+
await expect(thread).toHaveCount(0);
431450
});

0 commit comments

Comments
 (0)