feat: migrate Menu and Dropdown with selection and submenus

Port HeroUI menu family and Dropdown from ce90ea3: Reka DropdownMenu base,
MenuKey[] selection with section owners, 17 official demos, dropdown docs
and B01 tests. Long-press stays demo-level; submenu close is level-scoped.
This commit is contained in:
maoyingjie
2026-10-24 20:43:04 +08:00
parent 1c4ecc3def
commit 06ddb6662a
48 changed files with 3091 additions and 5 deletions
@@ -0,0 +1,115 @@
<script lang="ts">
import {
computed,
defineComponent,
h,
inject,
mergeProps,
provide,
ref,
useAttrs,
type PropType,
} from "vue";
import { DropdownMenuItem as RekaMenuItem } from "reka-ui";
import { menuItemVariants, type MenuItemVariants } from "@heroui-vue/styles";
import {
menuContextKey,
menuItemContextKey,
menuSectionContextKey,
type MenuKey,
} from "../menu/context";
export default defineComponent({
name: "MenuItem",
inheritAttrs: false,
props: {
id: { type: [String, Number] as PropType<MenuKey>, default: undefined },
textValue: { type: String, default: undefined },
isDisabled: { type: Boolean, default: false },
disabled: { type: Boolean, default: undefined },
variant: {
type: String as PropType<MenuItemVariants["variant"]>,
default: "default",
},
/** Set false to keep the menu open after an action item. */
closeOnSelect: { type: Boolean, default: undefined },
},
emits: {
select: (_key: MenuKey) => true,
},
setup(props, { slots, emit }) {
const attrs = useAttrs();
const menuCtx = inject(menuContextKey, null);
const sectionCtx = inject(menuSectionContextKey, null);
const owner = sectionCtx?.selection ?? menuCtx?.selection;
const hasSubmenu = ref(false);
const key = computed<MenuKey>(
() => props.id ?? (attrs.id as MenuKey | undefined) ?? "",
);
const isDisabled = computed(
() =>
props.isDisabled ||
props.disabled === true ||
(owner?.isItemDisabled(key.value) ?? false),
);
const isSelected = computed(
() => owner?.isItemSelected(key.value) ?? false,
);
const slotsComputed = computed(() =>
menuItemVariants({ variant: props.variant as never }),
);
provide(menuItemContextKey, {
slots: slotsComputed,
isDisabled,
isSelected,
hasSubmenu: computed(() => hasSubmenu.value),
setHasSubmenu(value: boolean) {
hasSubmenu.value = value;
},
});
function handleSelect(event?: { 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?.();
return;
}
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?.();
}
return;
}
menuCtx?.emitAction(key.value);
if (props.closeOnSelect === false) event?.preventDefault?.();
else menuCtx?.closeLevel();
}
return () =>
h(
RekaMenuItem as never,
mergeProps(attrs as Record<string, unknown>, {
class: [slotsComputed.value.item(), attrs.class],
"data-slot": "menu-item",
"data-has-submenu": hasSubmenu.value || undefined,
"data-selection-mode": owner?.selectionMode.value ?? "none",
disabled: isDisabled.value,
textValue: props.textValue,
// User handlers (if any) run first via mergeProps array.
onSelect: (event: unknown) =>
handleSelect(event as { preventDefault?: () => void }),
}),
slots,
);
},
});
</script>