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,51 @@
import { computed, type ComputedRef, type Ref } from "vue";
import type {
MenuKey,
MenuSelectionMode,
MenuSelectionOwner,
} from "../menu/context";
export interface UseMenuSelectionOptions {
selectionMode: Ref<MenuSelectionMode> | ComputedRef<MenuSelectionMode>;
selectedKeys:
Ref<ReadonlyArray<MenuKey>> | ComputedRef<ReadonlyArray<MenuKey>>;
disabledKeys?:
Ref<ReadonlyArray<MenuKey>> | ComputedRef<ReadonlyArray<MenuKey>>;
/** Accepted-value update. Return the accepted keys (may reject the proposal). */
onSelectionChange: (keys: MenuKey[]) => void;
}
/**
* Menu/section selection owner. Proposals are one-shot toggles; the parent
* emits the next keys and keeps ownership of the accepted value.
*/
export function useMenuSelection(
options: UseMenuSelectionOptions,
): MenuSelectionOwner {
const isItemSelected = (key: MenuKey) =>
options.selectedKeys.value.some((k) => k === key);
const isItemDisabled = (key: MenuKey) =>
options.disabledKeys?.value.some((k) => k === key) ?? false;
const proposeSelection = (key: MenuKey) => {
const mode = options.selectionMode.value;
if (mode === "none") return;
const current = options.selectedKeys.value;
const has = isItemSelected(key);
if (mode === "single") {
options.onSelectionChange(has ? [] : [key]);
return;
}
options.onSelectionChange(
has ? current.filter((k) => k !== key) : [...current, key],
);
};
return {
selectionMode: computed(() => options.selectionMode.value),
selectedKeys: computed(() => options.selectedKeys.value),
proposeSelection,
isItemSelected,
isItemDisabled,
};
}