043a5418f3
- menu-item.css keys disabled styling off the presence attribute Reka emits. - Dropdown surfaces drive the ported enter/exit animations and use data-side for placement-specific motion. - One role=menu per popup; items render their key as DOM id; submenu surfaces point aria-labelledby at the real trigger; MenuSection names its group from a nested Header. - Custom (asChild) triggers stay keyboard focusable; library Button/Avatar keep their own layout instead of the custom-trigger rules. - Items emit data-focused/data-hovered/data-pressed, expose the upstream render props to slots, and merge caller classes through tailwind-variants. - Submenu triggers declare hasSubmenu explicitly and forward class/ARIA; the default submenu indicator uses the baseline chevron artwork. - Demos restore Kbd slot="keyboard" (27x), IconSquarePlus, and remove the added comment, stray space and duplicated indicator SVGs. - Document the page after the official structure and API tables, including renamed props, Vue extensions and remaining gaps.
917 lines
27 KiB
TypeScript
917 lines
27 KiB
TypeScript
import { afterEach, expect, it, vi } from "vitest";
|
|
import { mount, flushPromises } from "@vue/test-utils";
|
|
import { defineComponent, h, ref, nextTick } from "vue";
|
|
import {
|
|
Button,
|
|
DropdownRoot,
|
|
DropdownTrigger,
|
|
DropdownPopover,
|
|
DropdownMenu,
|
|
DropdownItem,
|
|
DropdownItemIndicator,
|
|
DropdownSection,
|
|
DropdownSubmenuTrigger,
|
|
DropdownSubmenuIndicator,
|
|
Avatar,
|
|
AvatarFallback,
|
|
Header,
|
|
Label,
|
|
Menu,
|
|
MenuItem,
|
|
MenuSection,
|
|
} from "../packages/vue/src";
|
|
|
|
const cleanup: (() => void)[] = [];
|
|
afterEach(() => {
|
|
cleanup
|
|
.splice(0)
|
|
.reverse()
|
|
.forEach((fn) => fn());
|
|
vi.useRealTimers();
|
|
document.body.innerHTML = "";
|
|
});
|
|
|
|
function mountApp(setup: () => unknown, render?: () => unknown) {
|
|
const w = mount(
|
|
defineComponent(
|
|
render
|
|
? { setup: setup as never, render: render as never }
|
|
: { setup: setup as never },
|
|
),
|
|
{ attachTo: document.body },
|
|
);
|
|
cleanup.push(() => w.unmount());
|
|
return w;
|
|
}
|
|
|
|
it("Dropdown opens and action item fires once", async () => {
|
|
const actions: string[] = [];
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(Button, () => "操作"),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => [
|
|
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
|
|
h(Label, () => "新建文件"),
|
|
),
|
|
h(
|
|
DropdownItem,
|
|
{ id: "delete-file", textValue: "删除文件", variant: "danger" },
|
|
() => h(Label, () => "删除文件"),
|
|
),
|
|
]),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
|
expect(items.length).toBe(2);
|
|
expect((items[1] as HTMLElement).className).toContain("hv-menu-item--danger");
|
|
expect((items[0] as HTMLElement).getAttribute("role")).toBe("menuitem");
|
|
|
|
(items[0] as HTMLElement).click();
|
|
await flushPromises();
|
|
expect(actions).toEqual(["new-file"]);
|
|
});
|
|
|
|
it("Menu multiple selection toggles keys", async () => {
|
|
const selected = ref<string[]>(["apple"]);
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(Button, () => "水果"),
|
|
h(DropdownPopover, {}, () =>
|
|
h(
|
|
DropdownMenu,
|
|
{
|
|
selectionMode: "multiple",
|
|
selectedKeys: selected.value,
|
|
"onUpdate:selectedKeys": (k: (string | number)[]) => {
|
|
selected.value = k as string[];
|
|
},
|
|
},
|
|
() => [
|
|
h(DropdownItem, { id: "apple", textValue: "苹果" }, () => [
|
|
h(DropdownItemIndicator),
|
|
h(Label, () => "苹果"),
|
|
]),
|
|
h(DropdownItem, { id: "banana", textValue: "香蕉" }, () => [
|
|
h(DropdownItemIndicator),
|
|
h(Label, () => "香蕉"),
|
|
]),
|
|
],
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
|
expect(items.length).toBe(2);
|
|
(items[1] as HTMLElement).click();
|
|
await flushPromises();
|
|
await nextTick();
|
|
expect(selected.value).toContain("banana");
|
|
expect(selected.value).toContain("apple");
|
|
});
|
|
|
|
it("MenuSection can own section-level selection", async () => {
|
|
const styles = ref<string[]>(["bold"]);
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(Button, () => "样式"),
|
|
h(DropdownPopover, {}, () =>
|
|
h(Menu, {}, () =>
|
|
h(
|
|
MenuSection,
|
|
{
|
|
selectionMode: "multiple",
|
|
selectedKeys: styles.value,
|
|
"onUpdate:selectedKeys": (k: (string | number)[]) => {
|
|
styles.value = k as string[];
|
|
},
|
|
},
|
|
() => [
|
|
h(MenuItem, { id: "bold", textValue: "粗体" }, () =>
|
|
h(Label, () => "粗体"),
|
|
),
|
|
h(MenuItem, { id: "italic", textValue: "斜体" }, () =>
|
|
h(Label, () => "斜体"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
|
expect(items.length).toBe(2);
|
|
(items[1] as HTMLElement).click();
|
|
await flushPromises();
|
|
await nextTick();
|
|
expect(styles.value).toEqual(expect.arrayContaining(["bold", "italic"]));
|
|
});
|
|
|
|
it("disabled item is marked disabled", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(Button, () => "操作"),
|
|
h(DropdownPopover, {}, () =>
|
|
h(Menu, { disabledKeys: ["x"] }, () =>
|
|
h(MenuItem, { id: "x", textValue: "X" }, () => h(Label, () => "X")),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
|
expect(item).toBeTruthy();
|
|
const disabled =
|
|
item.hasAttribute("disabled") ||
|
|
item.getAttribute("aria-disabled") === "true" ||
|
|
item.getAttribute("data-disabled") === "true" ||
|
|
(item as HTMLInputElement).disabled === true;
|
|
expect(disabled).toBe(true);
|
|
});
|
|
|
|
it("indicator uses checkmark svg and data-visible when selected", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(Button, () => "水果"),
|
|
h(DropdownPopover, {}, () =>
|
|
h(
|
|
DropdownMenu,
|
|
{ selectionMode: "multiple", selectedKeys: ["apple"] },
|
|
() => [
|
|
h(DropdownItem, { id: "apple", textValue: "苹果" }, () => [
|
|
h(DropdownItemIndicator),
|
|
h(Label, () => "苹果"),
|
|
]),
|
|
],
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const ind = document.querySelector('[data-slot="menu-item-indicator"]');
|
|
expect(ind).toBeTruthy();
|
|
expect(ind!.getAttribute("data-type")).toBe("checkmark");
|
|
expect(
|
|
ind!.querySelector('[data-slot="menu-item-indicator--checkmark"]'),
|
|
).not.toBeNull();
|
|
});
|
|
|
|
it("submenu trigger opens nested items", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(Button, () => "分享"),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () => [
|
|
h(DropdownItem, { id: "copy-link", textValue: "复制链接" }, () =>
|
|
h(Label, () => "复制链接"),
|
|
),
|
|
h(DropdownSubmenuTrigger, {}, () => [
|
|
h(DropdownItem, { id: "share", textValue: "分享" }, () => [
|
|
h(Label, () => "其他"),
|
|
h(DropdownSubmenuIndicator),
|
|
]),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(
|
|
DropdownItem,
|
|
{ id: "whatsapp", textValue: "WhatsApp" },
|
|
() => h(Label, () => "WhatsApp"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
]),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const triggers = document.querySelectorAll(
|
|
'[data-slot="menu-item"][data-has-submenu="true"], [data-slot="dropdown-submenu-trigger"]',
|
|
);
|
|
expect(triggers.length).toBeGreaterThanOrEqual(1);
|
|
// Open submenu via pointer (Reka SubTrigger)
|
|
(triggers[0] as HTMLElement).dispatchEvent(
|
|
new MouseEvent("pointerenter", { bubbles: true }),
|
|
);
|
|
(triggers[0] as HTMLElement).click();
|
|
await flushPromises();
|
|
await nextTick();
|
|
const html = document.body.innerHTML;
|
|
expect(html).toContain("WhatsApp");
|
|
expect(html).toContain("其他");
|
|
});
|
|
|
|
it("long-press trigger opens on hold and suppresses click", async () => {
|
|
vi.useFakeTimers();
|
|
const w = mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
|
|
h(DropdownTrigger, {}, () =>
|
|
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(DropdownTrigger, {}, () => 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();
|
|
});
|
|
|
|
function pointer(
|
|
type: string,
|
|
props: Record<string, unknown> = {},
|
|
): MouseEvent {
|
|
const event = new MouseEvent(type, {
|
|
bubbles: true,
|
|
cancelable: true,
|
|
...(props as MouseEventInit),
|
|
});
|
|
for (const key of ["pointerType", "isPrimary"]) {
|
|
if (key in props) {
|
|
Object.defineProperty(event, key, { value: props[key] });
|
|
}
|
|
}
|
|
return event;
|
|
}
|
|
|
|
function mountLongPress(duration = 300) {
|
|
const actions: string[] = [];
|
|
const w = mountApp(
|
|
() => () =>
|
|
h(
|
|
DropdownRoot,
|
|
{ trigger: "longPress", longPressDuration: duration },
|
|
() => [
|
|
h(DropdownTrigger, {}, () =>
|
|
h(Button, { "aria-label": "菜单" }, () => "长按"),
|
|
),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
|
h(DropdownItem, { id: "x", textValue: "X" }, () =>
|
|
h(Label, () => "X"),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
return { w, actions, button: () => w.get("button").element as HTMLElement };
|
|
}
|
|
|
|
function menuState() {
|
|
return (
|
|
document
|
|
.querySelector('[data-slot="dropdown-popover"]')
|
|
?.getAttribute("data-state") ?? null
|
|
);
|
|
}
|
|
|
|
it("longPress: short click on the real trigger does not open", async () => {
|
|
vi.useFakeTimers();
|
|
const { button } = mountLongPress();
|
|
await flushPromises();
|
|
button().click();
|
|
await flushPromises();
|
|
await vi.advanceTimersByTimeAsync(600);
|
|
await nextTick();
|
|
expect(menuState()).toBeNull();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("longPress: opens at the threshold and stays open after release", async () => {
|
|
vi.useFakeTimers();
|
|
const { button } = mountLongPress();
|
|
await flushPromises();
|
|
const el = button();
|
|
el.dispatchEvent(pointer("pointerdown", { button: 0, pointerType: "mouse" }));
|
|
await vi.advanceTimersByTimeAsync(200);
|
|
await nextTick();
|
|
expect(menuState()).toBeNull();
|
|
await vi.advanceTimersByTimeAsync(150);
|
|
await nextTick();
|
|
expect(menuState()).toBe("open");
|
|
el.dispatchEvent(pointer("pointerup", { button: 0, pointerType: "mouse" }));
|
|
el.click();
|
|
await flushPromises();
|
|
await nextTick();
|
|
expect(menuState()).toBe("open");
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("longPress: pointercancel and out-of-range movement cancel the pending open", async () => {
|
|
vi.useFakeTimers();
|
|
const { button } = mountLongPress();
|
|
await flushPromises();
|
|
const el = button();
|
|
el.dispatchEvent(pointer("pointerdown", { button: 0, pointerType: "mouse" }));
|
|
el.dispatchEvent(pointer("pointermove", { clientX: 40, clientY: 0 }));
|
|
await vi.advanceTimersByTimeAsync(400);
|
|
await nextTick();
|
|
expect(menuState()).toBeNull();
|
|
|
|
el.dispatchEvent(
|
|
pointer("pointerdown", { button: 0, clientX: 0, clientY: 0 }),
|
|
);
|
|
el.dispatchEvent(pointer("pointercancel", {}));
|
|
await vi.advanceTimersByTimeAsync(400);
|
|
await nextTick();
|
|
expect(menuState()).toBeNull();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("longPress: non-primary button and non-primary pointer are ignored", async () => {
|
|
vi.useFakeTimers();
|
|
const { button } = mountLongPress();
|
|
await flushPromises();
|
|
const el = button();
|
|
el.dispatchEvent(pointer("pointerdown", { button: 2, pointerType: "mouse" }));
|
|
await vi.advanceTimersByTimeAsync(400);
|
|
await nextTick();
|
|
expect(menuState()).toBeNull();
|
|
|
|
el.dispatchEvent(pointer("pointerdown", { button: 0, isPrimary: false }));
|
|
await vi.advanceTimersByTimeAsync(400);
|
|
await nextTick();
|
|
expect(menuState()).toBeNull();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("longPress: Enter and Space open once from the keyboard", async () => {
|
|
const { w } = mountLongPress();
|
|
await flushPromises();
|
|
await w.get("button").trigger("keydown", { key: "Enter" });
|
|
await flushPromises();
|
|
expect(menuState()).toBe("open");
|
|
|
|
await w.get("button").trigger("keydown", { key: " " });
|
|
await flushPromises();
|
|
expect(menuState()).toBeNull();
|
|
|
|
await w.get("button").trigger("keydown", { key: " " });
|
|
await flushPromises();
|
|
expect(menuState()).toBe("open");
|
|
});
|
|
|
|
it("longPress: unmount clears the pending long-press timer", async () => {
|
|
vi.useFakeTimers();
|
|
const { w } = mountLongPress();
|
|
await flushPromises();
|
|
(w.get("button").element as HTMLElement).dispatchEvent(
|
|
pointer("pointerdown", { button: 0 }),
|
|
);
|
|
expect(vi.getTimerCount()).toBeGreaterThan(0);
|
|
w.unmount();
|
|
cleanup.pop();
|
|
await nextTick();
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
function mountSubmenu() {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () => [
|
|
h(DropdownItem, { id: "copy-link", textValue: "复制链接" }, () =>
|
|
h(Label, () => "复制链接"),
|
|
),
|
|
h(DropdownSubmenuTrigger, {}, () => [
|
|
h(DropdownItem, { id: "share", textValue: "分享" }, () => [
|
|
h(Label, () => "分享"),
|
|
h(DropdownSubmenuIndicator),
|
|
]),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(
|
|
DropdownItem,
|
|
{ id: "whatsapp", textValue: "WhatsApp" },
|
|
() => h(Label, () => "WhatsApp"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
]),
|
|
),
|
|
]),
|
|
);
|
|
}
|
|
|
|
function escapeKey(node: Element) {
|
|
return node.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Escape",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
}
|
|
|
|
async function settle() {
|
|
await flushPromises();
|
|
await nextTick();
|
|
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
await flushPromises();
|
|
}
|
|
|
|
async function openSubmenu() {
|
|
const subTrigger = document.querySelector(
|
|
'[data-slot="menu-item"][data-has-submenu="true"]',
|
|
) as HTMLElement;
|
|
expect(subTrigger).toBeTruthy();
|
|
subTrigger.click();
|
|
await flushPromises();
|
|
await nextTick();
|
|
const subContent = document.querySelector(
|
|
"[data-submenu-content]",
|
|
) as HTMLElement;
|
|
expect(subContent).toBeTruthy();
|
|
return { subTrigger, subContent };
|
|
}
|
|
|
|
it("Escape closes the submenu level first, then the root menu", async () => {
|
|
mountSubmenu();
|
|
await flushPromises();
|
|
await nextTick();
|
|
const { subTrigger, subContent } = await openSubmenu();
|
|
const child = subContent.querySelector(
|
|
'[data-slot="menu-item"]',
|
|
) as HTMLElement;
|
|
child.focus();
|
|
expect(document.activeElement).toBe(child);
|
|
|
|
escapeKey(child);
|
|
await settle();
|
|
expect(document.querySelector("[data-submenu-content]")).toBeNull();
|
|
expect(
|
|
document.querySelectorAll('[data-slot="dropdown-popover"]').length,
|
|
).toBe(1);
|
|
expect(document.activeElement).toBe(subTrigger);
|
|
|
|
escapeKey(subTrigger);
|
|
await settle();
|
|
expect(document.querySelector('[data-slot="dropdown-popover"]')).toBeNull();
|
|
});
|
|
|
|
it("Escape on a pointer-opened submenu trigger closes only that level", async () => {
|
|
mountSubmenu();
|
|
await flushPromises();
|
|
await nextTick();
|
|
const { subTrigger } = await openSubmenu();
|
|
// Pointer opening leaves focus on the trigger, outside the sub content.
|
|
subTrigger.focus();
|
|
expect(document.activeElement).toBe(subTrigger);
|
|
|
|
escapeKey(subTrigger);
|
|
await settle();
|
|
expect(document.querySelector("[data-submenu-content]")).toBeNull();
|
|
expect(
|
|
document.querySelectorAll('[data-slot="dropdown-popover"]').length,
|
|
).toBe(1);
|
|
expect(document.activeElement).toBe(subTrigger);
|
|
|
|
escapeKey(subTrigger);
|
|
await settle();
|
|
expect(document.querySelector('[data-slot="dropdown-popover"]')).toBeNull();
|
|
});
|
|
|
|
it("keyboard activation on a focused item fires the action once", async () => {
|
|
const actions: string[] = [];
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
|
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
|
|
h(Label, () => "新建文件"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
|
item.focus();
|
|
expect(document.activeElement).toBe(item);
|
|
item.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Enter",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
expect(actions).toEqual(["new-file"]);
|
|
});
|
|
|
|
it("keyboard activation is cancelled when the item click is prevented", async () => {
|
|
const actions: string[] = [];
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
|
h(
|
|
DropdownItem,
|
|
{
|
|
id: "new-file",
|
|
textValue: "新建文件",
|
|
onClick: (e: MouseEvent) => e.preventDefault(),
|
|
},
|
|
() => h(Label, () => "新建文件"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
|
item.focus();
|
|
item.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Enter",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
expect(actions).toEqual([]);
|
|
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
|
|
});
|
|
|
|
it("the 17 baseline previews all mount without console warnings", async () => {
|
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
const demos = import.meta.glob("../apps/docs/demos/dropdown-*.vue", {
|
|
eager: true,
|
|
import: "default",
|
|
});
|
|
const names = Object.keys(demos).map((path) =>
|
|
path.replace("../apps/docs/demos/", "").replace(".vue", ""),
|
|
);
|
|
expect(names).toHaveLength(17);
|
|
try {
|
|
for (const name of names) {
|
|
const w = mount(demos[`../apps/docs/demos/${name}.vue`] as never, {
|
|
attachTo: document.body,
|
|
});
|
|
await flushPromises();
|
|
w.unmount();
|
|
await flushPromises();
|
|
}
|
|
expect(error.mock.calls).toEqual([]);
|
|
expect(warn.mock.calls).toEqual([]);
|
|
} finally {
|
|
warn.mockRestore();
|
|
error.mockRestore();
|
|
document.body.innerHTML = "";
|
|
}
|
|
});
|
|
|
|
it("menu items expose DOM ids and a single role=menu per popup", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () => [
|
|
h(DropdownItem, { id: "first", textValue: "一" }, () =>
|
|
h(Label, () => "一"),
|
|
),
|
|
h(DropdownItem, { id: "second", textValue: "二" }, () =>
|
|
h(Label, () => "二"),
|
|
),
|
|
]),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
|
expect(Array.from(items).map((item) => item.getAttribute("id"))).toEqual([
|
|
"first",
|
|
"second",
|
|
]);
|
|
expect(document.querySelectorAll('[role="menu"]')).toHaveLength(1);
|
|
});
|
|
|
|
it("disabled items match the ported CSS presence selector", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, { disabledKeys: ["x"] }, () =>
|
|
h(DropdownItem, { id: "x", textValue: "X" }, () =>
|
|
h(Label, () => "X"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
|
// `menu-item.css` styles `&[data-disabled]`, so presence is the contract.
|
|
expect(item.matches("[data-disabled]")).toBe(true);
|
|
});
|
|
|
|
it("submenu surface keeps a resolvable accessible name", async () => {
|
|
mountSubmenu();
|
|
await flushPromises();
|
|
await nextTick();
|
|
const { subContent } = await openSubmenu();
|
|
await flushPromises();
|
|
const labelledBy = subContent.getAttribute("aria-labelledby");
|
|
expect(labelledBy).toBeTruthy();
|
|
expect(document.getElementById(labelledBy as string)).not.toBeNull();
|
|
});
|
|
|
|
it("Section names its group from a nested Header", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(DropdownSection, {}, () => [
|
|
h(Header, {}, () => "操作"),
|
|
h(DropdownItem, { id: "a", textValue: "A" }, () =>
|
|
h(Label, () => "A"),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const section = document.querySelector(
|
|
'[data-slot="menu-section"]',
|
|
) as HTMLElement;
|
|
const labelledBy = section.getAttribute("aria-labelledby");
|
|
expect(labelledBy).toBeTruthy();
|
|
const header = document.getElementById(labelledBy as string);
|
|
expect(header?.textContent).toBe("操作");
|
|
});
|
|
|
|
it("a non-button custom trigger stays keyboard focusable", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, {}, () => [
|
|
h(DropdownTrigger, {}, () =>
|
|
h(Avatar, {}, () => h(AvatarFallback, {}, () => "JD")),
|
|
),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(DropdownItem, { id: "a", textValue: "A" }, () =>
|
|
h(Label, () => "A"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const trigger = document.querySelector(
|
|
'[data-slot="dropdown-trigger"]',
|
|
) as HTMLElement;
|
|
expect(trigger.tagName).toBe("SPAN");
|
|
expect(trigger.getAttribute("role")).toBe("button");
|
|
expect(trigger.getAttribute("tabindex")).toBe("0");
|
|
});
|
|
|
|
it("item and indicator slots expose the upstream render state", async () => {
|
|
const seen: Record<string, unknown>[] = [];
|
|
const indicatorState: Record<string, unknown>[] = [];
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(
|
|
DropdownMenu,
|
|
{ selectionMode: "single", selectedKeys: ["a"] },
|
|
{
|
|
default: () =>
|
|
h(
|
|
DropdownItem,
|
|
{ id: "a", textValue: "A" },
|
|
{
|
|
default: (state: Record<string, unknown>) => {
|
|
seen.push(state);
|
|
return [
|
|
h(
|
|
DropdownItemIndicator,
|
|
{},
|
|
{
|
|
default: (indicator: Record<string, unknown>) => {
|
|
indicatorState.push(indicator);
|
|
return null;
|
|
},
|
|
},
|
|
),
|
|
h(Label, () => "A"),
|
|
];
|
|
},
|
|
},
|
|
),
|
|
},
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
expect(seen[0]).toMatchObject({
|
|
isSelected: true,
|
|
isDisabled: false,
|
|
isPressed: false,
|
|
});
|
|
expect(seen[0]).toHaveProperty("isFocused");
|
|
expect(indicatorState[0]).toMatchObject({
|
|
isSelected: true,
|
|
isIndeterminate: false,
|
|
});
|
|
});
|
|
|
|
it("caller classes merge with tailwind-variants semantics", async () => {
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(DropdownItem, { id: "a", textValue: "A", class: "p-0" }, () =>
|
|
h(Label, () => "A"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
|
expect(item.className).toContain("hv-menu-item");
|
|
expect(item.className).toContain("p-0");
|
|
});
|
|
|
|
it("pressing the submenu trigger without an indicator still treats it as a submenu", async () => {
|
|
const actions: string[] = [];
|
|
mountApp(
|
|
() => () =>
|
|
h(DropdownRoot, { defaultOpen: true }, () => [
|
|
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
|
h(DropdownPopover, {}, () =>
|
|
h(
|
|
DropdownMenu,
|
|
{ onAction: (key: string) => actions.push(key) },
|
|
() =>
|
|
h(DropdownSubmenuTrigger, {}, () => [
|
|
h(DropdownItem, { id: "more", textValue: "更多" }, () =>
|
|
h(Label, () => "更多"),
|
|
),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(DropdownItem, { id: "child", textValue: "子项" }, () =>
|
|
h(Label, () => "子项"),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
await flushPromises();
|
|
await nextTick();
|
|
const trigger = document.querySelector(
|
|
'[data-slot="menu-item"][data-has-submenu="true"]',
|
|
) as HTMLElement;
|
|
expect(trigger).not.toBeNull();
|
|
trigger.click();
|
|
await flushPromises();
|
|
await nextTick();
|
|
// A submenu trigger must not fire the menu action nor close the root menu.
|
|
expect(actions).toEqual([]);
|
|
expect(document.querySelector("[data-submenu-content]")).not.toBeNull();
|
|
expect(
|
|
document.querySelectorAll('[data-slot="dropdown-popover"]').length,
|
|
).toBeGreaterThan(1);
|
|
});
|