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:
@@ -4,6 +4,7 @@ import { defineComponent, h, ref, nextTick } from "vue";
|
||||
import {
|
||||
Button,
|
||||
DropdownRoot,
|
||||
DropdownTrigger,
|
||||
DropdownPopover,
|
||||
DropdownMenu,
|
||||
DropdownItem,
|
||||
@@ -258,7 +259,9 @@ it("long-press trigger opens on hold and suppresses click", async () => {
|
||||
const w = mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
|
||||
h(Button, { "aria-label": "菜单" }, () => "长按"),
|
||||
h(DropdownTrigger, {}, () =>
|
||||
h(Button, { "aria-label": "菜单" }, () => "长按"),
|
||||
),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(DropdownItem, { id: "x", textValue: "X" }, () =>
|
||||
@@ -286,7 +289,7 @@ it("long-press cancel on move threshold", async () => {
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
|
||||
h(Button, () => "长按"),
|
||||
h(DropdownTrigger, {}, () => h(Button, () => "长按")),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(DropdownItem, { id: "x", textValue: "X" }, () =>
|
||||
@@ -318,3 +321,262 @@ it("long-press cancel on move threshold", async () => {
|
||||
).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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user