fix: align Dropdown/Menu with the HeroUI baseline after fidelity audit

- 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.
This commit is contained in:
maoyingjie
2026-10-24 22:14:10 +08:00
parent 64aaf5e53f
commit 043a5418f3
27 changed files with 1124 additions and 254 deletions
+264
View File
@@ -12,6 +12,9 @@ import {
DropdownSection,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
Avatar,
AvatarFallback,
Header,
Label,
Menu,
MenuItem,
@@ -650,3 +653,264 @@ it("keyboard activation is cancelled when the item click is prevented", async ()
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);
});