fix: real trigger owns long press, Vue click chain and layered Esc

- DropdownTrigger takes over the longPress press lifecycle in capture phase and
  cancels Reka's own open/toggle through its defaultPrevented guard; DropdownRoot
  no longer wraps the whole menu content. Short click, Enter and Space now open
  exactly once, and release cannot close the menu again.
- MenuItem hands user handlers plus activation to Vue as one DOM onClick array,
  so preventDefault, capture and stopImmediatePropagation keep native semantics
  instead of a hand-rolled handler loop.
- DropdownSubmenuTrigger owns its own open state and closes only its level on
  Escape, restoring focus to the submenu trigger; the parent menu stays open and
  a second Escape closes the root.
This commit is contained in:
maoyingjie
2026-10-24 21:42:25 +08:00
parent 1692c6b0ac
commit e797c25d46
10 changed files with 565 additions and 132 deletions
+56
View File
@@ -1,4 +1,5 @@
import { afterEach, expect, it, vi } from "vitest";
import { Button, DropdownTrigger } from "../packages/vue/src";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h } from "vue";
import {
@@ -104,3 +105,58 @@ it("standalone exported Menu and MenuItem do not require an internal DropdownMen
cleanup.push(() => w.unmount());
}).not.toThrow();
});
it("stopImmediatePropagation stops subsequent user handlers and activation", async () => {
const action = vi.fn();
const second = vi.fn();
const node = await renderMenu(
{ onAction: action },
{
onClick: [(e: MouseEvent) => e.stopImmediatePropagation(), second],
},
);
node.click();
await flushPromises();
expect(second).not.toHaveBeenCalled();
expect(action).not.toHaveBeenCalled();
});
it("longPress trigger does not open on a short click of the actual button", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { trigger: "longPress" }, () => [
h(DropdownTrigger, {}, () => h(Button, {}, () => "Hold")),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
),
]),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
(w.get("button").element as HTMLElement).click();
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).toBeNull();
});
it("longPress keyboard Enter opens instead of toggling twice", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { trigger: "longPress" }, () => [
h(DropdownTrigger, {}, () => h(Button, {}, () => "Hold")),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
),
]),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
await w.get("button").trigger("keydown", { key: "Enter" });
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});