Files
heroui-vue/apps/docs/components/avatar.md
T

150 lines
5.7 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.
---
title: Avatar 头像
description: 显示用户头像,支持自定义 fallback 内容
component: avatar
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Avatar, AvatarImage, AvatarFallback } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="avatar-basic" />
## 组件结构
```vue
<script setup lang="ts">
import { Avatar, AvatarImage, AvatarFallback } from "@heroui-vue/components";
</script>
<template>
<Avatar>
<AvatarImage />
<AvatarFallback />
</Avatar>
</template>
```
## 示例
### 尺寸
<ComponentPreview name="avatar-sizes" />
### 颜色
<ComponentPreview name="avatar-colors" />
### 变体
<ComponentPreview name="avatar-variants" />
### 回退内容
<ComponentPreview name="avatar-fallback" />
## 自定义样式
### 自定义图片组件
在 `AvatarImage` 上使用 `as-child`,即可与自定义图片组件组合。请将 `src` 传给 `AvatarImage`,以便跟踪加载状态,并在图片就绪前显示回退内容。
<ComponentPreview name="avatar-custom-image-component" />
### Tailwind CSS
<ComponentPreview name="avatar-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 Avatar 组件类。
```css
@layer components {
.hv-avatar {
@apply size-16 border-2 border-accent;
}
.hv-avatar__fallback {
@apply bg-gradient-to-br from-purple-500 to-pink-500;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Avatar 组件使用以下 CSS 类:
#### 基础类 [!toc]
- `.hv-avatar` - 基础容器,默认尺寸(size-10)
- `.hv-avatar__image` - 图片元素,aspect-square 尺寸
- `.hv-avatar__fallback` - 居中内容的 fallback 容器
#### 尺寸修饰符 [!toc]
- `.hv-avatar--sm` - 小头像(size-8)
- `.hv-avatar--md` - 中头像(默认,无额外样式)
- `.hv-avatar--lg` - 大头像(size-12)
#### 变体修饰符 [!toc]
- `.hv-avatar--soft` - 浅色背景的 soft 变体
#### 颜色修饰符 [!toc]
- `.hv-avatar__fallback--default` - 默认文字颜色
- `.hv-avatar__fallback--accent` - 强调色文字
- `.hv-avatar__fallback--success` - 成功色文字
- `.hv-avatar__fallback--warning` - 警告色文字
- `.hv-avatar__fallback--danger` - 危险色文字
## API 参考
### Avatar
| Prop | 类型 | 默认值 | 描述 |
| --------- | ------------------------------------------------------------- | ----------- | ----------------- |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 头像尺寸 |
| `color` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | `'default'` | Fallback 颜色主题 |
| `variant` | `'default' \| 'soft'` | `'default'` | 视觉样式变体 |
| `class` | `string` | - | 附加 CSS 类 |
### AvatarImage
| Prop | 类型 | 默认值 | 描述 |
| ------------- | ---------------------------------- | ------- | ------------------------------------------ |
| `src` | `string` | - | 图片 URL |
| `srcSet` | `string` | - | 响应式图片的 `srcset` 属性 |
| `sizes` | `string` | - | 响应式图片的 `sizes` 属性 |
| `alt` | `string` | - | 图片替代文本 |
| `asChild` | `boolean` | `false` | 将属性合并到子元素上,而不是渲染原生 `img` |
| `@load` | `(event: Event) => void` | - | 图片加载成功时的回调 |
| `@error` | `(event: Event) => void` | - | 图片加载失败时的回调 |
| `crossOrigin` | `'anonymous' \| 'use-credentials'` | - | 图片请求的 CORS 设置 |
| `loading` | `'eager' \| 'lazy'` | - | 原生懒加载属性 |
| `class` | `string` | - | 附加 CSS 类 |
### AvatarFallback
| Prop | 类型 | 默认值 | 描述 |
| --------- | ------------------------------------------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------- |
| `delayMs` | `number` | - | 显示 fallback 前的延迟(防止闪烁)。`0` 与未指定不同:底座在 `delayMs=0` 时不会显示 fallback,需要延迟时请传正数 |
| `color` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | - | 覆盖父级颜色(替换而非叠加) |
| `class` | `string` | - | 附加 CSS 类 |
Avatar 使用 Reka UI 的 `AvatarRoot` / `AvatarImage` / `AvatarFallback` 处理加载与回退状态。`onLoad` / `onError` 对应 Vue 原生 `@load` / `@error` 事件,不声明与 React 合成事件完全等价。
## 相关组件
[AvatarGroup 头像组](./avatar-group) · [Card 卡片](./card)