fix: portal dropdown surfaces and close the review capability gaps
- Root and submenu surfaces render through the dropdown portal with the overlay z-index, so clipping/isolation ancestors no longer cut the menu; panel height is capped by the floating available height and the animation origin maps the real transform origin. - Standalone Menu is a static in-flow list: no scroll lock, no outside pointer blocking, no floating placement and no focus stealing. - Selection ownership is isolated per level: section disabled keys add to the menu list, and a nested Menu resets the section owner. - closeOnSelect resolves item > menu policy > per-mode default for selection items; actions and radio items close, checkboxes stay open. - Business keys no longer become DOM ids (instance ids + data-key), and submenu surfaces label themselves from the real trigger. - Long-press keydown runs the caller's handlers first and only opens when not cancelled; submenu panels merge caller keydown with the internal Escape. - Link items render a real anchor, aria labels land on the role=menu node, submenu panels forward placement/offset/collision props and caller attrs, and the popover provides the default Surface variant. - Wrapper parts declare real props/emits, plus the missing official capability props (disallowEmptySelection, escapeKeyBehavior, shouldFocusWrap, autoFocus, shouldCloseOnSelect, close, empty slot, isKeyboardDismissDisabled, shouldCloseOnInteractOutside, placement/offset family, scoped slots). - Ship an independent consumer fixture for browser-level verification.
This commit is contained in:
+254
-12
@@ -20,6 +20,7 @@ import {
|
||||
MenuItem,
|
||||
MenuSection,
|
||||
} from "../packages/vue/src";
|
||||
import type { MenuKey } from "../packages/vue/src";
|
||||
|
||||
const cleanup: (() => void)[] = [];
|
||||
afterEach(() => {
|
||||
@@ -45,13 +46,13 @@ function mountApp(setup: () => unknown, render?: () => unknown) {
|
||||
}
|
||||
|
||||
it("Dropdown opens and action item fires once", async () => {
|
||||
const actions: string[] = [];
|
||||
const actions: MenuKey[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "操作"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => [
|
||||
h(DropdownMenu, { onAction: (k: MenuKey) => actions.push(k) }, () => [
|
||||
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
|
||||
h(Label, () => "新建文件"),
|
||||
),
|
||||
@@ -343,7 +344,7 @@ function pointer(
|
||||
}
|
||||
|
||||
function mountLongPress(duration = 300) {
|
||||
const actions: string[] = [];
|
||||
const actions: MenuKey[] = [];
|
||||
const w = mountApp(
|
||||
() => () =>
|
||||
h(
|
||||
@@ -354,7 +355,7 @@ function mountLongPress(duration = 300) {
|
||||
h(Button, { "aria-label": "菜单" }, () => "长按"),
|
||||
),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
||||
h(DropdownMenu, { onAction: (k: MenuKey) => actions.push(k) }, () =>
|
||||
h(DropdownItem, { id: "x", textValue: "X" }, () =>
|
||||
h(Label, () => "X"),
|
||||
),
|
||||
@@ -585,13 +586,13 @@ it("Escape on a pointer-opened submenu trigger closes only that level", async ()
|
||||
});
|
||||
|
||||
it("keyboard activation on a focused item fires the action once", async () => {
|
||||
const actions: string[] = [];
|
||||
const actions: MenuKey[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
||||
h(DropdownMenu, { onAction: (k: MenuKey) => actions.push(k) }, () =>
|
||||
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
|
||||
h(Label, () => "新建文件"),
|
||||
),
|
||||
@@ -617,13 +618,13 @@ it("keyboard activation on a focused item fires the action once", async () => {
|
||||
});
|
||||
|
||||
it("keyboard activation is cancelled when the item click is prevented", async () => {
|
||||
const actions: string[] = [];
|
||||
const actions: MenuKey[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
||||
h(DropdownMenu, { onAction: (k: MenuKey) => actions.push(k) }, () =>
|
||||
h(
|
||||
DropdownItem,
|
||||
{
|
||||
@@ -702,8 +703,12 @@ it("menu items expose DOM ids and a single role=menu per popup", async () => {
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
||||
expect(Array.from(items).map((item) => item.getAttribute("id"))).toEqual([
|
||||
const items = Array.from(
|
||||
document.querySelectorAll('[data-slot="menu-item"]'),
|
||||
);
|
||||
// Business keys stay out of the DOM; each instance gets its own id.
|
||||
expect(new Set(items.map((item) => item.id)).size).toBe(items.length);
|
||||
expect(items.map((item) => item.getAttribute("data-key"))).toEqual([
|
||||
"first",
|
||||
"second",
|
||||
]);
|
||||
@@ -872,7 +877,7 @@ it("caller classes merge with tailwind-variants semantics", async () => {
|
||||
});
|
||||
|
||||
it("pressing the submenu trigger without an indicator still treats it as a submenu", async () => {
|
||||
const actions: string[] = [];
|
||||
const actions: MenuKey[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
@@ -880,7 +885,7 @@ it("pressing the submenu trigger without an indicator still treats it as a subme
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(
|
||||
DropdownMenu,
|
||||
{ onAction: (key: string) => actions.push(key) },
|
||||
{ onAction: (key: MenuKey) => actions.push(key) },
|
||||
() =>
|
||||
h(DropdownSubmenuTrigger, {}, () => [
|
||||
h(DropdownItem, { id: "more", textValue: "更多" }, () =>
|
||||
@@ -914,3 +919,240 @@ it("pressing the submenu trigger without an indicator still treats it as a subme
|
||||
document.querySelectorAll('[data-slot="dropdown-popover"]').length,
|
||||
).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
it("menu capability props are honoured", async () => {
|
||||
const close = vi.fn();
|
||||
const selections: MenuKey[][] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(
|
||||
DropdownMenu,
|
||||
{
|
||||
selectionMode: "multiple",
|
||||
selectedKeys: ["a"],
|
||||
disallowEmptySelection: true,
|
||||
escapeKeyBehavior: "clearSelection",
|
||||
shouldFocusWrap: true,
|
||||
autoFocus: false,
|
||||
onClose: close,
|
||||
"onUpdate:selectedKeys": (keys: MenuKey[]) =>
|
||||
selections.push(keys),
|
||||
},
|
||||
() =>
|
||||
h(DropdownItem, { id: "a", textValue: "A" }, () =>
|
||||
h(Label, () => "A"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const menu = document.querySelector('[role="menu"]') as HTMLElement;
|
||||
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||
|
||||
// disallowEmptySelection keeps the last key.
|
||||
item.click();
|
||||
await flushPromises();
|
||||
expect(selections).toEqual([]);
|
||||
|
||||
// escapeKeyBehavior clears the selection instead of closing.
|
||||
menu.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "Escape",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await flushPromises();
|
||||
expect(selections).toEqual([]);
|
||||
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
|
||||
|
||||
// close fires when the level actually closes.
|
||||
menu.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
|
||||
await flushPromises();
|
||||
});
|
||||
|
||||
it("link items render an anchor and keep navigation semantics", async () => {
|
||||
const actions: MenuKey[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(
|
||||
DropdownMenu,
|
||||
{ onAction: (key: MenuKey) => actions.push(key) },
|
||||
() =>
|
||||
h(
|
||||
DropdownItem,
|
||||
{
|
||||
id: "link",
|
||||
href: "/target",
|
||||
target: "_blank",
|
||||
rel: "noopener",
|
||||
},
|
||||
() => h(Label, () => "Go"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const link = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||
expect(link.tagName).toBe("A");
|
||||
expect(link.getAttribute("href")).toBe("/target");
|
||||
expect(link.getAttribute("target")).toBe("_blank");
|
||||
expect(link.getAttribute("rel")).toBe("noopener");
|
||||
expect(link.getAttribute("role")).toBe("menuitem");
|
||||
link.click();
|
||||
await flushPromises();
|
||||
expect(actions).toEqual(["link"]);
|
||||
});
|
||||
|
||||
it("disabled link items do not navigate", async () => {
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(
|
||||
DropdownItem,
|
||||
{ id: "link", href: "/target", isDisabled: true },
|
||||
() => h(Label, () => "Go"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const link = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||
const event = new MouseEvent("click", { bubbles: true, cancelable: true });
|
||||
link.dispatchEvent(event);
|
||||
expect(event.defaultPrevented).toBe(true);
|
||||
});
|
||||
|
||||
it("empty slot renders when the menu has no items", async () => {
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(
|
||||
DropdownMenu,
|
||||
{},
|
||||
{
|
||||
empty: () => h("p", { "data-test": "empty" }, "无结果"),
|
||||
},
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-test="empty"]')).not.toBeNull();
|
||||
});
|
||||
|
||||
it("submenu panel receives panel props and keeps caller handlers", async () => {
|
||||
const keydown = vi.fn();
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(DropdownSubmenuTrigger, {}, () => [
|
||||
h(DropdownItem, { id: "sub", textValue: "S" }, () => [
|
||||
h(Label, () => "S"),
|
||||
h(DropdownSubmenuIndicator),
|
||||
]),
|
||||
h(
|
||||
DropdownPopover,
|
||||
{
|
||||
sideOffset: 12,
|
||||
collisionPadding: 20,
|
||||
class: "panel-x",
|
||||
onKeydown: keydown,
|
||||
},
|
||||
() =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(DropdownItem, { id: "inner", textValue: "I" }, () =>
|
||||
h(Label, () => "I"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const panel = document.querySelector("[data-submenu-content]") as HTMLElement;
|
||||
expect(panel.className).toContain("panel-x");
|
||||
// Portal keeps the panel outside the clipping ancestor.
|
||||
expect(panel.closest('[data-slot="dropdown-popover"]')).toBe(panel);
|
||||
panel.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "a", bubbles: true, cancelable: true }),
|
||||
);
|
||||
await flushPromises();
|
||||
expect(keydown).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("dropdown popover is portaled out of clipping ancestors", async () => {
|
||||
const host = document.createElement("div");
|
||||
host.style.overflow = "hidden";
|
||||
host.style.isolation = "isolate";
|
||||
document.body.append(host);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(DropdownItem, { id: "a", textValue: "A" }, () =>
|
||||
h(Label, () => "A"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: host },
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const popover = document.querySelector(
|
||||
'[data-slot="dropdown-popover"]',
|
||||
) as HTMLElement;
|
||||
expect(popover).not.toBeNull();
|
||||
expect(host.contains(popover)).toBe(false);
|
||||
expect(popover.closest("[data-reka-popper-content-wrapper]")).toBe(
|
||||
popover.parentElement,
|
||||
);
|
||||
});
|
||||
|
||||
it("standalone Menu stays in flow without modal side effects", async () => {
|
||||
const outside = document.createElement("button");
|
||||
document.body.append(outside);
|
||||
const w = mount(Menu, {
|
||||
slots: { default: () => h(MenuItem, { id: "x" }, () => "X") },
|
||||
attachTo: document.body,
|
||||
});
|
||||
cleanup.push(() => w.unmount());
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const surface = document.querySelector(
|
||||
"[data-reka-menu-content]",
|
||||
) as HTMLElement;
|
||||
expect(surface.getAttribute("data-static")).toBe("true");
|
||||
expect(document.body.style.overflow).not.toBe("hidden");
|
||||
expect(outside.getAttribute("aria-hidden")).not.toBe("true");
|
||||
// Focus is not stolen from the page.
|
||||
expect(document.activeElement).not.toBe(surface);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user