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
@@ -21,15 +21,6 @@ import {
type NativeClickHandler =
((e: MouseEvent) => void) | ((e: MouseEvent) => void)[] | undefined;
function invokeClick(handlers: NativeClickHandler, event: MouseEvent): void {
if (!handlers) return;
if (Array.isArray(handlers)) {
for (const fn of handlers) fn(event);
} else {
handlers(event);
}
}
export default defineComponent({
name: "MenuItem",
inheritAttrs: false,
@@ -87,34 +78,46 @@ export default defineComponent({
},
});
function activate(cancelEvent?: { preventDefault?: () => void }) {
function activate() {
if (isDisabled.value) return;
emit("select", key.value);
if (hasSubmenu.value) {
cancelEvent?.preventDefault?.();
return;
}
if (hasSubmenu.value) return;
const mode = selectionMode.value;
if (mode !== "none") {
owner?.proposeSelection(key.value);
if (mode === "single") menuCtx?.closeLevel();
else cancelEvent?.preventDefault?.();
return;
}
menuCtx?.emitAction(key.value);
if (props.closeOnSelect === false) cancelEvent?.preventDefault?.();
else menuCtx?.closeLevel();
if (props.closeOnSelect === false) return;
menuCtx?.closeLevel();
}
return () => {
// Own the click chain so user preventDefault cancels activation/close.
// Reka's handleSelect is not used for our action contract.
// Hand the user handlers and our activation to Vue as one DOM `onClick`
// array so browser/Vue listener semantics stay intact: capture order,
// preventDefault and stopImmediatePropagation all behave natively, and a
// cancelled event never reaches the activation entry. Reka's own
// `handleSelect` is prepended by its prop merge and is neutralised in
// `onSelect`, so it cannot emit an action or close the menu.
const userOnClick = (attrs as { onClick?: NativeClickHandler }).onClick;
const rest: Record<string, unknown> = {
...(attrs as Record<string, unknown>),
};
delete rest.onClick;
const clickChain: Array<(e: MouseEvent) => void> = (
Array.isArray(userOnClick)
? [...userOnClick]
: userOnClick
? [userOnClick]
: []
) as Array<(e: MouseEvent) => void>;
clickChain.push((event: MouseEvent) => {
if (event.defaultPrevented || isDisabled.value) return;
activate();
});
return h(
RekaMenuItem as never,
{
@@ -134,17 +137,7 @@ export default defineComponent({
"data-selection-mode": selectionMode.value,
disabled: isDisabled.value,
textValue: props.textValue,
onClick: (event: MouseEvent) => {
invokeClick(userOnClick, event);
if (event.defaultPrevented || isDisabled.value) {
event.stopImmediatePropagation();
return;
}
activate(event);
// Cancel Reka's select/close; we own the close contract.
event.preventDefault();
event.stopImmediatePropagation();
},
onClick: clickChain,
onSelect: (itemSelectEvent?: { preventDefault?: () => void }) => {
itemSelectEvent?.preventDefault?.();
},