显示一张图片,并在加载中或失败时给出回退内容。
用 Avatar 表示一个人或一个团队,比如在评论、成员列表或账户菜单里。它在图片加载完成前显示姓名首字母或一个图标,图片加载失败时也继续显示。
<script setup lang="ts">
import { Avatar, AvatarFallback, AvatarImage } from "@ui-sdk/vue/avatar"
const person = {
name: "shadcn",
src: "https://github.com/shadcn.png",
}
</script>
<template>
<div data-slot="row">
<Avatar>
<AvatarImage :src="person.src" :alt="person.name" />
<AvatarFallback
><span aria-hidden="true">CN</span
><span data-slot="visually-hidden">shadcn</span></AvatarFallback
>
</Avatar>
</div>
</template>安装
npm install @ui-sdk/vue @ui-sdk/core组合
用下面的组合构建一个 Avatar:
Avatar
├── AvatarImage
└── AvatarFallbackAvatar把图片的加载状态共享给它的各个部件。AvatarImage渲染图片。- 没有图片加载时,
AvatarFallback会显示。
图片是真正的 img,服务端标记里也一样。浏览器可以在水合之前就开始获取它。回退内容会一直可见,直到图片加载完成;如果加载失败,回退内容会保留。
用法
导入各个部件并把它们组合起来:
<script setup lang="ts">
import { Avatar, AvatarFallback, AvatarImage } from "@ui-sdk/vue/avatar"
</script>
<template>
<Avatar>
<AvatarImage src="/ada.png" alt="Ada Lovelace" />
<AvatarFallback>AL</AvatarFallback>
</Avatar>
</template>自己构建
@ui-sdk/vue/avatar 自带一个现成的 Avatar。如果你想用自己的名字构建一个,从 @ui-sdk/core 里的 avatar 原语(primitive)开始:
import { avatar } from "@ui-sdk/core/avatar"
import { createParts } from "@ui-sdk/vue/primitive"
const {
Root: UserPhoto,
Image: UserPhotoImage,
Fallback: UserPhotoInitials,
} = createParts(avatar, { root: "span", image: "img", fallback: "span" })
export { UserPhoto, UserPhotoImage, UserPhotoInitials }<template>
<UserPhoto>
<UserPhotoImage src="https://github.com/shadcn.png" alt="shadcn" />
<UserPhotoInitials>CN</UserPhotoInitials>
</UserPhoto>
</template>它们和现成的 Avatar 由同一批部件组成,只是用了你的名字。更多内容见渲染原语和自定义渲染。
回退延迟
在回退内容上设置 delay,让它在显示前等待一段时间。这样可以避免图片加载很快时首字母一闪而过。不设置延迟时,回退内容会立即出现,在服务端也是如此。
<template>
<Avatar>
<AvatarImage src="https://github.com/shadcn.png" alt="shadcn" />
<AvatarFallback :delay="600">CN</AvatarFallback>
</Avatar>
</template>图片属性
把 srcset、crossorigin 和 referrerpolicy 这类图片属性传给图片。它们作用在浏览器加载的同一个 img 上。对于页面靠下的头像,使用 loading="lazy"。
<template>
<Avatar>
<AvatarImage
src="https://github.com/shadcn.png"
srcset="https://github.com/shadcn.png?size=80 1x, https://github.com/shadcn.png?size=160 2x"
alt="shadcn"
crossorigin="anonymous"
referrerpolicy="no-referrer"
loading="lazy"
/>
<AvatarFallback>CN</AvatarFallback>
</Avatar>
</template>可交互的头像
点击头像会执行某个动作时,改用按钮。给它一个无障碍名称,这个名称在图片加载期间也要一直可用。
<template>
<Avatar @click="openProfile">
<template #render="{ props, children }">
<button v-bind="props" type="button" aria-label="shadcn">
<component :is="children" />
</button>
</template>
<AvatarImage src="https://github.com/shadcn.png" alt="" />
<AvatarFallback aria-hidden="true">CN</AvatarFallback>
</Avatar>
</template>部件与组合说明了如何在这个框架里更换根元素。
加载状态
@loading-status-change 会收到新状态和变更详情。可能的状态有 loading、loaded、error 和 idle。
<template>
<Avatar
@loading-status-change="
(status, details) => {
if (status === 'error') reportBrokenImage(details.reason)
}
"
>
<AvatarImage :src="user.image" :alt="user.name" />
<AvatarFallback>{{ user.initials }}</AvatarFallback>
</Avatar>
</template>替换已经加载的图片时,浏览器会让旧图保持可见,直到新来源加载完成。替换失败时会显示回退内容。变更详情说明状态为什么变化;见原因。
自定义渲染
用部件与组合替换元素或读取它的状态。每个部件都会收到加载状态,所以图片加载失败时,回退内容可以显示不同的文字:
<template>
<Avatar>
<AvatarImage :src="user.image" :alt="user.name" />
<AvatarFallback v-slot="{ state }">
{{ state.loadingStatus === "error" ? "?" : user.initials }}
</AvatarFallback>
</Avatar>
</template>样式
设置根元素的大小,让图片和回退内容填满它。图片在加载完成前会管理自己的隐藏样式;更换它的元素时,保留这些样式。
每个部件都有一个 data-slot,比如 avatar-fallback,并通过数据属性下列出的属性显示加载状态。
数据属性
Avatar
[data-slot="avatar"]
| 属性 | 含义 |
|---|---|
data-loading-status | idleloadingloadederrorWhere the image is: no image yet, loading, loaded, or failed. |
AvatarImage
[data-slot="avatar-image"]
| 属性 | 含义 |
|---|---|
data-loading-status | idleloadingloadederrorWhere the image is: no image yet, loading, loaded, or failed. |
AvatarFallback
[data-slot="avatar-fallback"]
| 属性 | 含义 |
|---|---|
data-loading-status | idleloadingloadederrorWhere the image is: no image yet, loading, loaded, or failed. |
API 参考
props 与下面相同,回调是事件。
Avatar
@loading-status-change((status: AvatarLoadingStatus, details: ChangeDetails<AvatarLoadingStatusChangeReason>) => void)- Runs when the image's loading status changes:
"loading"when it gets a source,"loaded","error", and"idle"when it leaves the page.details.reasonsays why, anddetails.eventis the image'sloadorerrorevent, or undefined when the status was known at once, like for a cached image. debug?boolean- Logs every state change with its reason to the console, in development builds.
AvatarImage
图片。渲染一个真正的 img,所以浏览器可以在水合之前获取它,并接受它的属性,比如 srcset、crossorigin 和 referrerpolicy。
AvatarFallback
delay?number- How long to wait, in milliseconds, before showing the fallback, so a fast image appears without the fallback flashing first. Without one, the fallback shows at once, server markup included.
状态
渲染函数、基于状态的类名和槽位会收到什么。每个部件收到相同的状态。
loadingStatusAvatarLoadingStatus- Where the image is:
"idle","loading","loaded"or"error".
原因
details.reason 说明状态为什么变化。
image-loadEvent | undefined- An image loaded. No event when it was already in the browser's cache.
image-errorEvent | undefined- An image failed to load, or has no source. No event when that was known at once.
src-changenone- An image got a new source, or left the page, so its status starts over.
事件
事件先收到新状态,然后收到详情。
| 绑定 | 所属 | 收到 |
|---|---|---|
@loading-status-change | Avatar | (status, details):图片的状态、原因和 details.event |
无障碍
给图片有意义的 alt 文本。回退内容里也要包含这个人的名字;如果你只想显示首字母,可以使用视觉上隐藏的文字。
头像旁边可见的名字已经能表明这个人是谁时,用空的 alt,并对屏幕阅读器隐藏装饰性的回退内容。
- An image that loads replaces the fallback.
- An image that fails keeps the fallback.
- An avatar without an image shows its fallback.
- A fallback with a delay waits before showing.
- The image's loading status is reported.
- Removing the image brings the fallback back.
- A new source shows its image once it loads.
- The image is fetched with its own referrer policy.
- A cached image doesn't flash the fallback when the avatar mounts again.
- A new source keeps showing the previous image until it loads.
- The image is fetched once, even when it can't be cached.
运行于 React、Vue 和 web components。
