fix: complete submenu panel ARIA, focus and event passthrough

- Submenu panel name priority is explicit aria-label > menu content channel >
  this instance's trigger; an explicit label drops the id reference so it cannot
  be shadowed.
- openAutoFocus and animationend chain with the caller's handlers like keydown
  and interact-outside, so caller callbacks run and can cancel.
- The panel loop falls back to the menu-level shouldFocusWrap and is always
  passed as a boolean: an undefined -> true update is dropped by the base prop
  chain, so the menu policy never applied.
- Add a jsdom CSSOM polyfill for CSS.escape via vitest setupFiles; the
  animationend test no longer raises an unhandled error.
This commit is contained in:
maoyingjie
2026-10-25 00:01:24 +08:00
parent b474a63303
commit 2e4b376537
9 changed files with 223 additions and 20 deletions
+46
View File
@@ -341,3 +341,49 @@ it("standalone Menu keeps its own accessible name", async () => {
document.querySelector('[role="menu"]')?.getAttribute("aria-label"),
).toBe("Standalone actions");
});
it("submenu keeps a directly supplied panel accessible name", async () => {
const panel = await openConfiguredSubmenu({
"aria-label": "Direct submenu name",
"aria-describedby": "explicit-description",
});
expect(panel.getAttribute("aria-label")).toBe("Direct submenu name");
expect(panel.getAttribute("aria-describedby")).toBe("explicit-description");
});
it("submenu preserves a caller's openAutoFocus callback", async () => {
const callback = vi.fn();
await openConfiguredSubmenu({ onOpenAutoFocus: callback });
expect(callback).toHaveBeenCalledTimes(1);
});
it("submenu respects the menu's shouldFocusWrap navigation", async () => {
await render({}, () =>
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "sub" }, () => [
"Sub",
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, {}, () =>
h(DropdownMenu, { shouldFocusWrap: true }, () => [
h(DropdownItem, { id: "first", textValue: "First" }, () => "First"),
h(DropdownItem, { id: "last", textValue: "Last" }, () => "Last"),
]),
),
]),
);
(document.querySelector('[data-key="sub"]') as HTMLElement).click();
await flushPromises();
const panel = document.querySelector("[data-submenu-content]")!;
const first = panel.querySelector('[data-key="first"]') as HTMLElement;
const last = panel.querySelector('[data-key="last"]') as HTMLElement;
first.focus();
await flushPromises();
first.dispatchEvent(
new KeyboardEvent("keydown", {
key: "ArrowUp",
bubbles: true,
cancelable: true,
}),
);
await flushPromises();
expect(document.activeElement).toBe(last);
});
+38
View File
@@ -0,0 +1,38 @@
/**
* jsdom lacks parts of the CSSOM. Reka's Presence uses `CSS.escape` when it
* resolves exiting children, so tests that dispatch real `animationend` events
* need the same capability a browser provides. Only fills the gap; never
* overrides an existing implementation.
*/
type CssEscapeHost = { escape?: (value: string) => string };
const cssGlobal = globalThis as unknown as { CSS?: CssEscapeHost };
if (!cssGlobal.CSS) cssGlobal.CSS = {};
if (typeof cssGlobal.CSS.escape !== "function") {
// Approximation of CSS.escape for selector fragments: hex-escape leading
// digits and any character outside the identifier set.
const escapeIdentifier = (value: string) => {
const input = String(value);
let output = "";
for (let index = 0; index < input.length; index += 1) {
const char = input[index];
const code = char.codePointAt(0) ?? 0;
const isFirst = index === 0;
const digits = code >= 0x30 && code <= 0x39;
const safe =
code >= 0x80 ||
code === 0x2d ||
code === 0x5f ||
(code >= 0x30 && code <= 0x39) ||
(code >= 0x41 && code <= 0x5a) ||
(code >= 0x61 && code <= 0x7a);
if (code === 0) output += "\uFFFD";
else if ((isFirst && digits) || !safe)
output += `\\${code.toString(16)} `;
else output += char;
}
return output;
};
cssGlobal.CSS.escape = escapeIdentifier;
}