fix(dropdown): preserve grouped root attrs and events without breaking seams

This commit is contained in:
maoyingjie
2026-10-28 12:35:35 +08:00
parent 8b1505438e
commit f8de4529f6
12 changed files with 305 additions and 23 deletions
+1 -1
View File
@@ -410,7 +410,7 @@ Dropdown 组件实现 ARIA 菜单模式,并提供:
- 官方 `items` + 渲染函数的数据驱动集合用 `v-for` 表达;`renderEmptyState` 对应 `empty` 插槽。不提供 React Collection 对象模型。
- 子菜单悬停展开延迟由底层固定为 100ms,未暴露官方 `delay`(默认 200ms);子菜单面板的 `side`/`align` 由底层固定,只有偏移与碰撞参数可调。
- `DropdownTrigger` 的作用域插槽只提供 `{ isOpen }`,`DropdownPopover` 只提供 `{ isEntering, isExiting, placement }`;官方 Button/Popover 的完整 render props 未全部覆盖。
- `DropdownRoot` 额外渲染一层 `.hv-dropdown` 包裹元素(官方该类没有 DOM 宿主),用于承载根级 class 与方向。作为 `ButtonGroup` 直接子级时省略该包裹元素,让触发按钮保持组内的位置接缝;该场景下根级 attrs 与官方一致不落 DOM。
- `DropdownRoot` 额外渲染一层 `.hv-dropdown` 包裹元素(官方该类没有 DOM 宿主),承载根级 class、style、id、ARIA、data 属性与原生事件。作为 `ButtonGroup` 直接子级时仍保留该容器,接缝样式按容器在组内的位置适配内部触发按钮;根级属性与捕获事件的行为不变。
- 条目 `id` 是业务 key,DOM id 为实例级;官方把 key 直接作为 DOM id(Vue 端避免数字 `0` 与字符串 `"0"` 冲突)。
- 条目 `onClick` 中 `preventDefault()` 会取消动作、选择与关闭;`stopImmediatePropagation()` 会终止后续处理器,均为原生事件语义。
+7
View File
@@ -3,6 +3,13 @@ import { defineAsyncComponent } from "vue";
// Keep regression URLs stable; load only the selected harness.
const labs = [
{
key: "button-group-dropdown",
title: "按钮组与下拉根属性",
component: defineAsyncComponent(
() => import("../../../tests/fixtures/button-group-dropdown.vue"),
),
},
{
key: "handbook",
title: "Handbook 实现规范",
+2
View File
@@ -288,3 +288,5 @@ ComboBox/Autocomplete的正式组合、原30项返修和本轮定位/ref/ARIA/lo
## Handbook 共享规范审计(2026-10-27)
固定源六篇56章节的实现证据与差异见 [Handbook审计](handbook-audit.md)。修复 data-scrollbar 三模式、八类浮层/Skeleton/Drawer 的减少动效与动态取消动画后的进入标记清理。按需 CSS 子路径、公共 useTheme、局部主题自动跨 Teleport 及 Skeleton 动画变量的局部/动态读取仍未提供;不声明所有组件已完成全部规范的浏览器验收。逐项场景与实际验证见 [任务结果](tasks/results/HANDBOOK-IMPLEMENTATION-AUDIT.md)。
HD-01(2026-10-28):按钮/切换组与导航示例已恢复固定cn基线;ButtonGroup内Dropdown保留根attrs/事件,以容器位置适配接缝。HD01-R1已关闭;不升级整族API兼容状态,详见[结果](tasks/results/HD-01-BUTTON-NAV-DEMOS.md)。
+5 -5
View File
@@ -1,5 +1,5 @@
<!-- task-metadata
{"base":"db5670f4e1efe0e6ef7433002de5fb0a4a284f84","upstream":"ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960","status":"implemented","requires":["docs/tasks/HD-QUEUE.md"],"sources":["apps/docs/src/demos/cn/button-group/basic.tsx"],"checks":["pnpm exec vitest run tests/button-demos.test.ts tests/button-toggle.test.ts tests/disclosure-accordion.test.ts","pnpm typecheck"]}
{"base":"db5670f4e1efe0e6ef7433002de5fb0a4a284f84","upstream":"ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960","status":"accepted","requires":["docs/tasks/HD-QUEUE.md"],"sources":["apps/docs/src/demos/cn/button-group/basic.tsx"],"checks":["pnpm exec vitest run tests/button-demos.test.ts tests/button-toggle.test.ts tests/disclosure-accordion.test.ts tests/b01-menu-dropdown.test.ts","pnpm typecheck"]}
-->
# HD-01-BUTTON-NAV-DEMOS:按钮与导航基线示例恢复
@@ -7,10 +7,10 @@
## 执行范围
- [ ] 恢复 button-group 全部示例的中文内容、数据、次序和完整组合:basic/custom-styles 的合并下拉与菜单说明、sizes 的 secondary、orientation 图标组、full-width 第二组、disabled 的子项覆盖例。
- [ ] 恢复 toggle-button / toggle-button-group 的中文文案、按钮数量、图标组、custom-styles 主题类及 aria-label;不要用紫色固定配色替代。
- [ ] 恢复 accordion-custom-styles 的三项藏品引导、原图片、surface 变体、选择器与 hover 动效;恢复 disclosure-group-controlled 的 HeroUI Native / Expo 文案与图片语义,品牌图形遵循现有图标约定并记录差异。
- [ ] 还原示例内部的 gap、对齐和换行;不能统一用 demo-row 覆盖不同根布局。button-basic 点击改文案、loading 新增段落应按基线收口;保留防重复触发保护。
- [x] 恢复 button-group 全部示例的中文内容、数据、次序和完整组合:basic/custom-styles 的合并下拉与菜单说明、sizes 的 secondary、orientation 图标组、full-width 第二组、disabled 的子项覆盖例。
- [x] 恢复 toggle-button / toggle-button-group 的中文文案、按钮数量、图标组、custom-styles 主题类及 aria-label;不要用紫色固定配色替代。
- [x] 恢复 accordion-custom-styles 的三项藏品引导、原图片、surface 变体、选择器与 hover 动效;恢复 disclosure-group-controlled 的 HeroUI Native / Expo 文案与图片语义,品牌图形遵循现有图标约定并记录差异。
- [x] 还原示例内部的 gap、对齐和换行;不能统一用 demo-row 覆盖不同根布局。button-basic 点击改文案、loading 新增段落应按基线收口;保留防重复触发保护。
## 用户可见通过条件
+21 -1
View File
@@ -74,7 +74,7 @@
## 集成人独立复核(2026-10-28)
复核基线:13f7b0b1fbb2857acc64df39edc2f506641743ce;范围为 df954af..13f7b0b。结论:示例恢复基本符合任务,接缝修复有效;下列新增回归未关闭前暂不验收合并。
复核基线:13f7b0b1fbb2857acc64df39edc2f506641743ce;范围为 df954af..13f7b0b。当时结论:示例恢复基本符合任务,接缝修复有效;下列新增回归未关闭前暂不验收合并。随后用户授权集成人修复并合并,关闭证据见下节。
| ID | 级别 / 归类 | 复现与实际结果 | 修复与验收要求 |
| --- | --- | --- | --- |
@@ -89,6 +89,26 @@
- HD01-R1另用临时VTU反例复现:组外断言通过,组内监听调用0次导致期望1次失败,rootAttr=false、aria-expanded=true。临时文件已清理;修复者应将正式回归归入现有button-toggle或menu-dropdown测试,不新增返修轮次文件。
- 临时反例清理后重新运行pnpm typecheck通过。本轮不修改实现、不合main、不push;此前已完成的构建和产物检查不冒称为本轮重跑。
## HD01-R1 集成人修复与验收(2026-10-28)
- 修复起点:8b15054;本节与实现同提交,准确SHA见本文件Git历史及后续main交接记录。用户明确授权“你改下吧,然后合并提交”。
- HD01-R1已关闭:保留DropdownRoot根容器承载class/style/id/data/ARIA及原生事件。ButtonGroup内部使用data-button-group-child标记适配触发按钮首/中/尾圆角与outline逻辑边框,不再通过删根元素修接缝;123e3f9的去容器方案已被替代。普通Button、独立Dropdown及调用方工具类覆盖保持原契约。
- 正式回归归入tests/button-toggle.test.ts:根身份与样式动态更新,原生可取消click/pointerdown/keydown的根捕获监听均执行且可阻止打开,放行后只打开一次;组外attrs仍有效。修改原结构断言使其检查容器位置标记,真实接缝由浏览器几何验证。
- 可复现浏览器夹具:5174为已确认cwd=apps/playground的原有服务,访问http://127.0.0.1:5174/?button-group-dropdown。勾选“取消打开”后点击/Enter均保持关闭,捕获计数增加;取消勾选后Enter打开菜单,Esc结束后回焦last。勾选“隐藏根容器”后9个根容器display=none、宽度0,取消后恢复。根id、ARIA和自定义CSS变量kept保留。
- 夹具3组:horizontal-ltr、horizontal-rtl、vertical-ltr,每组三个Dropdown。首/中/尾圆角及outline左右/上下边框实测正确:LTR首[24,0,0,24]、中全0、尾[0,24,24,0];RTL首尾镜像;纵向首顶部圆角、尾底部圆角。只留外侧1px描边,内部接缝0px。文档basic混合Button/Dropdown同样正确;custom-styles保留全0圆角与rgb(8,135,43)。
- 文档5173核验浅深主题;360px时document.scrollWidth=360,basic/custom预览clientWidth=scrollWidth=318,未横向溢出;结束恢复系统主题与原视口。不关闭原有5173/5174服务。
| 检查 | 实际结果 |
| --- | --- |
| 定向button-toggle/b01-menu-dropdown/trigger-events/button-demos | 4文件96项通过 |
| pnpm test | 全部110文件1617项客户端用例通过 |
| pnpm typecheck | 通过 |
| pnpm build:docs | 通过;既有chunk大小提示非失败 |
| pnpm --filter @heroui-vue/playground build | 通过,含公开组合回归夹具 |
| node scripts/check-package.mjs b01-menu-dropdown | 最终tarball ESM/CSS及NodeNext/bundler契约通过;新增产物接缝标记检查 |
首次离线产物检查因TypeScript tarball缺缓存失败,联网重跑完整命令后通过,不把离线失败算通过。无新依赖、无SSR/水合/Nuxt或逐像素工程;HD-02及其他任务未实施。
## 未完成与下一会话
- 6049437(与 896bac2 内容重复)遗留在 task/docs-header-hover-parity 分支顶部;集成人可按重复变更合并,或在该分支无人使用时将其裁掉,本会话按约定未动他人分支。
+4
View File
@@ -352,3 +352,7 @@ CSS/tv 仅加 `hv-` 前缀与 Apache-2.0 来源;`colorPickerVariants` 保持
## DOCS-CONTROL-STATES(2026-10-28)
固定 ce90ea3 的 search-field.css、number-field.css 及 search-dialog.tsx 用于核对来源;本库两类原生输入补 placeholder:text-field-placeholder 属主题一致性修复,不声称原样复制即可满足规范。文档源码入口改 Link + buttonVariants 为 Vue 公共契约适配,组合入口的下划线在文档使用处限定;普通 Link 行为保留。见任务结果。
## HD-01 示例与 Dropdown 组合(2026-10-28)
Button/ButtonGroup/ToggleButton/ToggleButtonGroup与指定Accordion/Disclosure示例按ce90ea3的cn源码恢复,保留Tabler图标适配。Vue Dropdown根容器承载attrs/原生事件;ButtonGroup CSS增加仅作用于标记直接成员的位置规则,圆角/outline数值沿用基线,来源许可保留。行为和证据见HD-01任务结果。
+4
View File
@@ -136,3 +136,7 @@ COLOR-PICKER 独立复核与集成(2026-10-27):复核修复 `74cd597`(
## 文档控件与 placeholder(2026-10-28)
DOCS-CONTROL-STATES:定向4文件/76项、typecheck、文档构建与handbook-styles最终tarball通过。真实浏览器核对五类字段主题token、禁用/只读、390px及文档搜索/主题/目录/链接/复制;含修复前3PASS2FAIL与独立tarball五项PASS反证链。顶部Button入口未复现hover变色,与真实输入框缺主题token分开记录。准确范围、复现与限制见[结果](tasks/results/DOCS-CONTROL-STATES.md)。
## HD-01 独立复核与返修(2026-10-28)
HD01-R1根attrs/捕获事件回归已修复;全量110文件1617项、类型、文档/回归页构建与b01-menu-dropdown最终tarball通过。真实浏览器覆盖取消打开/放行/回焦、根样式隐藏、横纵/RTL接缝及360px。详见[同一结果表](tasks/results/HD-01-BUTTON-NAV-DEMOS.md),未将机制相同或测试数量当成全库视觉验收。
@@ -146,3 +146,73 @@
.hv-button-group--full-width {
@apply w-full;
}
/* Vue Dropdown keeps a root element for consumer attrs and capture events.
* Its trigger's position belongs to that wrapper, not to the trigger's own
* :first-child/:last-child. Scope the correction to marked direct members;
* ordinary buttons and standalone dropdowns retain the baseline rules. */
.hv-button-group > .hv-dropdown[data-button-group-child] > .hv-button {
@apply rounded-none;
}
.hv-button-group--horizontal
> .hv-dropdown[data-button-group-child]:first-child
> .hv-button {
@apply rounded-s-3xl;
}
.hv-button-group--horizontal
> .hv-dropdown[data-button-group-child]:last-child
> .hv-button {
@apply rounded-e-3xl;
}
.hv-button-group--vertical
> .hv-dropdown[data-button-group-child]:first-child
> .hv-button {
@apply rounded-t-3xl;
}
.hv-button-group--vertical
> .hv-dropdown[data-button-group-child]:last-child
> .hv-button {
@apply rounded-b-3xl;
}
/* Only the outer outline edges remain; utilities on the trigger may still
* override these component-layer defaults, as they do for direct Buttons. */
.hv-button-group--horizontal
> .hv-dropdown[data-button-group-child]
> .hv-button--outline {
@apply border-x-0;
}
.hv-button-group--horizontal
> .hv-dropdown[data-button-group-child]:first-child
> .hv-button--outline {
@apply border-s;
}
.hv-button-group--horizontal
> .hv-dropdown[data-button-group-child]:last-child
> .hv-button--outline {
@apply border-e;
}
.hv-button-group--vertical
> .hv-dropdown[data-button-group-child]
> .hv-button--outline {
@apply border-y-0;
}
.hv-button-group--vertical
> .hv-dropdown[data-button-group-child]:first-child
> .hv-button--outline {
@apply border-t;
}
.hv-button-group--vertical
> .hv-dropdown[data-button-group-child]:last-child
> .hv-button--outline {
@apply border-b;
}
@@ -33,10 +33,9 @@ export default defineComponent({
/** Long-press duration in ms (longPress trigger). */
longPressDuration: { type: Number, default: LONG_PRESS_DURATION },
/**
* Absorbs ButtonGroup's direct-child marker; not a public API. When marked,
* the root wrapper is skipped so the trigger keeps the positional slot the
* group seam styles rely on — upstream `Dropdown` has no DOM root (RAC
* `MenuTrigger` renders a bare SlotProvider) and drops root-level attrs.
* Absorbs ButtonGroup's direct-child marker; not a public API. The wrapper
* remains the attrs/event host; group styles use its position to shape the
* nested trigger instead of dropping the root and its consumer attributes.
*/
__button_group_child: { type: Boolean, default: false },
},
@@ -78,10 +77,6 @@ export default defineComponent({
},
{ default: () => slots.default?.() },
);
// ButtonGroup member: no wrapper element, so the trigger button is the
// group's positional child and the shared seam rules apply unchanged.
if (props.__button_group_child) return menu;
return h(
"div",
{
@@ -89,6 +84,7 @@ export default defineComponent({
class: [slotsComputed.value.root(), attrs.class],
"data-slot": "dropdown-root",
"data-trigger": props.trigger,
"data-button-group-child": props.__button_group_child || undefined,
},
[menu],
);
+103 -7
View File
@@ -606,7 +606,7 @@ describe("ButtonGroup fullWidth inheritance", () => {
});
describe("ButtonGroup dropdown composition", () => {
it("keeps the dropdown trigger positional for the shared seam styles", () => {
it("marks the positional dropdown wrapper without leaking the private prop", () => {
const w = mount(
defineComponent({
render: () =>
@@ -626,13 +626,12 @@ describe("ButtonGroup dropdown composition", () => {
);
const group = w.get('[data-slot="button-group"]');
const trigger = w.get('button[aria-label="更多选项"]');
// The group seam CSS keys off positional children and upstream Dropdown
// has no DOM root: the trigger must sit directly in the group, with no
// wrapper in between and no internal marker leaked onto the DOM.
expect(trigger.element.parentElement).toBe(group.element);
expect(trigger.element.matches(":last-child")).toBe(true);
const root = w.get('[data-slot="dropdown-root"]');
expect(root.element.parentElement).toBe(group.element);
expect(root.element.matches(":last-child")).toBe(true);
expect(root.attributes("data-button-group-child")).toBe("true");
expect(trigger.element.parentElement).toBe(root.element);
expect(w.html()).not.toContain("__button_group_child");
expect(w.find('[data-slot="dropdown-root"]').exists()).toBe(false);
w.unmount();
});
@@ -646,6 +645,103 @@ describe("ButtonGroup dropdown composition", () => {
const root = w.get('[data-slot="dropdown-root"]');
expect(root.classes()).toContain("custom-root");
expect(root.attributes("data-trigger")).toBe("press");
expect(root.attributes("data-button-group-child")).toBeUndefined();
w.unmount();
});
it("preserves reactive root styles, identity and accessible attributes in a group", async () => {
const visible = ref(false);
const w = mount(
defineComponent({
render: () =>
h(ButtonGroup, () => [
h(
DropdownRoot,
{
id: "consumer-menu",
class: visible.value ? "visible-root" : "hidden-root",
style: { display: visible.value ? "flex" : "none" },
"data-consumer-root": visible.value ? "shown" : "hidden",
"aria-label": "菜单操作区",
},
() => h(DropdownTrigger, null, () => h(Button, () => "操作")),
),
]),
}),
);
const root = w.get("#consumer-menu");
expect(root.classes()).toContain("hidden-root");
expect((root.element as HTMLElement).style.display).toBe("none");
expect(root.attributes("data-consumer-root")).toBe("hidden");
expect(root.attributes("aria-label")).toBe("菜单操作区");
visible.value = true;
await nextTick();
expect(root.classes()).toContain("visible-root");
expect(root.classes()).not.toContain("hidden-root");
expect((root.element as HTMLElement).style.display).toBe("flex");
expect(root.attributes("data-consumer-root")).toBe("shown");
w.unmount();
});
it("keeps root capture cancellation for click, pointer and keyboard activation", async () => {
const cancel = ref(true);
const click = vi.fn((event: MouseEvent) => {
if (cancel.value) event.preventDefault();
});
const pointer = vi.fn((event: PointerEvent) => event.preventDefault());
const key = vi.fn((event: KeyboardEvent) => event.preventDefault());
const open = vi.fn();
const w = mount(
defineComponent({
render: () =>
h(ButtonGroup, () => [
h(
DropdownRoot,
{
onClickCapture: click,
onPointerdownCapture: pointer,
onKeydownCapture: key,
"onUpdate:open": open,
},
() => h(DropdownTrigger, null, () => h(Button, () => "操作")),
),
]),
}),
);
const button = w.get("button");
button.element.dispatchEvent(
new MouseEvent("click", { bubbles: true, cancelable: true }),
);
button.element.dispatchEvent(
new MouseEvent("pointerdown", {
bubbles: true,
cancelable: true,
button: 0,
ctrlKey: false,
}),
);
button.element.dispatchEvent(
new KeyboardEvent("keydown", {
bubbles: true,
cancelable: true,
key: "Enter",
}),
);
await nextTick();
expect(click).toHaveBeenCalledTimes(1);
expect(pointer).toHaveBeenCalledTimes(1);
expect(key).toHaveBeenCalledTimes(1);
expect(open).not.toHaveBeenCalled();
expect(button.attributes("aria-expanded")).toBe("false");
cancel.value = false;
button.element.dispatchEvent(
new MouseEvent("click", { bubbles: true, cancelable: true }),
);
await nextTick();
expect(click).toHaveBeenCalledTimes(2);
expect(open).toHaveBeenCalledTimes(1);
expect(open).toHaveBeenCalledWith(true);
expect(button.attributes("aria-expanded")).toBe("true");
w.unmount();
});
});
+74
View File
@@ -0,0 +1,74 @@
<script setup lang="ts">
import { reactive, ref } from "vue";
import {
Button,
ButtonGroup,
DropdownRoot,
DropdownTrigger,
DropdownPopover,
DropdownMenu,
DropdownItem,
} from "@heroui-vue/components";
const cancel = ref(true);
const hidden = ref(false);
const counts = reactive({ click: 0, pointerdown: 0, keydown: 0 });
const cases = [
{ orientation: "horizontal" as const, dir: "ltr" as const },
{ orientation: "horizontal" as const, dir: "rtl" as const },
{ orientation: "vertical" as const, dir: "ltr" as const },
];
const positions = ["first", "middle", "last"];
function capture(event: Event) {
counts[event.type as keyof typeof counts]++;
if (cancel.value) event.preventDefault();
}
</script>
<template>
<main class="p-6 space-y-6">
<h1>ButtonGroup / Dropdown 根属性与接缝</h1>
<label>
<input v-model="cancel" type="checkbox" />
取消打开
</label>
<label>
<input v-model="hidden" type="checkbox" />
隐藏根容器
</label>
<output aria-label="捕获事件次数">{{ JSON.stringify(counts) }}</output>
<section v-for="item in cases" :key="item.orientation + item.dir">
<h2>{{ item.orientation }} / {{ item.dir }}</h2>
<ButtonGroup
:orientation="item.orientation"
:dir="item.dir"
:data-case="item.orientation + '-' + item.dir"
>
<DropdownRoot
v-for="position in positions"
:id="item.orientation + '-' + item.dir + '-' + position"
:key="position"
class="consumer-root"
:data-position="position"
:style="{
display: hidden ? 'none' : 'flex',
'--consumer-marker': 'kept',
}"
:aria-label="position + '菜单容器'"
@click.capture="capture"
@pointerdown.capture="capture"
@keydown.capture="capture"
>
<DropdownTrigger>
<Button variant="outline">{{ position }}</Button>
</DropdownTrigger>
<DropdownPopover>
<DropdownMenu>
<DropdownItem id="action">执行操作</DropdownItem>
</DropdownMenu>
</DropdownPopover>
</DropdownRoot>
</ButtonGroup>
</section>
</main>
</template>
+10 -1
View File
@@ -36,6 +36,7 @@
"MenuSection"
],
"css": [
"data-button-group-child",
"hv-menu-item",
"hv-menu-item__indicator--submenu",
"hv-dropdown__popover",
@@ -570,7 +571,15 @@
]
},
"form": {
"exports": ["Form", "FormRoot", "TextField", "Input", "Label", "FieldError", "Button"],
"exports": [
"Form",
"FormRoot",
"TextField",
"Input",
"Label",
"FieldError",
"Button"
],
"css": [".hv-textfield", ".hv-input", ".hv-field-error", ".hv-button"]
},
"api-root-aliases": {