fix: menu selection ARIA, cancelable activation, submenu and long-press

- Selected items use menuitemradio/menuitemcheckbox + aria-checked + data-selected
- onClick preventDefault cancels action/selection/close (user handlers first)
- Submenu trigger is a single menuitem (asChild); standalone Menu owns Reka root
- DropdownRoot trigger=longPress with threshold, keyboard fallback, cleanup
- Export *Props types; independent tarball ESM/CSS + NodeNext/bundler contracts
- Esc still closes the full submenu stack (recorded gap)
This commit is contained in:
maoyingjie
2026-10-24 21:19:04 +08:00
parent 06ddb6662a
commit 1692c6b0ac
16 changed files with 719 additions and 141 deletions
+106
View File
@@ -0,0 +1,106 @@
import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h } from "vue";
import {
DropdownRoot,
DropdownPopover,
DropdownMenu,
DropdownItem,
DropdownItemIndicator,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
Menu,
MenuItem,
} from "../packages/vue/src";
const cleanup: (() => void)[] = [];
afterEach(() => {
cleanup
.splice(0)
.reverse()
.forEach((fn) => fn());
document.body.innerHTML = "";
});
async function renderMenu(
menuProps = {},
itemProps = {},
children = () => "A",
) {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { defaultOpen: true }, () =>
h(DropdownPopover, {}, () =>
h(DropdownMenu, menuProps, () =>
h(
DropdownItem,
{ id: "a", textValue: "A", ...itemProps },
children,
),
),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
return document.querySelector('[data-slot="menu-item"]') as HTMLElement;
}
it("selected multiple item exposes checked menu semantics and source CSS selected state", async () => {
const node = await renderMenu(
{ selectionMode: "multiple", selectedKeys: ["a"] },
{},
() => [h(DropdownItemIndicator, { type: "dot" }), "A"] as never,
);
expect(node.getAttribute("role")).toBe("menuitemcheckbox");
expect(node.getAttribute("aria-checked")).toBe("true");
});
it("preventing click prevents action and default close", async () => {
const action = vi.fn();
const node = await renderMenu(
{ onAction: action },
{ onClick: (e: MouseEvent) => e.preventDefault() },
);
node.click();
await flushPromises();
expect(action).not.toHaveBeenCalled();
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});
it("submenu trigger has one menuitem DOM node rather than nested interactive items", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { defaultOpen: true }, () =>
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "sub", textValue: "Sub" }, () => [
"Sub",
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "child" }, () => "Child"),
),
),
]),
),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(
document.querySelector('[role="menuitem"] [role="menuitem"]'),
).toBeNull();
});
it("standalone exported Menu and MenuItem do not require an internal DropdownMenuRoot", () => {
expect(() => {
const w = mount(Menu, {
slots: { default: () => h(MenuItem, { id: "a" }, () => "A") },
});
cleanup.push(() => w.unmount());
}).not.toThrow();
});