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:
@@ -160,13 +160,17 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
|
||||
|
||||
### DropdownRoot
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | ---------------- | ------ | -------------- |
|
||||
| `open` | `boolean` | — | 受控展开 |
|
||||
| `defaultOpen` | `boolean` | — | 非受控初始展开 |
|
||||
| `modal` | `boolean` | `true` | 模态焦点管理 |
|
||||
| `dir` | `'ltr' \| 'rtl'` | — | 文本方向 |
|
||||
| `update:open` | 事件 | — | 展开变更 |
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------------- | ------------------------ | --------- | ------------------------------- |
|
||||
| `open` | `boolean` | — | 受控展开 |
|
||||
| `defaultOpen` | `boolean` | — | 非受控初始展开 |
|
||||
| `modal` | `boolean` | `true` | 模态焦点管理 |
|
||||
| `dir` | `'ltr' \| 'rtl'` | — | 文本方向 |
|
||||
| `trigger` | `'press' \| 'longPress'` | `'press'` | 长按打开并抑制随后 click |
|
||||
| `longPressDuration` | `number` | `500` | 长按时长 ms;移动超过 10px 取消 |
|
||||
| `update:open` | 事件 | — | 展开变更 |
|
||||
|
||||
长按模式下 Enter/Space 仍可键盘打开;`pointerup`/`pointercancel`/`pointerleave`/卸载会清理计时。
|
||||
|
||||
### DropdownTrigger
|
||||
|
||||
@@ -199,13 +203,16 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
|
||||
|
||||
### DropdownItem
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------ | ----------------------- | ----------- | -------------- |
|
||||
| `id` | `MenuKey` | — | 条目标识 |
|
||||
| `textValue` | `string` | — | 首字母导航文本 |
|
||||
| `variant` | `'default' \| 'danger'` | `'default'` | 视觉变体 |
|
||||
| `isDisabled` | `boolean` | `false` | 禁用 |
|
||||
| `select` | 事件 `(key: MenuKey)` | — | 激活回调 |
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| --------------- | ----------------------- | ----------- | ---------------------------- |
|
||||
| `id` | `MenuKey` | — | 条目标识 |
|
||||
| `textValue` | `string` | — | 首字母导航文本 |
|
||||
| `variant` | `'default' \| 'danger'` | `'default'` | 视觉变体 |
|
||||
| `isDisabled` | `boolean` | `false` | 禁用 |
|
||||
| `closeOnSelect` | `boolean` | — | `false` 时动作后保持菜单打开 |
|
||||
| `select` | 事件 `(key: MenuKey)` | — | 激活回调 |
|
||||
|
||||
**事件取消**:条目上 `@click.prevent`(或 `preventDefault`)会取消默认动作、选择变更与关闭。选择项输出 `menuitemradio`/`menuitemcheckbox` 并带 `aria-checked`;`selectionMode=none` 为 `menuitem`。
|
||||
|
||||
### DropdownItemIndicator
|
||||
|
||||
@@ -224,9 +231,9 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
|
||||
## 上游差异
|
||||
|
||||
- React 对象部件改具名导出;`Selection`/`Set` 改 `MenuKey[]`;`onAction`/`onSelectionChange` 改 Vue 事件。
|
||||
- 条目 `onClick` `preventDefault` 取消动作/选择/关闭(对应上游可取消事件契约)。
|
||||
- 长按封装在 `DropdownRoot` `trigger="longPress"`;Menu/Item/Section 导出含 `*Props` 类型。
|
||||
- `Kbd slot="keyboard"` 为上游原生 slot 透传;本库 Kbd 支持同结构。
|
||||
- Menu/Item/Section 无独立官方文档页,契约折叠于 Dropdown 页;本库提供同名导出供组合。
|
||||
- 长按触发由 Reka `DropdownMenuRoot` 的 trigger 策略提供;与上游 `trigger="longPress"` 对应关系见演示。
|
||||
|
||||
## 相关组件
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
import { onBeforeUnmount, ref } from "vue";
|
||||
import {
|
||||
DropdownRoot,
|
||||
DropdownTrigger,
|
||||
@@ -9,78 +8,13 @@ import {
|
||||
Button,
|
||||
Label,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const open = ref(false);
|
||||
let pressTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let suppressClick = false;
|
||||
|
||||
function clearPressTimer() {
|
||||
if (pressTimer !== null) {
|
||||
clearTimeout(pressTimer);
|
||||
pressTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function onPointerDown(e: PointerEvent) {
|
||||
clearPressTimer();
|
||||
suppressClick = false;
|
||||
const startX = e.clientX;
|
||||
const startY = e.clientY;
|
||||
pressTimer = setTimeout(() => {
|
||||
pressTimer = null;
|
||||
suppressClick = true;
|
||||
open.value = true;
|
||||
}, 500);
|
||||
const onMove = (event: PointerEvent) => {
|
||||
if (
|
||||
Math.abs(event.clientX - startX) > 10 ||
|
||||
Math.abs(event.clientY - startY) > 10
|
||||
) {
|
||||
clearPressTimer();
|
||||
window.removeEventListener("pointermove", onMove);
|
||||
}
|
||||
};
|
||||
window.addEventListener("pointermove", onMove);
|
||||
const cleanupMove = () => {
|
||||
window.removeEventListener("pointermove", onMove);
|
||||
window.removeEventListener("pointerup", cleanupMove);
|
||||
window.removeEventListener("pointercancel", cleanupMove);
|
||||
};
|
||||
window.addEventListener("pointerup", cleanupMove);
|
||||
window.addEventListener("pointercancel", cleanupMove);
|
||||
}
|
||||
|
||||
function onPointerUp() {
|
||||
clearPressTimer();
|
||||
}
|
||||
|
||||
function onClick(e: MouseEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (suppressClick) {
|
||||
suppressClick = false;
|
||||
return;
|
||||
}
|
||||
open.value = false;
|
||||
}
|
||||
|
||||
onBeforeUnmount(clearPressTimer);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<DropdownRoot v-model:open="open">
|
||||
<!-- trigger="longPress": press-and-hold opens; click is suppressed. -->
|
||||
<DropdownRoot trigger="longPress">
|
||||
<DropdownTrigger>
|
||||
<Button
|
||||
aria-label="菜单"
|
||||
variant="secondary"
|
||||
@pointerdown="onPointerDown"
|
||||
@pointerup="onPointerUp"
|
||||
@pointercancel="onPointerUp"
|
||||
@pointerleave="onPointerUp"
|
||||
@click.capture="onClick"
|
||||
@contextmenu.prevent
|
||||
>
|
||||
长按
|
||||
</Button>
|
||||
<Button aria-label="菜单" variant="secondary">长按</Button>
|
||||
</DropdownTrigger>
|
||||
<DropdownPopover>
|
||||
<DropdownMenu>
|
||||
|
||||
Reference in New Issue
Block a user