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");
});