Skip to content

Commit f416250

Browse files
author
Larry
committed
Count only closing popups as the closing tree, and gate the exit in the test
Review found two problems. useFinalFocusUnlessMoved treated focus under any [data-closed] ancestor as not moved. Base UI also marks collapsed Collapsible and Accordion roots data-closed, and visible controls can sit inside them. The exception now matches only popup roles (menu, dialog, alertdialog, listbox). menu-dismiss.spec relied on a 400ms transition, so the race might not happen. The test now pauses the menu's exit transition when it starts, checks that closing has started, moves focus while the menu is still mounted, and then releases the transition. It also waits until the entrance has settled: an Escape before the entrance starts closes with no transition to hold. A new case moves focus under an unrelated data-closed container. Fail-before, 10 runs per engine, chromium and webkit: - without the hook's moved check: 10/10 fail (focus pulled to the row) - with the old broad [data-closed] match: 10/10 fail (collapsed case) - with this change: 10/10 pass Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent 35dd419 commit f416250

2 files changed

Lines changed: 98 additions & 24 deletions

File tree

‎src/shared/design-system/ui/finalFocus.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ type CloseType = Parameters<
1313
Extract<FinalFocus, (...args: never) => unknown>
1414
>[0];
1515

16+
/**
17+
* Focus inside another closing popup (a submenu, or the popup this one is
18+
* nested in) is part of the closing tree, not a user move. Collapsed
19+
* disclosures also carry `data-closed`, so match popup roles only.
20+
*/
21+
const closingPopup =
22+
':is([role="menu"], [role="dialog"], [role="alertdialog"], [role="listbox"])[data-closed]';
23+
1624
/**
1725
* Keep an explicit `finalFocus` from pulling focus back after the user moved it.
1826
*
@@ -53,7 +61,7 @@ export function useFinalFocusUnlessMoved(
5361
active !== doc.body &&
5462
active !== target &&
5563
!popup.current?.contains(active) &&
56-
!active.closest("[data-closed]");
64+
!active.closest(closingPopup);
5765
return moved ? false : target;
5866
},
5967
[finalFocus],

‎tests/browser/menu-dismiss.spec.mjs‎

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,57 @@ import { test, expect } from "./fixture.mjs";
22
import { open } from "./timeline.mjs";
33

44
// Browser-only boundary: Base UI restores focus when a popup's exit
5-
// transition ends. A slow transition lets the user's next click land first.
6-
const slowMenus = `:is(.buzz-menu-popup, .buzz-popover-popup) {
7-
transition-duration: 400ms !important;
8-
}`;
9-
const settled = (locator) =>
10-
expect
11-
.poll(() => locator.evaluate((element) => element.getAnimations().length))
5+
// transition ends. The test holds that transition, so the user's next focus
6+
// move lands while the old menu is still closing.
7+
async function holdMenuExit(page) {
8+
await page.evaluate(() => {
9+
document.addEventListener(
10+
"transitionrun",
11+
(event) => {
12+
if (
13+
event.target instanceof HTMLElement &&
14+
event.target.matches(".buzz-menu-popup[data-ending-style]")
15+
)
16+
for (const animation of event.target.getAnimations())
17+
animation.pause();
18+
},
19+
true,
20+
);
21+
});
22+
return {
23+
// Closing has started and cannot finish until release().
24+
held: (popup) =>
25+
expect
26+
.poll(() =>
27+
popup.evaluate((element) =>
28+
element.getAnimations().some((a) => a.playState === "paused"),
29+
),
30+
)
31+
.toBe(true),
32+
release: () =>
33+
page.evaluate(() => {
34+
for (const animation of document.getAnimations())
35+
if (animation.playState === "paused") animation.finish();
36+
}),
37+
};
38+
}
39+
40+
// The entrance has started and settled. Animations alone are not enough: an
41+
// Escape before the entrance transition starts closes without any transition.
42+
async function opened(popup) {
43+
await expect(popup).toHaveAttribute("data-open", "");
44+
await expect(popup).not.toHaveAttribute("data-starting-style");
45+
await expect
46+
.poll(() => popup.evaluate((element) => element.getAnimations().length))
1247
.toBe(0);
48+
}
1349

1450
test("closing a menu keeps focus where the user moved it", async ({
1551
page,
1652
app,
1753
}) => {
1854
await open(page, app);
19-
await page.addStyleTag({ content: slowMenus });
55+
const exit = await holdMenuExit(page);
2056
const row = page
2157
.getByRole("navigation", {
2258
name: "Subscribed channels",
@@ -29,20 +65,50 @@ test("closing a menu keeps focus where the user moved it", async ({
2965
exact: true,
3066
});
3167

32-
await row.click({ button: "right" });
33-
await settled(rowMenu);
34-
await page.keyboard.press("Escape");
35-
await composer.click();
36-
await expect(composer).toBeFocused();
37-
// Only now does the row menu finish closing. Its explicit final focus (the
38-
// row) must not pull focus back out of the composer.
39-
await expect(rowMenu).toHaveCount(0);
40-
await expect(composer).toBeFocused();
68+
try {
69+
await row.click({ button: "right" });
70+
await opened(rowMenu);
71+
await page.keyboard.press("Escape");
72+
await exit.held(rowMenu);
73+
await composer.click();
74+
await expect(composer).toBeFocused();
75+
await expect(rowMenu).toHaveCount(1);
76+
await exit.release();
77+
// The row menu has finished closing. Its explicit final focus (the row)
78+
// must not pull focus back out of the composer.
79+
await expect(rowMenu).toHaveCount(0);
80+
await expect(composer).toBeFocused();
81+
82+
// A collapsed disclosure (Base UI marks it data-closed, like a closing
83+
// popup) is ordinary page content. Focus moved into it also stays.
84+
await page.evaluate(() => {
85+
const collapsed = document.createElement("div");
86+
collapsed.setAttribute("data-closed", "");
87+
collapsed.innerHTML = '<button type="button">Collapsed control</button>';
88+
document.getElementById("main-content").append(collapsed);
89+
});
90+
const collapsedControl = page.getByRole("button", {
91+
name: "Collapsed control",
92+
});
93+
await row.click({ button: "right" });
94+
await opened(rowMenu);
95+
await page.keyboard.press("Escape");
96+
await exit.held(rowMenu);
97+
await collapsedControl.focus();
98+
await expect(rowMenu).toHaveCount(1);
99+
await exit.release();
100+
await expect(rowMenu).toHaveCount(0);
101+
await expect(collapsedControl).toBeFocused();
41102

42-
// With nothing else focused, Escape still returns focus to the row.
43-
await row.click({ button: "right" });
44-
await settled(rowMenu);
45-
await page.keyboard.press("Escape");
46-
await expect(rowMenu).toHaveCount(0);
47-
await expect(row).toBeFocused();
103+
// With nothing else focused, Escape still returns focus to the row.
104+
await row.click({ button: "right" });
105+
await opened(rowMenu);
106+
await page.keyboard.press("Escape");
107+
await exit.held(rowMenu);
108+
await exit.release();
109+
await expect(rowMenu).toHaveCount(0);
110+
await expect(row).toBeFocused();
111+
} finally {
112+
await exit.release();
113+
}
48114
});

0 commit comments

Comments
 (0)