Skip to content

Commit fa82d8b

Browse files
matt2eclaude
andauthored
fix(messages): end deferred composer focus on any focus move; stabilise hover-preview probe (#1244)
Signed-off-by: Matt Toohey <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 42d90e5 commit fa82d8b

3 files changed

Lines changed: 51 additions & 14 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,23 @@ it("does not reclaim startup focus after another control takes it", async () =>
608608
}
609609
});
610610

611+
it("does not reclaim startup focus after a control that took focus unmounts", async () => {
612+
document.body.setAttribute("inert", "");
613+
try {
614+
const h = mount({ autoFocus: true });
615+
const other = document.createElement("button");
616+
document.body.append(other);
617+
other.focus();
618+
other.remove();
619+
expect(document.activeElement).toBe(document.body);
620+
document.body.removeAttribute("inert");
621+
await act(async () => {}); // Observer callback runs as a microtask.
622+
expect(h.input()).not.toHaveFocus();
623+
} finally {
624+
document.body.removeAttribute("inert");
625+
}
626+
});
627+
611628
it("does not take focus from a modal when the conversation mounts behind it", () => {
612629
const dialog = document.createElement("div");
613630
dialog.setAttribute("role", "dialog");

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,19 +412,22 @@ function Composer({
412412
// A navigation/dialog owner may restore focus during this commit. Let that
413413
// explicit handoff win over the conversation's default initial focus. A
414414
// loading conversation defers it until its composer is first enabled.
415-
if (disabled) {
416-
// Disabling after the handoff ends it; re-enabling must not refocus.
417-
if (focusOnMount.current === input.current)
418-
focusOnMount.current = undefined;
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.
415+
// Focus moved while the composer waits also ends it, even if that control
416+
// later unmounts: restoring an archived channel must not pull focus back.
417+
const endOnFocusMove = () => {
422418
const moved = (event: FocusEvent) => {
423419
if (event.target !== focusOnMount.current)
424420
focusOnMount.current = undefined;
425421
};
426422
document.addEventListener("focusin", moved, true);
427423
return () => document.removeEventListener("focusin", moved, true);
424+
};
425+
if (disabled) {
426+
// Disabling after the handoff ends it; re-enabling must not refocus.
427+
if (focusOnMount.current === input.current)
428+
focusOnMount.current = undefined;
429+
if (!focusOnMount.current) return;
430+
return endOnFocusMove();
428431
}
429432
if (!focusOnMount.current) return;
430433
const focus = () => {
@@ -454,17 +457,22 @@ function Composer({
454457
};
455458
const inertAncestor = input.current?.closest("[inert]");
456459
if (inertAncestor) {
460+
const stopWatching = endOnFocusMove();
457461
const observer = new MutationObserver(() => {
458462
if (!inertAncestor.hasAttribute("inert")) {
459463
observer.disconnect();
464+
stopWatching();
460465
focus();
461466
}
462467
});
463468
observer.observe(inertAncestor, {
464469
attributes: true,
465470
attributeFilter: ["inert"],
466471
});
467-
return () => observer.disconnect();
472+
return () => {
473+
observer.disconnect();
474+
stopWatching();
475+
};
468476
}
469477
focus();
470478
}, [disabled]);

‎tests/browser/buzz-links.spec.mjs‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,28 @@ test("a hovered message preview survives the timeline's scroll-time pointer-even
331331
const link = page
332332
.locator(`[data-channel-timeline] [data-message-id="${message.id}"]`)
333333
.getByRole("link", { name: "Alpha", exact: true });
334-
await expect(link).toBeVisible();
334+
await expect(link).toBeInViewport();
335335
await virtuaIdle(page);
336336
const preview = page.getByLabel("Message preview", { exact: true });
337-
await link.hover();
337+
// Move the raw pointer: Locator.hover retries by scrolling the timeline,
338+
// and each scroll restarts Virtua's pointer-event hold.
339+
let box = await link.boundingBox();
340+
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
338341
await expect(
339342
preview.getByText("Broadcast reply", { exact: true }),
340343
).toBeVisible();
344+
// Opening the preview can re-measure rows and scroll the list. WebKit can
345+
// then take the link out of its hover state although the pointer stays on
346+
// it, and the probe below would wait for a mouseleave that cannot happen.
347+
// After the list settles, move onto the link again (1px aside, as WebKit
348+
// can ignore a move to the unchanged position) and prove it is hovered.
349+
await virtuaIdle(page);
350+
box = await link.boundingBox();
351+
const point = { x: box.x + box.width / 2 - 1, y: box.y + box.height / 2 };
352+
await page.mouse.move(point.x, point.y);
353+
await expect
354+
.poll(() => link.evaluate((element) => element.matches(":hover")))
355+
.toBe(true);
341356
const closed = await preview.evaluateHandle((popup) => {
342357
const seen = { closed: false };
343358
new MutationObserver(() => {
@@ -349,11 +364,8 @@ test("a hovered message preview survives the timeline's scroll-time pointer-even
349364
// Virtua stops the list hit-testing until 150ms after each scroll. When
350365
// the browser hit-tests the still pointer in that time, as WebKit does
351366
// after a scroll, it reports a mouseleave from the link. A 1px move that
352-
// stays on the link makes that hit-test happen now; WebKit can ignore a
353-
// move to the unchanged position.
367+
// stays on the link makes that hit-test happen now.
354368
const list = page.locator('[data-channel-timeline="alpha"] ol');
355-
const box = await link.boundingBox();
356-
const point = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
357369
const hitTest = async (pointerEvents) => {
358370
await list.evaluate((element, value) => {
359371
element.style.pointerEvents = value;

0 commit comments

Comments
 (0)