145a208687
HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
154 lines
6.5 KiB
Markdown
154 lines
6.5 KiB
Markdown
---
|
||
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 / AvatarRoot
|
||
|
||
| 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` 时立即显示,正数延迟可动态更新 |
|
||
| `color` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | - | 覆盖父级颜色(替换而非叠加) |
|
||
| `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 合成事件完全等价。
|
||
|
||
## 相关组件
|
||
|
||
[AvatarGroup 头像组](./avatar-group) · [Separator 分隔符](./separator) · [Badge 徽章](./badge)
|