fix: complete Avatar image composition and fallback migration
This commit is contained in:
@@ -110,7 +110,7 @@ Avatar 组件使用以下 CSS 类:
|
|||||||
|
|
||||||
## API 参考
|
## API 参考
|
||||||
|
|
||||||
### Avatar
|
### Avatar / AvatarRoot
|
||||||
|
|
||||||
| Prop | 类型 | 默认值 | 描述 |
|
| Prop | 类型 | 默认值 | 描述 |
|
||||||
| --------- | ------------------------------------------------------------- | ----------- | ----------------- |
|
| --------- | ------------------------------------------------------------- | ----------- | ----------------- |
|
||||||
@@ -123,7 +123,7 @@ Avatar 组件使用以下 CSS 类:
|
|||||||
|
|
||||||
| Prop | 类型 | 默认值 | 描述 |
|
| Prop | 类型 | 默认值 | 描述 |
|
||||||
| ------------- | ---------------------------------- | ------- | ------------------------------------------ |
|
| ------------- | ---------------------------------- | ------- | ------------------------------------------ |
|
||||||
| `src` | `string` | - | 图片 URL |
|
| `src` | `string` | - | 可选图片 URL,省略时显示回退 |
|
||||||
| `srcSet` | `string` | - | 响应式图片的 `srcset` 属性 |
|
| `srcSet` | `string` | - | 响应式图片的 `srcset` 属性 |
|
||||||
| `sizes` | `string` | - | 响应式图片的 `sizes` 属性 |
|
| `sizes` | `string` | - | 响应式图片的 `sizes` 属性 |
|
||||||
| `alt` | `string` | - | 图片替代文本 |
|
| `alt` | `string` | - | 图片替代文本 |
|
||||||
@@ -138,10 +138,14 @@ Avatar 组件使用以下 CSS 类:
|
|||||||
|
|
||||||
| Prop | 类型 | 默认值 | 描述 |
|
| Prop | 类型 | 默认值 | 描述 |
|
||||||
| --------- | ------------------------------------------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------- |
|
| --------- | ------------------------------------------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------- |
|
||||||
| `delayMs` | `number` | - | 显示 fallback 前的延迟(防止闪烁)。`0` 与未指定不同:底座在 `delayMs=0` 时不会显示 fallback,需要延迟时请传正数 |
|
| `delayMs` | `number` | - | 显示 fallback 前的延迟(毫秒);省略或 `0` 时立即显示,正数延迟可动态更新 |
|
||||||
| `color` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | - | 覆盖父级颜色(替换而非叠加) |
|
| `color` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | - | 覆盖父级颜色(替换而非叠加) |
|
||||||
| `class` | `string` | - | 附加 CSS 类 |
|
| `class` | `string` | - | 附加 CSS 类 |
|
||||||
|
|
||||||
|
三个部件均支持 `as`(根/回退默认 `span`,图片默认 `img`)与 `asChild`(默认 `false`)。`asChild` 需要单个可接收属性的元素或组件;自定义组件应将 attrs 和事件透传到实际 DOM。Image 的默认插槽用于提供自定义图片,Root / Fallback 的默认插槽分别提供部件与回退内容。`referrerPolicy` 接受原生图片策略并透传。
|
||||||
|
|
||||||
|
`AvatarRoot` 是 `Avatar` 的同一组件别名。React ref 改用 Vue 模板 ref 的 `$el` 获取根 DOM;不提供框架专属合成事件。图片加载由底座预加载 `src` 来决定显示状态,`srcSet` / `sizes` 传给实际图片,预加载探测不按响应式候选选择图片;加载中的实际 img 保留在 DOM 并以 `display:none` 隐藏,这是与上游 Radix 按加载状态挂载图片的差异。未提供 `src` 时仅有 `srcSet` 不会令图片显示。
|
||||||
|
|
||||||
Avatar 使用 Reka UI 的 `AvatarRoot` / `AvatarImage` / `AvatarFallback` 处理加载与回退状态。`onLoad` / `onError` 对应 Vue 原生 `@load` / `@error` 事件,不声明与 React 合成事件完全等价。
|
Avatar 使用 Reka UI 的 `AvatarRoot` / `AvatarImage` / `AvatarFallback` 处理加载与回退状态。`onLoad` / `onError` 对应 Vue 原生 `@load` / `@error` 事件,不声明与 React 合成事件完全等价。
|
||||||
|
|
||||||
## 相关组件
|
## 相关组件
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
| accordion | 验证中(M23) | Accordion/Item/Heading/Trigger/Panel/Body/Indicator;10/10 基线例;surface/hideSeparator;复用组键协议 |
|
| accordion | 验证中(M23) | Accordion/Item/Heading/Trigger/Panel/Body/Indicator;10/10 基线例;surface/hideSeparator;复用组键协议 |
|
||||||
| alert-dialog | 验证中(M33) | 11 部件;role=alertdialog;默认不误关;14/14 基线例;Reka Dialog 底座 |
|
| alert-dialog | 验证中(M33) | 11 部件;role=alertdialog;默认不误关;14/14 基线例;Reka Dialog 底座 |
|
||||||
| autocomplete | 已迁移(有已知差异) | Autocomplete 七部件;24/24 基线例;触发器单一 Tab 入口 + 弹层内搜索框虚拟焦点、受控开关/文本、多选 Value 用作用域插槽;见 B02 报告 |
|
| autocomplete | 已迁移(有已知差异) | Autocomplete 七部件;24/24 基线例;触发器单一 Tab 入口 + 弹层内搜索框虚拟焦点、受控开关/文本、多选 Value 用作用域插槽;见 B02 报告 |
|
||||||
| avatar | 验证中(最小实现) | Reka Avatar 加载/回退;size/color/variant;Image asChild;AvatarGroup 继承仅限直接子级 |
|
| avatar | 已迁移(客户端,有已知差异) | Avatar/AvatarRoot、Image/Fallback;7/7 基线例;as/asChild、可选src、原生load/error、零/正延迟与动态更新、换图恢复;Reka预加载src与隐藏DOM差异见AVATAR结果;AvatarGroup继承仅限直接子级 |
|
||||||
| avatar-group | 验证中(最小实现) | max 截断与自动计数、显式 Count 不受截断、isGrid、overlap clip/ring;默认无 role=group |
|
| avatar-group | 验证中(最小实现) | max 截断与自动计数、显式 Count 不受截断、isGrid、overlap clip/ring;默认无 role=group |
|
||||||
| badge | 验证中(最小实现) | Badge/BadgeAnchor/BadgeLabel;四角定位、圆点、文本/数字自动包装;Anchor 非链接 |
|
| badge | 验证中(最小实现) | Badge/BadgeAnchor/BadgeLabel;四角定位、圆点、文本/数字自动包装;Anchor 非链接 |
|
||||||
| breadcrumbs | 验证中(M17) | Breadcrumbs/BreadcrumbsItem;7/7 基线例;ol+aria-label、末项 current、禁用/无 href span、separator 插槽;onAction 未提供,class 同时落 li/link |
|
| breadcrumbs | 验证中(M17) | Breadcrumbs/BreadcrumbsItem;7/7 基线例;ol+aria-label、末项 current、禁用/无 href span、separator 插槽;onAction 未提供,class 同时落 li/link |
|
||||||
|
|||||||
@@ -76,3 +76,7 @@ ColorPicker/ColorPickerRoot、ColorPickerTrigger、ColorPickerPopover、Props/
|
|||||||
差异:固定源 `setColor(null)` 会把接受值改写为 `#000000`,本库组合拒绝空提案并保持接受颜色(共享颜色模型既有决策,独立字段仍可清空);固定源 Trigger 复用 Button render props,本库插槽状态收敛为 `color`/`isDisabled`;固定源 Root 经 `DialogTrigger` 内部管理展开,`open`/`isOpen`/`defaultOpen` 受控展开与 `name`/`form` 单值完整精度提交、`isDisabled`/`isReadOnly` 均为 Vue 扩展。弹层默认 `bottom left`,RTL 经 ConfigProvider `dir` 镜像为 `end` 对齐;轴名/输出等本地化文案跟随 ConfigProvider locale(默认 en-US),与其它颜色组件一致。
|
差异:固定源 `setColor(null)` 会把接受值改写为 `#000000`,本库组合拒绝空提案并保持接受颜色(共享颜色模型既有决策,独立字段仍可清空);固定源 Trigger 复用 Button render props,本库插槽状态收敛为 `color`/`isDisabled`;固定源 Root 经 `DialogTrigger` 内部管理展开,`open`/`isOpen`/`defaultOpen` 受控展开与 `name`/`form` 单值完整精度提交、`isDisabled`/`isReadOnly` 均为 Vue 扩展。弹层默认 `bottom left`,RTL 经 ConfigProvider `dir` 镜像为 `end` 对齐;轴名/输出等本地化文案跟随 ConfigProvider locale(默认 en-US),与其它颜色组件一致。
|
||||||
|
|
||||||
本项由主会话亲自实施/自测,未委派其他 AI,未宣称独立复核。SSR/水合/Nuxt 服务端、逐像素对比、完整读屏矩阵与真机矩阵未投入;截图需要可见桌面窗口、CDP 鼠标注入在本轮后半程持续超时,交互证据以真实键盘事件、真实拖动事件与真实浏览器 DOM 指针事件序列(isTrusted=false)加实际几何/计算样式为准。
|
本项由主会话亲自实施/自测,未委派其他 AI,未宣称独立复核。SSR/水合/Nuxt 服务端、逐像素对比、完整读屏矩阵与真机矩阵未投入;截图需要可见桌面窗口、CDP 鼠标注入在本轮后半程持续超时,交互证据以真实键盘事件、真实拖动事件与真实浏览器 DOM 指针事件序列(isTrusted=false)加实际几何/计算样式为准。
|
||||||
|
|
||||||
|
## AVATAR(2026-10-27)
|
||||||
|
|
||||||
|
Avatar/AvatarRoot、Image/Fallback 已完成公共客户端迁移补齐,见 [结果](tasks/results/AVATAR.md)。修复Image asChild丢失默认插槽与delayMs=0永久隐藏;可选src、三部件as/asChild、原生事件类型和AvatarRootProps公开。加载状态仍由Reka持有,回退延迟使用本库定时器以处理零值/动态切换与卸载取消。实际img在加载中隐藏而不卸载,srcSet/sizes仅透传实际图片、预加载仍只探测src;与固定源差异已在文档记录。既有AvatarGroup直接子级继承不变。
|
||||||
|
|||||||
@@ -36,3 +36,5 @@ TIME-FIELD 已完成公共迁移和客户端验收,见 [结果](tasks/results/
|
|||||||
ColorSwatchPicker 最终修复 `3c8473d`,已合入 main(`457d340`),原复核阻断全部关闭;最终全量客户端105文件/1558项、类型、文档构建、五契约tarball及main集成回归通过。推送已获授权,远端同步以实际Git状态为准,详见任务结果。
|
ColorSwatchPicker 最终修复 `3c8473d`,已合入 main(`457d340`),原复核阻断全部关闭;最终全量客户端105文件/1558项、类型、文档构建、五契约tarball及main集成回归通过。推送已获授权,远端同步以实际Git状态为准,详见任务结果。
|
||||||
|
|
||||||
ColorPicker 独立复核修复 `74cd597`,已合入 main(`23eecaf`);main 集成全量107文件/1571项、类型、文档构建和十契约最终tarball通过。补验390px窄屏与焦点恢复,原公开差异保留;远端同步以实际Git状态为准。
|
ColorPicker 独立复核修复 `74cd597`,已合入 main(`23eecaf`);main 集成全量107文件/1571项、类型、文档构建和十契约最终tarball通过。补验390px窄屏与焦点恢复,原公开差异保留;远端同步以实际Git状态为准。
|
||||||
|
|
||||||
|
Avatar 头像已由主会话亲自补齐迁移,未委派其他AI;自定义图片插槽、零延迟回退、可选src、原生事件类型与Root别名已修正。7/7固定源例核对、加载回归及真实浏览器范围见 [AVATAR结果](tasks/results/AVATAR.md)。
|
||||||
|
|||||||
+6
-10
@@ -1,12 +1,8 @@
|
|||||||
# 当前工作区交接
|
# 当前工作区交接
|
||||||
|
|
||||||
- 任务:COLOR-PICKER 独立复核通过,状态 `accepted`;已按用户授权合 main,待本记录提交后 push。
|
- 任务:AVATAR,主会话亲自补齐迁移;不委派其他AI。
|
||||||
- 工作区/分支:heroui-vue / `main`;复核修复 `74cd597`,合并提交 `23eecaffef45714409e16db575c3912fac7fa189`;main 集成全量107文件/1571项通过。
|
- 工作区/分支:heroui-vue / `feat/avatar-complete`。
|
||||||
- 复核对象:`63606399e374ac31c4c75149790c9df96af5ebfe`;起点 `d75540c5be53336b09a3a0cf3ac179990b9e5523`,实施 `ee5fce9`。
|
- 起点:`6de246de49cfdb0ce8c8770de9049d6e7644e4ed`;先前main `c05cc7d`,无未提交改动。
|
||||||
- 复核修正:颜色提案事件文档;既有 ColorField 焦点测试时间戳隔离。结果表 R1/R2 均关闭。
|
- 用户授权:完成验证后提交并合main;未授权本轮push。
|
||||||
- 验证:全量107文件/1571项、typecheck、build:docs、十契约最终tarball通过;独立浏览器 basic/嵌套Select/焦点恢复/390px窄屏通过。
|
- 入口:`docs/tasks/AVATAR.md`。
|
||||||
- 入口:`docs/tasks/COLOR-PICKER.md` 与 `docs/tasks/results/COLOR-PICKER.md`。
|
- 保留已有5173文档服务;单目录串行。
|
||||||
- 预览:保留已有文档5173,页面 `/components/color-picker`,未停止他人服务。
|
|
||||||
- 接续:当前无新实施任务;合并与远端同步以最终 Git SHA 回读为准。
|
|
||||||
|
|
||||||
单目录串行;后续先检查实际git状态,不从旧历史自动开始新工作。
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
"pnpm exec vitest run tests/close-button-avatar-skeleton.test.ts tests/avatar-loading.test.ts",
|
"pnpm exec vitest run tests/close-button-avatar-skeleton.test.ts tests/avatar-loading.test.ts",
|
||||||
"pnpm typecheck",
|
"pnpm typecheck",
|
||||||
"pnpm build:docs",
|
"pnpm build:docs",
|
||||||
"node scripts/check-package.mjs avatar --offline",
|
"node scripts/check-package.mjs avatar",
|
||||||
"pnpm test"
|
"pnpm test"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
| 卡 | 用途 |
|
| 卡 | 用途 |
|
||||||
| ----------------------------------------- | ----------------------------------------------------------- |
|
| ----------------------------------------- | ----------------------------------------------------------- |
|
||||||
|
| [AVATAR](AVATAR.md) | 主会话亲自补齐头像三部件、7例与加载回退验收 |
|
||||||
| [COLOR-PICKER](COLOR-PICKER.md) | 公共可组合颜色选择器迁移,含固定源 6 例、共享颜色与嵌套弹层验收 |
|
| [COLOR-PICKER](COLOR-PICKER.md) | 公共可组合颜色选择器迁移,含固定源 6 例、共享颜色与嵌套弹层验收 |
|
||||||
| [COLOR-SWATCH-PICKER](COLOR-SWATCH-PICKER.md) | 公共色块选择集合迁移,含固定源 10 例与产物验收 |
|
| [COLOR-SWATCH-PICKER](COLOR-SWATCH-PICKER.md) | 公共色块选择集合迁移,含固定源 10 例与产物验收 |
|
||||||
| [COLOR-SWATCH](COLOR-SWATCH.md) | 公共颜色值/Swatch家族 |
|
| [COLOR-SWATCH](COLOR-SWATCH.md) | 公共颜色值/Swatch家族 |
|
||||||
|
|||||||
@@ -320,3 +320,7 @@ CSS/tv 仅加 `hv-` 前缀与 Apache-2.0 来源,`size/variant/layout` 默认 `
|
|||||||
CSS/tv 仅加 `hv-` 前缀与 Apache-2.0 来源;`colorPickerVariants` 保持基线的 base/trigger/popover 三槽、无 size/variant。差异有记录的适配:`origin-(--trigger-anchor-point)` → `--reka-popper-transform-origin`,`data-placement^` 方向动画 → `data-side`,弹层 `max-height` 取 `--reka-popover-content-available-height`,退出动画保留 `pointer-events: none` 与 reduced-motion 停用。
|
CSS/tv 仅加 `hv-` 前缀与 Apache-2.0 来源;`colorPickerVariants` 保持基线的 base/trigger/popover 三槽、无 size/variant。差异有记录的适配:`origin-(--trigger-anchor-point)` → `--reka-popper-transform-origin`,`data-placement^` 方向动画 → `data-side`,弹层 `max-height` 取 `--reka-popover-content-available-height`,退出动画保留 `pointer-events: none` 与 reduced-motion 停用。
|
||||||
|
|
||||||
行为契约审计了固定依赖的官方发布源码(`react-aria-components@1.21.1` 的 ColorPicker 私有实现与类型声明、`react-stately` `useColorPickerState`),未安装或执行 React。要点:无值时默认 `#000000`(`props.defaultValue || '#000000'`)、受控 `null` 视为非受控、`setColor(null)` 改写为 `#000000`;本库共享 owner 拒绝空提案并保持接受颜色(差异已记录)。固定源 Root 经 `DialogTrigger` 内部管理展开,`open`/`isOpen`/`defaultOpen` 受控展开为 Vue 适配扩展;Trigger 的 Button render props 收敛为 `color`/`isDisabled` 插槽状态;`name`/`form` 单值提交与 `isDisabled`/`isReadOnly` 为 Vue 扩展,不冒充 RAC 原能力。来源与差异见 [任务结果](tasks/results/COLOR-PICKER.md)。
|
行为契约审计了固定依赖的官方发布源码(`react-aria-components@1.21.1` 的 ColorPicker 私有实现与类型声明、`react-stately` `useColorPickerState`),未安装或执行 React。要点:无值时默认 `#000000`(`props.defaultValue || '#000000'`)、受控 `null` 视为非受控、`setColor(null)` 改写为 `#000000`;本库共享 owner 拒绝空提案并保持接受颜色(差异已记录)。固定源 Root 经 `DialogTrigger` 内部管理展开,`open`/`isOpen`/`defaultOpen` 受控展开为 Vue 适配扩展;Trigger 的 Button render props 收敛为 `color`/`isDisabled` 插槽状态;`name`/`form` 单值提交与 `isDisabled`/`isReadOnly` 为 Vue 扩展,不冒充 RAC 原能力。来源与差异见 [任务结果](tasks/results/COLOR-PICKER.md)。
|
||||||
|
|
||||||
|
## AVATAR 迁移补齐(2026-10-27)
|
||||||
|
|
||||||
|
重新核对固定提交 `ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960` 的 `packages/react/src/components/avatar/{avatar.tsx,index.ts}`、Avatar CSS/variants、中文 `(media)/avatar.mdx` 与 `apps/docs/src/demos/cn/avatar` 全7例。保留既有来源/许可与hv命名空间、全部视觉数值;补Image默认插槽、可选src、Root别名/部件替换/原生事件类型及零延迟适配。仍用Reka处理图片状态,框架/预加载差异见组件文档与 [结果](tasks/results/AVATAR.md)。
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import { avatarContextKey, avatarGroupContextKey } from "./context";
|
|||||||
defineOptions({ inheritAttrs: false });
|
defineOptions({ inheritAttrs: false });
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
|
as?: string;
|
||||||
|
asChild?: boolean;
|
||||||
color?: AvatarVariants["color"];
|
color?: AvatarVariants["color"];
|
||||||
size?: AvatarVariants["size"];
|
size?: AvatarVariants["size"];
|
||||||
variant?: AvatarVariants["variant"];
|
variant?: AvatarVariants["variant"];
|
||||||
@@ -45,7 +47,13 @@ provide(avatarContextKey, { slots });
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<AvatarRoot v-bind="attrs" :class="slots.base()" :data-slot="dataSlot">
|
<AvatarRoot
|
||||||
|
v-bind="attrs"
|
||||||
|
:as="as"
|
||||||
|
:as-child="asChild"
|
||||||
|
:class="slots.base()"
|
||||||
|
:data-slot="dataSlot"
|
||||||
|
>
|
||||||
<slot />
|
<slot />
|
||||||
</AvatarRoot>
|
</AvatarRoot>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { inject, useAttrs } from "vue";
|
import { inject, ref, watch, useAttrs } from "vue";
|
||||||
import { AvatarFallback } from "reka-ui";
|
import { AvatarFallback } from "reka-ui";
|
||||||
import { avatarVariants, type AvatarVariants } from "@heroui-vue/styles";
|
import { avatarVariants, type AvatarVariants } from "@heroui-vue/styles";
|
||||||
import { avatarContextKey } from "./context";
|
import { avatarContextKey } from "./context";
|
||||||
@@ -7,10 +7,29 @@ import { avatarContextKey } from "./context";
|
|||||||
defineOptions({ inheritAttrs: false });
|
defineOptions({ inheritAttrs: false });
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
|
as?: string;
|
||||||
|
asChild?: boolean;
|
||||||
delayMs?: number;
|
delayMs?: number;
|
||||||
color?: AvatarVariants["color"];
|
color?: AvatarVariants["color"];
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
// Keep loading ownership in Reka; normalize the delay independently because
|
||||||
|
// its truthy delay check leaves delayMs=0 hidden forever.
|
||||||
|
const canRender = ref(false);
|
||||||
|
watch(
|
||||||
|
() => props.delayMs,
|
||||||
|
(delay, _, onCleanup) => {
|
||||||
|
canRender.value = delay === undefined || delay <= 0;
|
||||||
|
if (!canRender.value) {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
canRender.value = true;
|
||||||
|
}, delay);
|
||||||
|
onCleanup(() => clearTimeout(timer));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true },
|
||||||
|
);
|
||||||
|
|
||||||
const attrs = useAttrs();
|
const attrs = useAttrs();
|
||||||
const ctx = inject(avatarContextKey, undefined);
|
const ctx = inject(avatarContextKey, undefined);
|
||||||
|
|
||||||
@@ -36,8 +55,10 @@ function fallbackClass() {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<AvatarFallback
|
<AvatarFallback
|
||||||
|
v-if="canRender"
|
||||||
v-bind="restAttrs()"
|
v-bind="restAttrs()"
|
||||||
:delay-ms="delayMs"
|
:as="as"
|
||||||
|
:as-child="asChild"
|
||||||
:class="fallbackClass()"
|
:class="fallbackClass()"
|
||||||
data-slot="avatar-fallback"
|
data-slot="avatar-fallback"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -6,16 +6,19 @@ import { avatarContextKey } from "./context";
|
|||||||
defineOptions({ inheritAttrs: false });
|
defineOptions({ inheritAttrs: false });
|
||||||
|
|
||||||
defineProps<{
|
defineProps<{
|
||||||
src: string;
|
src?: string;
|
||||||
|
as?: string;
|
||||||
alt?: string;
|
alt?: string;
|
||||||
asChild?: boolean;
|
asChild?: boolean;
|
||||||
crossOrigin?: "" | "anonymous" | "use-credentials";
|
crossOrigin?: "" | "anonymous" | "use-credentials";
|
||||||
loading?: "eager" | "lazy";
|
loading?: "eager" | "lazy";
|
||||||
sizes?: string;
|
sizes?: string;
|
||||||
srcSet?: string;
|
srcSet?: string;
|
||||||
referrerPolicy?: string;
|
referrerPolicy?: import("vue").ImgHTMLAttributes["referrerpolicy"];
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
const emit = defineEmits<{ load: [event: Event]; error: [event: Event] }>();
|
||||||
|
|
||||||
const attrs = useAttrs();
|
const attrs = useAttrs();
|
||||||
const ctx = inject(avatarContextKey, undefined);
|
const ctx = inject(avatarContextKey, undefined);
|
||||||
const imageClass = computed(
|
const imageClass = computed(
|
||||||
@@ -26,7 +29,8 @@ const imageClass = computed(
|
|||||||
<template>
|
<template>
|
||||||
<AvatarImage
|
<AvatarImage
|
||||||
v-bind="attrs"
|
v-bind="attrs"
|
||||||
:src="src"
|
:src="src ?? ''"
|
||||||
|
:as="as"
|
||||||
:alt="alt"
|
:alt="alt"
|
||||||
:as-child="asChild"
|
:as-child="asChild"
|
||||||
:crossorigin="crossOrigin"
|
:crossorigin="crossOrigin"
|
||||||
@@ -35,6 +39,10 @@ const imageClass = computed(
|
|||||||
:srcset="srcSet"
|
:srcset="srcSet"
|
||||||
:referrerpolicy="referrerPolicy"
|
:referrerpolicy="referrerPolicy"
|
||||||
:class="imageClass"
|
:class="imageClass"
|
||||||
|
@load="emit('load', $event)"
|
||||||
|
@error="emit('error', $event)"
|
||||||
data-slot="avatar-image"
|
data-slot="avatar-image"
|
||||||
/>
|
>
|
||||||
|
<slot />
|
||||||
|
</AvatarImage>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ export type {
|
|||||||
TextAreaProps,
|
TextAreaProps,
|
||||||
CloseButtonProps,
|
CloseButtonProps,
|
||||||
AvatarProps,
|
AvatarProps,
|
||||||
|
AvatarRootProps,
|
||||||
AvatarImageProps,
|
AvatarImageProps,
|
||||||
AvatarFallbackProps,
|
AvatarFallbackProps,
|
||||||
AvatarGroupProps,
|
AvatarGroupProps,
|
||||||
@@ -107,7 +108,10 @@ export { default as Spinner } from "./components/spinner/Spinner.vue";
|
|||||||
export { default as TextArea } from "./components/textarea/TextArea.vue";
|
export { default as TextArea } from "./components/textarea/TextArea.vue";
|
||||||
|
|
||||||
export { default as CloseButton } from "./components/close-button/CloseButton.vue";
|
export { default as CloseButton } from "./components/close-button/CloseButton.vue";
|
||||||
export { default as Avatar } from "./components/avatar/Avatar.vue";
|
export {
|
||||||
|
default as Avatar,
|
||||||
|
default as AvatarRoot,
|
||||||
|
} from "./components/avatar/Avatar.vue";
|
||||||
export { default as AvatarImage } from "./components/avatar/AvatarImage.vue";
|
export { default as AvatarImage } from "./components/avatar/AvatarImage.vue";
|
||||||
export { default as AvatarFallback } from "./components/avatar/AvatarFallback.vue";
|
export { default as AvatarFallback } from "./components/avatar/AvatarFallback.vue";
|
||||||
export { default as AvatarGroup } from "./components/avatar/AvatarGroup.vue";
|
export { default as AvatarGroup } from "./components/avatar/AvatarGroup.vue";
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export type SpinnerProps = InstanceType<typeof Spinner>["$props"];
|
|||||||
export type TextAreaProps = InstanceType<typeof TextArea>["$props"];
|
export type TextAreaProps = InstanceType<typeof TextArea>["$props"];
|
||||||
export type CloseButtonProps = InstanceType<typeof CloseButton>["$props"];
|
export type CloseButtonProps = InstanceType<typeof CloseButton>["$props"];
|
||||||
export type AvatarProps = InstanceType<typeof Avatar>["$props"];
|
export type AvatarProps = InstanceType<typeof Avatar>["$props"];
|
||||||
|
export type AvatarRootProps = AvatarProps;
|
||||||
export type AvatarImageProps = InstanceType<typeof AvatarImage>["$props"];
|
export type AvatarImageProps = InstanceType<typeof AvatarImage>["$props"];
|
||||||
export type AvatarFallbackProps = InstanceType<typeof AvatarFallback>["$props"];
|
export type AvatarFallbackProps = InstanceType<typeof AvatarFallback>["$props"];
|
||||||
export type AvatarGroupProps = InstanceType<typeof AvatarGroup>["$props"];
|
export type AvatarGroupProps = InstanceType<typeof AvatarGroup>["$props"];
|
||||||
|
|||||||
@@ -0,0 +1,226 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { flushPromises, mount } from "@vue/test-utils";
|
||||||
|
import { defineComponent, h, nextTick, ref } from "vue";
|
||||||
|
import { Avatar, AvatarImage, AvatarFallback } from "../packages/vue/src";
|
||||||
|
import CustomImageDemo from "../apps/docs/demos/avatar-custom-image-component.vue";
|
||||||
|
|
||||||
|
const wrappers: ReturnType<typeof mount>[] = [];
|
||||||
|
afterEach(() => {
|
||||||
|
wrappers.splice(0).forEach((w) => w.unmount());
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
function mockImages() {
|
||||||
|
const images: PreloadImage[] = [];
|
||||||
|
class PreloadImage extends EventTarget {
|
||||||
|
source = "";
|
||||||
|
get src() {
|
||||||
|
return this.source;
|
||||||
|
}
|
||||||
|
set src(value: string) {
|
||||||
|
this.source = value;
|
||||||
|
this.complete = false;
|
||||||
|
this.naturalWidth = 0;
|
||||||
|
}
|
||||||
|
complete = false;
|
||||||
|
naturalWidth = 0;
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
images.push(this);
|
||||||
|
}
|
||||||
|
finish(type: "load" | "error") {
|
||||||
|
this.complete = true;
|
||||||
|
this.naturalWidth = type === "load" ? 40 : 0;
|
||||||
|
this.dispatchEvent(new Event(type));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vi.stubGlobal("Image", PreloadImage);
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderAvatar(
|
||||||
|
imageProps: Record<string, unknown> = {},
|
||||||
|
fallbackProps = {},
|
||||||
|
) {
|
||||||
|
const w = mount(Avatar, {
|
||||||
|
slots: {
|
||||||
|
default: () => [
|
||||||
|
h(AvatarImage, imageProps),
|
||||||
|
h(AvatarFallback, fallbackProps, { default: () => "JD" }),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
wrappers.push(w);
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Avatar public loading composition", () => {
|
||||||
|
it("renders the fixed-source custom image demo through asChild", async () => {
|
||||||
|
const images = mockImages();
|
||||||
|
const w = mount(CustomImageDemo, { attachTo: document.body });
|
||||||
|
wrappers.push(w);
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.findAll("img")).toHaveLength(1);
|
||||||
|
const img = w.get("img");
|
||||||
|
expect(img.attributes("alt")).toBe("约翰·多伊");
|
||||||
|
expect(img.attributes("width")).toBe("40");
|
||||||
|
expect(img.classes()).toContain("hv-avatar__image");
|
||||||
|
images[0].finish("load");
|
||||||
|
await flushPromises();
|
||||||
|
expect(img.isVisible()).toBe(true);
|
||||||
|
expect(w.find("[data-slot=avatar-fallback]").exists()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps native events and dynamic attrs on the custom image", async () => {
|
||||||
|
mockImages();
|
||||||
|
const load = vi.fn();
|
||||||
|
const error = vi.fn();
|
||||||
|
const title = ref("first");
|
||||||
|
const w = mount(
|
||||||
|
defineComponent({
|
||||||
|
setup: () => () =>
|
||||||
|
h(
|
||||||
|
Avatar,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
default: () =>
|
||||||
|
h(
|
||||||
|
AvatarImage,
|
||||||
|
{
|
||||||
|
src: "/a.png",
|
||||||
|
asChild: true,
|
||||||
|
title: title.value,
|
||||||
|
onLoad: load,
|
||||||
|
onError: error,
|
||||||
|
srcSet: "/a.png 1x, /b.png 2x",
|
||||||
|
sizes: "40px",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: () =>
|
||||||
|
h("img", { alt: "custom", class: "child-image" }),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
{ attachTo: document.body },
|
||||||
|
);
|
||||||
|
wrappers.push(w);
|
||||||
|
const img = w.get("img");
|
||||||
|
expect(img.attributes("srcset")).toContain("2x");
|
||||||
|
expect(img.attributes("sizes")).toBe("40px");
|
||||||
|
expect(img.classes()).toContain("child-image");
|
||||||
|
await img.trigger("load");
|
||||||
|
await img.trigger("error");
|
||||||
|
expect(load).toHaveBeenCalledTimes(1);
|
||||||
|
expect(error).toHaveBeenCalledTimes(1);
|
||||||
|
title.value = "second";
|
||||||
|
await nextTick();
|
||||||
|
expect(img.attributes("title")).toBe("second");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns to fallback for a changed failed source and can recover", async () => {
|
||||||
|
const images = mockImages();
|
||||||
|
const src = ref("/first.png");
|
||||||
|
const w = mount(
|
||||||
|
defineComponent({
|
||||||
|
setup: () => () =>
|
||||||
|
h(
|
||||||
|
Avatar,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
default: () => [
|
||||||
|
h(AvatarImage, { src: src.value, alt: "profile" }),
|
||||||
|
h(AvatarFallback, {}, { default: () => "JD" }),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
{ attachTo: document.body },
|
||||||
|
);
|
||||||
|
wrappers.push(w);
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.get("[data-slot=avatar-fallback]").text()).toBe("JD");
|
||||||
|
images[0].finish("load");
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.get("img").isVisible()).toBe(true);
|
||||||
|
expect(w.find("[data-slot=avatar-fallback]").exists()).toBe(false);
|
||||||
|
images[0].complete = false;
|
||||||
|
src.value = "/missing.png";
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.get("img").isVisible()).toBe(false);
|
||||||
|
images[0].finish("error");
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.get("[data-slot=avatar-fallback]").text()).toBe("JD");
|
||||||
|
images[0].complete = false;
|
||||||
|
src.value = "/recovered.png";
|
||||||
|
await flushPromises();
|
||||||
|
images[0].finish("load");
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.get("img").attributes("src")).toBe("/recovered.png");
|
||||||
|
expect(w.get("img").isVisible()).toBe(true);
|
||||||
|
expect(w.find("[data-slot=avatar-fallback]").exists()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows fallback with an omitted src and zero delay", async () => {
|
||||||
|
mockImages();
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const w = renderAvatar({}, { delayMs: 0 });
|
||||||
|
await vi.runAllTimersAsync();
|
||||||
|
await flushPromises();
|
||||||
|
expect(w.get("[data-slot=avatar-fallback]").text()).toBe("JD");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates a pending delay to zero and supports root/fallback asChild", async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const delay = ref(600);
|
||||||
|
const w = mount(
|
||||||
|
defineComponent({
|
||||||
|
setup: () => () =>
|
||||||
|
h(
|
||||||
|
Avatar,
|
||||||
|
{ asChild: true },
|
||||||
|
{
|
||||||
|
default: () =>
|
||||||
|
h("div", { "data-custom-root": "" }, [
|
||||||
|
h(
|
||||||
|
AvatarFallback,
|
||||||
|
{ asChild: true, delayMs: delay.value },
|
||||||
|
{
|
||||||
|
default: () =>
|
||||||
|
h("strong", { "aria-label": "initials" }, "JD"),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
{ attachTo: document.body },
|
||||||
|
);
|
||||||
|
wrappers.push(w);
|
||||||
|
expect(w.get("[data-slot=avatar]").element.tagName).toBe("DIV");
|
||||||
|
expect(w.find("strong").exists()).toBe(false);
|
||||||
|
delay.value = 0;
|
||||||
|
await nextTick();
|
||||||
|
expect(w.get("strong").text()).toBe("JD");
|
||||||
|
expect(w.get("strong").attributes("aria-label")).toBe("initials");
|
||||||
|
expect(w.get("strong").classes()).toContain("hv-avatar__fallback");
|
||||||
|
expect(vi.getTimerCount()).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("honors positive delay and cancels the pending fallback on unmount", async () => {
|
||||||
|
mockImages();
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const w = renderAvatar({ src: "/pending.png" }, { delayMs: 600 });
|
||||||
|
expect(w.find("[data-slot=avatar-fallback]").exists()).toBe(false);
|
||||||
|
await vi.advanceTimersByTimeAsync(599);
|
||||||
|
expect(w.find("[data-slot=avatar-fallback]").exists()).toBe(false);
|
||||||
|
await vi.advanceTimersByTimeAsync(1);
|
||||||
|
expect(w.get("[data-slot=avatar-fallback]").text()).toBe("JD");
|
||||||
|
const pending = renderAvatar({}, { delayMs: 1000 });
|
||||||
|
pending.unmount();
|
||||||
|
wrappers.splice(wrappers.indexOf(pending), 1);
|
||||||
|
expect(vi.getTimerCount()).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { h } from "vue";
|
||||||
|
import {
|
||||||
|
Avatar,
|
||||||
|
AvatarRoot,
|
||||||
|
AvatarImage,
|
||||||
|
AvatarFallback,
|
||||||
|
avatarVariants,
|
||||||
|
type AvatarProps,
|
||||||
|
type AvatarRootProps,
|
||||||
|
type AvatarImageProps,
|
||||||
|
type AvatarFallbackProps,
|
||||||
|
} from "@heroui-vue/components";
|
||||||
|
const root: AvatarProps = {
|
||||||
|
size: "lg",
|
||||||
|
color: "accent",
|
||||||
|
variant: "soft",
|
||||||
|
as: "div",
|
||||||
|
asChild: true,
|
||||||
|
};
|
||||||
|
const image: AvatarImageProps = {
|
||||||
|
asChild: true,
|
||||||
|
srcSet: "/a.png 2x",
|
||||||
|
sizes: "40px",
|
||||||
|
loading: "lazy",
|
||||||
|
crossOrigin: "anonymous",
|
||||||
|
referrerPolicy: "no-referrer",
|
||||||
|
onLoad: (event: Event) => event.type,
|
||||||
|
onError: (event: Event) => event.type,
|
||||||
|
};
|
||||||
|
const fallback: AvatarFallbackProps = {
|
||||||
|
delayMs: 0,
|
||||||
|
color: "danger",
|
||||||
|
asChild: true,
|
||||||
|
};
|
||||||
|
const rootAlias: AvatarRootProps = root;
|
||||||
|
h(AvatarRoot, rootAlias);
|
||||||
|
h(AvatarImage, image);
|
||||||
|
h(AvatarFallback, fallback);
|
||||||
|
// @ts-expect-error baseline size only
|
||||||
|
const badRoot: AvatarProps = { size: "xl" };
|
||||||
|
// @ts-expect-error baseline color only
|
||||||
|
const badFallback: AvatarFallbackProps = { color: "blue" };
|
||||||
|
// @ts-expect-error native loading policy
|
||||||
|
const badImage: AvatarImageProps = { loading: "immediate" };
|
||||||
|
// @ts-expect-error native referrer policy
|
||||||
|
const badPolicy: AvatarImageProps = { referrerPolicy: "secret" };
|
||||||
|
void [Avatar, avatarVariants, badRoot, badFallback, badImage, badPolicy];
|
||||||
@@ -532,5 +532,22 @@
|
|||||||
"background-color:var(--overlay)",
|
"background-color:var(--overlay)",
|
||||||
"data-exiting"
|
"data-exiting"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"avatar": {
|
||||||
|
"exports": [
|
||||||
|
"Avatar",
|
||||||
|
"AvatarRoot",
|
||||||
|
"AvatarImage",
|
||||||
|
"AvatarFallback",
|
||||||
|
"avatarVariants"
|
||||||
|
],
|
||||||
|
"css": [
|
||||||
|
".hv-avatar",
|
||||||
|
".hv-avatar__image",
|
||||||
|
".hv-avatar__fallback",
|
||||||
|
".hv-avatar--sm",
|
||||||
|
".hv-avatar--lg",
|
||||||
|
".hv-avatar--soft"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user