fix: complete Avatar image composition and fallback migration

This commit is contained in:
maoyingjie
2026-10-27 18:05:26 +08:00
parent 6de246de49
commit fa8b22719e
16 changed files with 366 additions and 23 deletions
@@ -7,6 +7,8 @@ import { avatarContextKey, avatarGroupContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
as?: string;
asChild?: boolean;
color?: AvatarVariants["color"];
size?: AvatarVariants["size"];
variant?: AvatarVariants["variant"];
@@ -45,7 +47,13 @@ provide(avatarContextKey, { slots });
</script>
<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 />
</AvatarRoot>
</template>
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { inject, ref, watch, useAttrs } from "vue";
import { AvatarFallback } from "reka-ui";
import { avatarVariants, type AvatarVariants } from "@heroui-vue/styles";
import { avatarContextKey } from "./context";
@@ -7,10 +7,29 @@ import { avatarContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
as?: string;
asChild?: boolean;
delayMs?: number;
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 ctx = inject(avatarContextKey, undefined);
@@ -36,8 +55,10 @@ function fallbackClass() {
<template>
<AvatarFallback
v-if="canRender"
v-bind="restAttrs()"
:delay-ms="delayMs"
:as="as"
:as-child="asChild"
:class="fallbackClass()"
data-slot="avatar-fallback"
>
@@ -6,16 +6,19 @@ import { avatarContextKey } from "./context";
defineOptions({ inheritAttrs: false });
defineProps<{
src: string;
src?: string;
as?: string;
alt?: string;
asChild?: boolean;
crossOrigin?: "" | "anonymous" | "use-credentials";
loading?: "eager" | "lazy";
sizes?: string;
srcSet?: string;
referrerPolicy?: string;
referrerPolicy?: import("vue").ImgHTMLAttributes["referrerpolicy"];
}>();
const emit = defineEmits<{ load: [event: Event]; error: [event: Event] }>();
const attrs = useAttrs();
const ctx = inject(avatarContextKey, undefined);
const imageClass = computed(
@@ -26,7 +29,8 @@ const imageClass = computed(
<template>
<AvatarImage
v-bind="attrs"
:src="src"
:src="src ?? ''"
:as="as"
:alt="alt"
:as-child="asChild"
:crossorigin="crossOrigin"
@@ -35,6 +39,10 @@ const imageClass = computed(
:srcset="srcSet"
:referrerpolicy="referrerPolicy"
:class="imageClass"
@load="emit('load', $event)"
@error="emit('error', $event)"
data-slot="avatar-image"
/>
>
<slot />
</AvatarImage>
</template>