fix: resolve close policy, handler merging and submenu panel props

- Menu forwards the raw close policy and the item resolves item > menu
  (boolean or function) > per-mode default, so action items close by default and
  an explicit single/multiple policy is no longer pre-empted.
- Root popover chains caller handlers with the internal ones for keydown,
  escape, interact-outside, auto focus and animation end; callers run first and
  a prevented event stops the internal logic.
- Submenu panels receive the nested popover's data/style/id/extra events, honour
  isKeyboardDismissDisabled without closing the parent level or a modal, and
  forward shouldCloseOnInteractOutside.
- A standalone Menu provides its own content-attribute channel so the
  accessible name/description land on the real role=menu node.
This commit is contained in:
maoyingjie
2026-10-24 23:42:05 +08:00
parent 53b4639b0e
commit b474a63303
12 changed files with 465 additions and 54 deletions
+160
View File
@@ -1156,3 +1156,163 @@ it("standalone Menu stays in flow without modal side effects", async () => {
// Focus is not stolen from the page.
expect(document.activeElement).not.toBe(surface);
});
it("root popover merges caller handlers for escape, outside, focus and animation", async () => {
const calls = { focus: 0, escape: 0, outside: 0, animation: 0, keydown: 0 };
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
h(
DropdownPopover,
{
onOpenAutoFocus: () => {
calls.focus += 1;
},
onEscapeKeyDown: () => {
calls.escape += 1;
},
onInteractOutside: () => {
calls.outside += 1;
},
onAnimationend: () => {
calls.animation += 1;
},
onKeydown: () => {
calls.keydown += 1;
},
},
() =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "a", textValue: "A" }, () =>
h(Label, () => "A"),
),
),
),
]),
);
await flushPromises();
await nextTick();
const menu = document.querySelector('[role="menu"]') as HTMLElement;
menu.dispatchEvent(
new KeyboardEvent("keydown", { key: "a", bubbles: true, cancelable: true }),
);
menu.dispatchEvent(new Event("animationend", { bubbles: true }));
await flushPromises();
expect(calls.keydown).toBe(1);
expect(calls.animation).toBe(1);
expect(calls.focus).toBe(1);
});
it("a cancelled caller keydown keeps the menu open for clearSelection menus", async () => {
const selections: MenuKey[][] = [];
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
h(
DropdownPopover,
{
onKeydown: (event: KeyboardEvent) => {
if (event.key === "Escape") event.preventDefault();
},
},
() =>
h(
DropdownMenu,
{
selectionMode: "multiple",
selectedKeys: ["a"],
escapeKeyBehavior: "clearSelection",
"onUpdate:selectedKeys": (keys: MenuKey[]) =>
selections.push(keys),
},
() =>
h(DropdownItem, { id: "a", textValue: "A" }, () =>
h(Label, () => "A"),
),
),
),
]),
);
await flushPromises();
await nextTick();
const menu = document.querySelector('[role="menu"]') as HTMLElement;
menu.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Escape",
bubbles: true,
cancelable: true,
}),
);
await flushPromises();
// The caller cancelled the escape, so the internal clear did not run.
expect(selections).toEqual([]);
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});
it("submenu keyboard dismissal can be disabled without closing the parent", async () => {
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "sub", textValue: "S" }, () => [
h(Label, () => "S"),
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, { isKeyboardDismissDisabled: true }, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "inner", textValue: "I" }, () =>
h(Label, () => "I"),
),
),
),
]),
),
),
]),
);
await flushPromises();
await nextTick();
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
await nextTick();
const panel = document.querySelector("[data-submenu-content]") as HTMLElement;
const item = panel.querySelector('[data-slot="menu-item"]') as HTMLElement;
item.focus();
item.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Escape",
bubbles: true,
cancelable: true,
}),
);
await settle();
expect(document.querySelector("[data-submenu-content]")).not.toBeNull();
expect(
document.querySelectorAll('[data-slot="dropdown-popover"]').length,
).toBeGreaterThan(1);
});
it("menu-level close policy wins over the section mode default", async () => {
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
h(DropdownPopover, {}, () =>
h(DropdownMenu, { shouldCloseOnSelect: false }, () =>
h(DropdownSection, { selectionMode: "single" }, () =>
h(DropdownItem, { id: "s", textValue: "S" }, () =>
h(Label, () => "S"),
),
),
),
),
]),
);
await flushPromises();
await nextTick();
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
await nextTick();
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});