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
+100
View File
@@ -241,3 +241,103 @@ it("submenu panel preserves a caller keydown listener", async () => {
await flushPromises();
expect(keydown).toHaveBeenCalledTimes(1);
});
it("default action activation closes its dropdown", async () => {
await render({}, () => h(DropdownItem, { id: "default" }, () => "Action"));
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
expect(document.querySelector('[role="menu"]')).toBeNull();
});
it.each([
["single", false, true],
["multiple", true, false],
] as const)(
"menu-level close policy for %s is honoured",
async (selectionMode, shouldCloseOnSelect, staysOpen) => {
await render({ selectionMode, shouldCloseOnSelect }, () =>
h(DropdownItem, { id: "policy" }, () => "Policy"),
);
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
expect(!!document.querySelector('[role="menu"]')).toBe(staysOpen);
},
);
it("root popover preserves a user keydown callback", async () => {
const callback = vi.fn();
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () =>
h(DropdownPopover, { onKeydown: callback }, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
document.querySelector('[role="menu"]')!.dispatchEvent(
new KeyboardEvent("keydown", {
key: "a",
bubbles: true,
cancelable: true,
}),
);
await flushPromises();
expect(callback).toHaveBeenCalledTimes(1);
});
async function openConfiguredSubmenu(props: Record<string, unknown>) {
await render({}, () =>
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "sub" }, () => [
"Sub",
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, props, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "inner" }, () => "Inner"),
),
),
]),
);
(document.querySelector('[data-key="sub"]') as HTMLElement).click();
await flushPromises();
return document.querySelector("[data-submenu-content]") as HTMLElement;
}
it("submenu preserves the panel's data and style attributes", async () => {
const panel = await openConfiguredSubmenu({
"data-custom": "kept",
style: { borderTopWidth: "7px" },
});
expect(panel.getAttribute("data-custom")).toBe("kept");
expect(panel.style.borderTopWidth).toBe("7px");
});
it("submenu honours isKeyboardDismissDisabled", async () => {
const panel = await openConfiguredSubmenu({
isKeyboardDismissDisabled: true,
});
const item = panel.querySelector('[data-slot="menu-item"]') as HTMLElement;
item.focus();
item.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Escape",
bubbles: true,
cancelable: true,
}),
);
await flushPromises();
expect(document.querySelector("[data-submenu-content]")).not.toBeNull();
});
it("standalone Menu keeps its own accessible name", async () => {
const w = mount(Menu, {
attrs: { "aria-label": "Standalone actions" },
slots: { default: () => h(DropdownItem, { id: "a" }, () => "A") },
attachTo: document.body,
});
cleanup.push(() => w.unmount());
await flushPromises();
expect(
document.querySelector('[role="menu"]')?.getAttribute("aria-label"),
).toBe("Standalone actions");
});
+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();
});