fix: menu selection ARIA, cancelable activation, submenu and long-press

- Selected items use menuitemradio/menuitemcheckbox + aria-checked + data-selected
- onClick preventDefault cancels action/selection/close (user handlers first)
- Submenu trigger is a single menuitem (asChild); standalone Menu owns Reka root
- DropdownRoot trigger=longPress with threshold, keyboard fallback, cleanup
- Export *Props types; independent tarball ESM/CSS + NodeNext/bundler contracts
- Esc still closes the full submenu stack (recorded gap)
This commit is contained in:
maoyingjie
2026-10-24 21:19:04 +08:00
parent 06ddb6662a
commit 1692c6b0ac
16 changed files with 719 additions and 141 deletions
@@ -4,7 +4,6 @@ import {
defineComponent,
h,
inject,
mergeProps,
provide,
ref,
useAttrs,
@@ -19,6 +18,18 @@ import {
type MenuKey,
} from "../menu/context";
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,
@@ -56,9 +67,15 @@ export default defineComponent({
const isSelected = computed(
() => owner?.isItemSelected(key.value) ?? false,
);
const selectionMode = computed(() => owner?.selectionMode.value ?? "none");
const slotsComputed = computed(() =>
menuItemVariants({ variant: props.variant as never }),
);
const role = computed(() => {
if (selectionMode.value === "multiple") return "menuitemcheckbox";
if (selectionMode.value === "single") return "menuitemradio";
return "menuitem";
});
provide(menuItemContextKey, {
slots: slotsComputed,
@@ -70,46 +87,71 @@ export default defineComponent({
},
});
function handleSelect(event?: { preventDefault?: () => void }) {
function activate(cancelEvent?: { preventDefault?: () => void }) {
if (isDisabled.value) return;
emit("select", key.value);
const mode = owner?.selectionMode.value ?? "none";
// Submenu trigger items open a nested menu; never close this level.
if (hasSubmenu.value) {
event?.preventDefault?.();
cancelEvent?.preventDefault?.();
return;
}
const mode = selectionMode.value;
if (mode !== "none") {
owner?.proposeSelection(key.value);
// Keep the menu open for multi-select; single closes like upstream.
if (mode === "single") {
menuCtx?.closeLevel();
} else {
event?.preventDefault?.();
}
if (mode === "single") menuCtx?.closeLevel();
else cancelEvent?.preventDefault?.();
return;
}
menuCtx?.emitAction(key.value);
if (props.closeOnSelect === false) event?.preventDefault?.();
if (props.closeOnSelect === false) cancelEvent?.preventDefault?.();
else menuCtx?.closeLevel();
}
return () =>
h(
return () => {
// Own the click chain so user preventDefault cancels activation/close.
// Reka's handleSelect is not used for our action contract.
const userOnClick = (attrs as { onClick?: NativeClickHandler }).onClick;
const rest: Record<string, unknown> = {
...(attrs as Record<string, unknown>),
};
delete rest.onClick;
return h(
RekaMenuItem as never,
mergeProps(attrs as Record<string, unknown>, {
{
...rest,
class: [slotsComputed.value.item(), attrs.class],
role: (attrs.role as string | undefined) ?? role.value,
"aria-checked":
selectionMode.value === "none"
? undefined
: isSelected.value
? "true"
: "false",
"aria-disabled": isDisabled.value || undefined,
"data-slot": "menu-item",
"data-selected": isSelected.value || undefined,
"data-has-submenu": hasSubmenu.value || undefined,
"data-selection-mode": owner?.selectionMode.value ?? "none",
"data-selection-mode": selectionMode.value,
disabled: isDisabled.value,
textValue: props.textValue,
// User handlers (if any) run first via mergeProps array.
onSelect: (event: unknown) =>
handleSelect(event as { preventDefault?: () => void }),
}),
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();
},
onSelect: (itemSelectEvent?: { preventDefault?: () => void }) => {
itemSelectEvent?.preventDefault?.();
},
},
slots,
);
};
},
});
</script>