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
@@ -1,8 +1,18 @@
<script lang="ts">
import { computed, defineComponent, h, inject, useAttrs } from "vue";
import { defineComponent, h, inject, onBeforeUnmount, useAttrs } from "vue";
import { DropdownMenuTrigger as RekaDropdownMenuTrigger } from "reka-ui";
import { dropdownContextKey } from "../menu/context";
const LONG_PRESS_MOVE_PX = 10;
const SELECTION_KEYS = new Set(["Enter", " ", "Spacebar"]);
/**
* Trigger. In `longPress` mode this component owns the press lifecycle and
* cancels Reka's own click/keydown activation through its documented
* `event.defaultPrevented` guard, so the real trigger element decides when the
* menu opens instead of a late bubble interception.
*/
export default defineComponent({
name: "DropdownTrigger",
inheritAttrs: false,
@@ -13,8 +23,73 @@ export default defineComponent({
setup(props, { slots }) {
const attrs = useAttrs();
const dropdownCtx = inject(dropdownContextKey, null);
return () =>
h(
const isLongPress = () =>
(dropdownCtx?.triggerMode.value ?? "press") === "longPress";
let timer: ReturnType<typeof setTimeout> | null = null;
let startX = 0;
let startY = 0;
function clearPress() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
function onPointerdown(event: PointerEvent) {
if (event.isPrimary === false) return;
if (event.pointerType === "mouse" && event.button !== 0) return;
clearPress();
startX = event.clientX;
startY = event.clientY;
timer = setTimeout(() => {
timer = null;
dropdownCtx?.setOpen(true);
}, dropdownCtx?.longPressDuration.value ?? 500);
}
function onPointermove(event: PointerEvent) {
if (!timer) return;
if (
Math.hypot(event.clientX - startX, event.clientY - startY) <=
LONG_PRESS_MOVE_PX
)
return;
clearPress();
}
function onActivation(event: MouseEvent | KeyboardEvent) {
if (!isLongPress()) return;
// Short press never opens; after a long press the closing click/keyup
// must not toggle the menu back. Cancelling here also stops Reka's own
// trigger handler for this event.
event.preventDefault();
}
function onKeydown(event: KeyboardEvent) {
if (!isLongPress()) return;
if (!SELECTION_KEYS.has(event.key)) return;
event.preventDefault();
dropdownCtx?.setOpen(!(dropdownCtx?.open.value ?? false));
}
const guardHandlers = {
onPointerdownCapture: onPointerdown,
onPointermoveCapture: onPointermove,
onPointerupCapture: clearPress,
onPointercancelCapture: clearPress,
onPointerleaveCapture: clearPress,
onClickCapture: onActivation,
onKeydownCapture: onKeydown,
onContextmenuCapture: clearPress,
};
onBeforeUnmount(clearPress);
return () => {
const trigger = h(
RekaDropdownMenuTrigger as never,
{
...attrs,
@@ -25,6 +100,19 @@ export default defineComponent({
},
slots,
);
if (!isLongPress()) return trigger;
return h(
"span",
{
...guardHandlers,
style: "display: contents",
"data-slot": "dropdown-trigger-region",
},
[trigger],
);
};
},
});
</script>