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
+68 -5
View File
@@ -238,9 +238,9 @@ it("submenu trigger opens nested items", async () => {
await flushPromises();
await nextTick();
const triggers = document.querySelectorAll(
'[data-slot="dropdown-submenu-trigger"]',
'[data-slot="menu-item"][data-has-submenu="true"], [data-slot="dropdown-submenu-trigger"]',
);
expect(triggers.length).toBe(1);
expect(triggers.length).toBeGreaterThanOrEqual(1);
// Open submenu via pointer (Reka SubTrigger)
(triggers[0] as HTMLElement).dispatchEvent(
new MouseEvent("pointerenter", { bubbles: true }),
@@ -251,7 +251,70 @@ it("submenu trigger opens nested items", async () => {
const html = document.body.innerHTML;
expect(html).toContain("WhatsApp");
expect(html).toContain("其他");
expect(
document.querySelector('[data-slot="dropdown-submenu-trigger"]'),
).toBeTruthy();
});
it("long-press trigger opens on hold and suppresses click", async () => {
vi.useFakeTimers();
const w = mountApp(
() => () =>
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
h(Button, { "aria-label": "菜单" }, () => "长按"),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "x", textValue: "X" }, () =>
h(Label, () => "X"),
),
),
),
]),
);
await flushPromises();
const btn = w.get("button");
btn.trigger("pointerdown", { button: 0, clientX: 10, clientY: 10 });
await vi.advanceTimersByTimeAsync(350);
await nextTick();
expect(
document
.querySelector('[data-slot="dropdown-popover"]')
?.getAttribute("data-state"),
).toBe("open");
vi.useRealTimers();
});
it("long-press cancel on move threshold", async () => {
vi.useFakeTimers();
mountApp(
() => () =>
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
h(Button, () => "长按"),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "x", textValue: "X" }, () =>
h(Label, () => "X"),
),
),
),
]),
);
await flushPromises();
const btn = document.querySelector("button") as HTMLElement;
btn.dispatchEvent(
new MouseEvent("pointerdown", {
bubbles: true,
button: 0,
clientX: 0,
clientY: 0,
}),
);
btn.dispatchEvent(
new MouseEvent("pointermove", { bubbles: true, clientX: 40, clientY: 0 }),
);
await vi.advanceTimersByTimeAsync(400);
await nextTick();
expect(
document
.querySelector('[data-slot="dropdown-popover"]')
?.getAttribute("data-state"),
).not.toBe("open");
vi.useRealTimers();
});
+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();
});
+105
View File
@@ -0,0 +1,105 @@
import {
DropdownRoot,
DropdownTrigger,
DropdownPopover,
DropdownMenu,
DropdownSection,
DropdownItem,
DropdownItemIndicator,
DropdownSubmenuIndicator,
DropdownSubmenuTrigger,
Menu,
MenuItem,
MenuItemIndicator,
MenuItemSubmenuIndicator,
MenuSection,
type DropdownRootProps,
type DropdownMenuProps,
type DropdownItemProps,
type DropdownItemIndicatorProps,
type MenuRootProps,
type MenuItemProps,
type MenuItemIndicatorProps,
type MenuSectionProps,
type MenuKey,
type MenuSelectionMode,
} from "@heroui-vue/components";
const mode: MenuSelectionMode = "multiple";
const keys: MenuKey[] = ["a", 0, ""];
const root: DropdownRootProps = {
trigger: "longPress",
longPressDuration: 400,
modal: true,
dir: "rtl",
};
// @ts-expect-error unknown trigger mode
const badTrigger: DropdownRootProps = { trigger: "hover" };
const menu: MenuRootProps = {
selectionMode: mode,
selectedKeys: keys,
disabledKeys: ["b"],
};
// @ts-expect-error selectionMode must be none|single|multiple
const badMode: MenuRootProps = { selectionMode: "multiple2" };
const item: MenuItemProps = {
id: "a",
textValue: "A",
variant: "danger",
isDisabled: true,
closeOnSelect: false,
};
// @ts-expect-error variant is default|danger only
const badVariant: MenuItemProps = { variant: "primary" };
const indicator: MenuItemIndicatorProps = { type: "dot" };
// @ts-expect-error type is checkmark|dot
const badIndicator: MenuItemIndicatorProps = { type: "cross" };
const section: MenuSectionProps = {
selectionMode: "single",
selectedKeys: ["x"],
};
const dItem: DropdownItemProps = {
id: "x",
textValue: "X",
variant: "default",
};
const dInd: DropdownItemIndicatorProps = { type: "checkmark" };
const dMenu: DropdownMenuProps = { selectionMode: "none" };
export const parts = [
DropdownRoot,
DropdownTrigger,
DropdownPopover,
DropdownMenu,
DropdownSection,
DropdownItem,
DropdownItemIndicator,
DropdownSubmenuIndicator,
DropdownSubmenuTrigger,
Menu,
MenuItem,
MenuItemIndicator,
MenuItemSubmenuIndicator,
MenuSection,
] as const;
export const values = {
root,
menu,
item,
indicator,
section,
dItem,
dInd,
dMenu,
badTrigger,
badMode,
badVariant,
badIndicator,
};
@@ -0,0 +1,63 @@
/**
* Independent ESM/CSS consumption for B01 Menu/Dropdown.
* Copy into a fresh consumer that installs packed tarballs (no source alias).
*/
import { readFileSync } from "node:fs";
const mod = await import("@heroui-vue/components");
const {
DropdownRoot,
DropdownTrigger,
DropdownPopover,
DropdownMenu,
DropdownItem,
DropdownItemIndicator,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
DropdownSection,
Menu,
MenuItem,
MenuItemIndicator,
MenuItemSubmenuIndicator,
MenuSection,
} = mod;
const expected = {
DropdownRoot,
DropdownTrigger,
DropdownPopover,
DropdownMenu,
DropdownItem,
DropdownItemIndicator,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
DropdownSection,
Menu,
MenuItem,
MenuItemIndicator,
MenuItemSubmenuIndicator,
MenuSection,
};
for (const [name, value] of Object.entries(expected)) {
if (!value) {
console.error("missing export", name);
process.exit(1);
}
}
const cssPath = import.meta.resolve("@heroui-vue/styles/css");
const css = readFileSync(new URL(cssPath), "utf8");
for (const token of [
"hv-menu-item",
"hv-menu-item__indicator--submenu",
"hv-dropdown__popover",
"hv-dropdown__menu",
"data-selected",
]) {
if (!css.includes(token)) {
console.error("css missing", token);
process.exit(1);
}
}
console.log("b01 menu/dropdown ESM+CSS consume ok");