Files
heroui-vue/docs/migrations/M01-M03-surface-link-spinner.md
T
2026-10-21 13:04:06 +08:00

218 lines
21 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# M01–M03 迁移报告:Surface、Link、Spinner
日期:2026-10-21
起始提交:`9d40dc0`(工作区干净,分支 `main`)
上游基线:HeroUI 3.2.6 / `ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960`
实现目录:`heroui-vue`(未改动 `../heroui`)
## 1. 修改范围
### 新增
| 路径 | 用途 |
| --------------------------------------------------------------------- | ---------------------------- |
| `packages/styles/components/{surface,link,spinner,textarea}.css` | 基线 CSS,hv- 前缀 |
| `packages/vue/src/components/surface/{Surface.vue,context.ts}` | Surface 与变体上下文 |
| `packages/vue/src/components/link/{Link.vue,LinkIcon.vue,context.ts}` | Link 与默认图标 |
| `packages/vue/src/components/spinner/Spinner.vue` | Spinner(含基线 SVG) |
| `packages/vue/src/components/textarea/TextArea.vue` | TextArea(Surface 演示依赖) |
| `apps/docs/demos/surface-*.vue`(4) | 基线演示 |
| `apps/docs/demos/link-*.vue`(6) | 文档引用演示 |
| `apps/docs/demos/spinner-*.vue`(5) | 基线演示 |
| `apps/docs/components/{surface,link,spinner}.md` | 中文文档 |
| `tests/surface-link-spinner.test.ts` | 行为测试(14 项) |
| `docs/migrations/M01-M03-surface-link-spinner.md` | 本报告 |
### 修改(含共享入口)
| 路径 | 变更 |
| ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| `packages/styles/src/index.ts` | 增加 surface/link/spinner/textarea 变体 |
| `packages/styles/index.css` | 导入新 CSS;移除临时 `.hv-spinner` 圆环样式 |
| `packages/vue/src/{index.ts,types.ts}` | 导出与类型 |
| `packages/vue/src/components/button/Button.vue` | 加载指示器改用 Spinner |
| `apps/docs/.vitepress/theme/Layout.vue` | 导航加入 Surface/Link/Spinner |
| `apps/docs/.vitepress/theme/style.css` | `a { text-decoration: none }` 移入 `@layer base`(修复 utility 覆盖) |
| `apps/docs/components.md` | 组件索引 |
| `apps/docs/demos/{input-on-surface,card-default,card-with-form,card-with-images,button-loading,button-loading-state}.vue` | 替换临时适配 |
| `tests/{aligned-components.test.ts,ssr.test.ts}` | 清单与 SSR 覆盖 |
| `package.json` | `test:unit` 加入新测试文件 |
| `docs/{compatibility,upstream,verification}.md` | 状态与来源 |
未改锁文件与无关组件逻辑。
## 2. 上游来源
- Surface:`packages/react/src/components/surface/surface.tsx`、`packages/styles/components/surface.css`、`cn/react/components/(layout)/surface.mdx`、`demos/cn/surface/{basic,variants,custom-styles,with-form-components}`
- Link:`…/link/link.tsx`、`link.css`、`(navigation)/link.mdx`、`demos/cn/link/{basic,icon-placement,underline-and-offset,custom-icon,render-function,custom-styles}`
- Spinner:`…/spinner/spinner.tsx`、`spinner.css`、`(feedback)/spinner.mdx`、`demos/cn/spinner/{basic,colors,sizes,speed,custom-styles}`
- Link 默认图标:`packages/react/src/components/icons.tsx` → `ExternalLinkIcon`
- Spinner SVG:`SpinnerPrimitive` 渐变路径
- TextArea(演示依赖):`packages/react/src/components/textarea/textarea.tsx`、`textarea.css`
上游 `underline-offset.tsx`、`underline-variants.tsx` 未出现在中英文文档 `ComponentPreview` 清单,本批未迁移。许可:Apache-2.0(见 `third-party/HEROUI-LICENSE`)。
## 3. API 对照
### Surface
| 上游 | Vue | 默认值 / 差异 |
| ------------------------- | ------------------------------ | -------------------------------------------------------------- |
| `variant` | `variant` | `"default"`;`transparent \| default \| secondary \| tertiary` |
| `className` | `class` | — |
| `children` | 默认插槽 | — |
| `SurfaceContext` | `surfaceContextKey` + `inject` | 读取 `variant`(ComputedRef) |
| `Surface` / `SurfaceRoot` | `Surface` | 命名导出,无 `Surface.Root` 点语法 |
| `data-slot="surface"` | 同左 | — |
### Link
| 上游 | Vue | 默认值 / 差异 |
| -------------------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `href` / `target` / `rel` / `download` | 同名 | `target` 默认 `"_self"` |
| `isDisabled` | `isDisabled` | `false`;`aria-disabled`;包装 attrs 的 click/keydown(含 capture)在禁用时 `preventDefault` + `stopImmediatePropagation`,调用方监听不触发;保留 href |
| `onPress` (PressEvent) | `@click` | **不等价**;原生激活路径 |
| `render` | `asChild` | 合并到默认插槽唯一元素 |
| `autoFocus` | `autoFocus` | 挂载聚焦 |
| `className` | `class` | — |
| `Link.Icon` | `LinkIcon` | 默认 ExternalLinkIcon;有插槽内容时去掉 `data-default-icon` |
| RAC 状态 props | 作用域插槽 | `isHovered/isPressed/isFocused/isFocusVisible/isDisabled` |
| React Router 示例 | `linkVariants()` + `RouterLink` | 基础包不依赖 vue-router |
### Spinner
| 上游 | Vue | 默认值 / 差异 |
| -------------------------------------- | -------- | ------------------------------------------------------- |
| `size` | `size` | `"md"`;`sm\|md\|lg\|xl` → 16/24/32/40px |
| `color` | `color` | `"accent"`;`current\|accent\|success\|warning\|danger` |
| `className` | `class` | — |
| `role="status"` `aria-label="Loading"` | 默认同左 | 可通过 attrs 覆盖 |
| `SpinnerPrimitive` SVG | 内置 SVG | 渐变 id 使用 `useId` 保证唯一 |
### TextArea(演示依赖)
`modelValue`/`update:modelValue`、`defaultValue`、`variant`(primary/secondary)、`fullWidth`、`invalid`;与 Input 相同的表单 reset 行为。未做 TextField 上下文。
## 4. 示例对照
| 组件 | 基线顺序 | 缺失 |
| ------- | --------------------------------------------------------------------------------------------- | ----- |
| Surface | basic → variants → with-form-components → custom-styles | 0 / 4 |
| Link | basic → icon-placement → underline-and-offset → custom-icon → render-function → custom-styles | 0 / 6 |
| Spinner | basic → colors → sizes → speed → custom-styles | 0 / 5 |
文案、结构与基线一致。替换项:
1. Link `custom-icon`:`@gravity-ui/icons` → Tabler `IconExternalLink` / `IconLink`。
2. Link `render-function`:`render={(props)=>…}` → `asChild` + `span[data-custom=foo]`。
3. Surface `with-form-components`:依赖 TextArea(本批最小实现)。
4. Button 加载演示:Tabler IconLoader2 → `<Spinner color="current" size="sm" />`(对齐上游 CN)。
5. Card 三处原生链接 → Link/LinkIcon。
6. Input 表面演示 → Surface 容器。
## 5. 命令结果
| 命令 | 结果 |
| ----------------------------------------------- | --------------------------------------- |
| `pnpm test` | 7 文件 / 51 项通过 |
| `pnpm typecheck` | 通过 |
| `pnpm build:docs` | 通过(styles + components + VitePress) |
| `pnpm format` | 通过 |
| `pnpm build:playground` | **未执行** |
| `test:browser` / `test:visual` / `test:package` | **未建立** |
新增测试位于 `tests/surface-link-spinner.test.ts`,已纳入 `test:unit`。
## 6. 浏览器证据
地址 `http://127.0.0.1:5173`,IAB,桌面视口;主题通过文档主题切换检查浅/深。
- `/components/surface`:4 例齐全;变体背景色与深色主题变量见 verification.md;表单例 Input/TextArea secondary。
- `/components/link`:underline / no-underline / offset 1–4 计算值;默认与自定义图标;asChild=`span[data-custom=foo]`。
- `/components/spinner`:四档尺寸、五色、role/aria、SVG viewBox。
- `/components/button`:加载 Spinner 16px,`data-pending`/`aria-busy`。
- `/components/card`:创作者中心、前往设置、忘记密码均为 `hv-link`。
IAB 截图接口超时,本轮无位图;证据为 DOM 与计算样式。
## 7. 已知差异与未验证项
- `onPress`/`PressEvent`、RAC `render` 类型、RouterProvider 不等价。
- 上游 2 个未引用演示文件未迁移。
- TextArea 未做完整 TextField/Form 验收。
- 未验证:SSR 水合、Nuxt、RTL、触屏、读屏、独立安装 dist、自动视觉对比、减少动态效果系统偏好实测(仅保证 `motion-reduce` 类存在)。
- 不宣称与 HeroUI 完整兼容。
## 8. 集成人复核步骤
1. `pnpm test && pnpm typecheck && pnpm build:docs`。
2. 打开 `http://127.0.0.1:5173/components/surface`,确认 4 例及 secondary 表单。
3. `/components/link`:展开各例源码;Tab 应显示焦点环;自定义图标为 Tabler。
4. `/components/spinner`:尺寸 16/24/32/40;Button 页加载例 Spinner 为 16px。
5. Card 页确认链接为 Link;检查浅/深主题下表面与链接色。
6. 对照本报告 API 表与 `docs/compatibility.md` 行状态。
## 9. 共享文件改动(需集成人确认)
- `packages/vue/src/index.ts`、`types.ts`
- `packages/styles/src/index.ts`、`index.css`
- `apps/docs/.vitepress/theme/{Layout.vue,style.css}`
- `package.json`(`test:unit`)
- `docs/compatibility.md`、`upstream.md`、`verification.md`
## 10. 独立复核与修正(2026-10-21)
独立 AI 复核结论为「待修正」,已按报告处理:
| 问题 | 处理 |
| --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1 中:禁用 Link 未阻断父级 `@keydown`(attrs 监听先于组件守卫) | **已修**:`Link.vue` 将 `onClick`/`onClickCapture`/`onKeydown`/`onKeydownCapture`/`onKeyup` 包装为合并 attrs,禁用时先 `preventDefault`+`stopImmediatePropagation` 再不调用调用方;补测 keydown Enter/Space 与 `onClickCapture` |
| 2 低-中:`@click.capture` 泄漏 | **已修**:同上包装 `onClickCapture` |
| 3 低:Button 自动 Spinner 仅 `loading` | **不改行为**:文档已写明 `isPending` 不自动插入图标、`loading` 别名才自动显示;与上游 render-prop 手放 Spinner 一致 |
| 4 信息:Link 文档「直接应用类」缺段 | **已补**:路由 `slots.icon()`、RouterLink `hv-link`、原生 a + `hv-link__icon` 三例 |
复测:`pnpm test` 52 项通过(新增禁用 keydown/capture 回归);`pnpm typecheck` 通过。
## 11. 二次复核返工(2026-10-21)
集成复核报告 `docs/migrations/M01-M03-review.md` 提出 3 个 Link 问题,处理如下:
| 问题 | 处理 |
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| P1 `autoFocus` 把 Primitive 实例当 DOM | **已修**:`:ref="setRoot"` 解析 `HTMLElement` / 实例 `$el`;`defineExpose({ element, focus })` 指向真实 DOM;普通锚点与 asChild 均可聚焦 |
| P2 `asChild` span 无链接行为 | **已修**:asChild 根补充 `role="link"`、`tabindex="0"`;Enter 对非原生可激活元素合成一次 `click`(与鼠标同一路径,不重复触发 a/button);文档写明 span 上的 href 不自动导航,真实导航用 `a`/RouterLink |
| P2 监听器数组抛错 | **已修**:`callHandlers` 兼容函数与函数数组;禁用时数组内处理器均不调用 |
| 附带 | Vue 将 `download?: boolean` 缺省转为 `false` 并渲染 `download="false"`;改为仅在 true/字符串时输出 download 属性 |
回归:新增 6 项(监听器数组、禁用数组、autoFocus 锚点/asChild、asChild 语义与 Enter、download 属性),`pnpm test` 共 **58** 项通过;连跑 3 次稳定。
## 12. 三次复核返工(2026-10-21)
集成复核 `docs/migrations/M01-M03-review.md` 第二次结论后,按 3 个问题修正:
| 问题 | 处理 |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| P1 Enter 重复激活 | **已修**:仅在 keydown **bubble** 合成一次激活;keyup 与 capture 只做禁用拦截,不再合成。完整 Enter 按下/松开只触发 1 次 click 语义 |
| P2 无法取消键盘激活 | **已修**:先执行调用方 keydown(含 capture),若 `preventDefault` 或 `stopImmediatePropagation` 则不合成;取消在 capture/bubble 均有效 |
| P2 数组丢失中断语义 | **已修**:`callHandlers` 包装 `stopImmediatePropagation`,数组内后续处理器不再执行 |
附带根因:嵌套 `el.click()` 在 Primitive/Slot 下 Vue 的 bubble `onClick` 可能不触发;键盘激活改为**直接按 click 顺序调用** `onClickCapture` → `onClick`(与 RAC onPress 类似),可被 keydown 取消,且不依赖嵌套 DOM click。事件处理器在触发时从 `attrs` 读取,避免 computed 快照丢失。
回归:完整按键序列、preventDefault/capture 取消、数组 `stopImmediatePropagation`。`pnpm test` **63** 项通过(连跑 10 次);typecheck、format、build:docs 通过。
## 13. 四次复核返工:真实 DOM click(2026-10-21)
集成复核指出键盘激活不能直接调用回调。已改为**真实派发 click**:
| 问题 | 处理 |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| 插槽/祖先收不到 click | `el.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }))`,插槽 `@click`、祖先 capture/bubble 均可达 |
| target/currentTarget 为 null | 真实事件派发,二者均为触发元素 |
| capture `preventDefault` 误当停传 | 不再在合成路径上把 `defaultPrevented` 当停止传播;原生事件语义保持 |
**根因**:Vue `createInvoker` 在 `e._vts <= invoker.attached` 时跳过处理器;同一同步轮次创建的 invoker 共用 `getNow()` 时间戳。嵌套/合成 click 时先到的 Vue capture 设置 `_vts` 后,同毫秒的 bubble/插槽监听器被跳过。派发前将 `_vts` 设为 `Date.now() + 1`,保证所有 Vue 监听器执行。
回归:完整 Enter 仅一次激活;keydown/capture 可取消;target/currentTarget;插槽与祖先 click;capture 仅 `preventDefault` 不阻断 bubble;原生 `a` 不重复激活;数组 `stopImmediatePropagation`。
`pnpm test` **67** 项通过(文件 20 次 + 全量 15 次稳定);typecheck、format、build:docs 通过。