Avatar(头像)
带回退内容的图像元素,用于表示用户。
loading
安装
bash
npx shadcn@latest add avatar1
1. 安装以下依赖:
bash
npm install @base-ui/react1
2. 将以下代码复制到你的项目中。
tsx
import * as React from "react"
import { Avatar as AvatarPrimitive } from "@base-ui/react/avatar"
import { cn } from "cn"
function Avatar({
className,
size = "default",
...props
}: AvatarPrimitive.Root.Props & {
size?: "default" | "sm" | "lg"
}) {
return (
<AvatarPrimitive.Root
data-slot="avatar"
data-size={size}
className={cn(
"group/avatar relative flex size-8 shrink-0 rounded-full select-none after:absolute after:inset-0 after:rounded-full after:border after:border-border after:mix-blend-darken data-[size=lg]:size-10 data-[size=sm]:size-6 dark:after:mix-blend-lighten",
className
)}
{...props}
/>
)
}
function AvatarImage({ className, ...props }: AvatarPrimitive.Image.Props) {
return (
<AvatarPrimitive.Image
data-slot="avatar-image"
className={cn(
"aspect-square size-full rounded-full object-cover",
className
)}
{...props}
/>
)
}
function AvatarFallback({
className,
...props
}: AvatarPrimitive.Fallback.Props) {
return (
<AvatarPrimitive.Fallback
data-slot="avatar-fallback"
className={cn(
"flex size-full items-center justify-center rounded-full bg-muted type-small text-muted-foreground group-data-[size=sm]/avatar:type-small",
className
)}
{...props}
/>
)
}
function AvatarBadge({ className, ...props }: React.ComponentProps<"span">) {
return (
<span
data-slot="avatar-badge"
className={cn(
"absolute end-0 bottom-0 z-10 inline-flex items-center justify-center rounded-full bg-primary text-primary-foreground bg-blend-color ring-2 ring-background select-none",
"group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden",
"group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2",
"group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2",
className
)}
{...props}
/>
)
}
function AvatarGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="avatar-group"
className={cn(
"group/avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background",
className
)}
{...props}
/>
)
}
function AvatarGroupCount({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="avatar-group-count"
className={cn(
"relative flex size-8 shrink-0 items-center justify-center rounded-full bg-muted type-small text-muted-foreground ring-2 ring-background group-has-data-[size=lg]/avatar-group:size-10 group-has-data-[size=sm]/avatar-group:size-6 [&>svg]:size-4 group-has-data-[size=lg]/avatar-group:[&>svg]:size-5 group-has-data-[size=sm]/avatar-group:[&>svg]:size-3",
className
)}
{...props}
/>
)
}
export {
Avatar,
AvatarImage,
AvatarFallback,
AvatarGroup,
AvatarGroupCount,
AvatarBadge,
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
3. 根据你的项目配置调整导入路径。
用法
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"1
tsx
<Avatar>
<AvatarImage src="https://github.com/shadcn.png" />
<AvatarFallback>CN</AvatarFallback>
</Avatar>1
2
3
4
2
3
4
结构
按以下结构组合 Avatar:
text
Avatar
├── AvatarImage
├── AvatarFallback
└── AvatarBadge1
2
3
4
2
3
4
按以下结构组合 AvatarGroup:
text
AvatarGroup
├── Avatar
│ ├── AvatarImage
│ ├── AvatarFallback
│ └── AvatarBadge
├── Avatar
│ ├── AvatarImage
│ ├── AvatarFallback
│ └── AvatarBadge
└── AvatarGroupCount1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
基础
基础头像,包含图像和回退内容。
loading
徽章
使用 AvatarBadge 组件为头像添加徽章。徽章位于头像的右下角。
loading
使用 className 属性自定义徽章的颜色、尺寸等样式。
tsx
<Avatar>
<AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" />
<AvatarFallback>CN</AvatarFallback>
<AvatarBadge className="bg-chart-2" />
</Avatar>1
2
3
4
5
2
3
4
5
带图标的徽章
也可以在 <AvatarBadge> 内使用图标。
loading
头像组
使用 AvatarGroup 组件添加一组头像。
loading
头像组计数
使用 <AvatarGroupCount> 为头像组添加计数。
loading
带图标的头像组
也可以在 <AvatarGroupCount> 内使用图标。
loading
尺寸
使用 size 属性调整头像尺寸。
loading
下拉菜单
可以将 Avatar 组件用作下拉菜单的触发器。
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
Avatar
Avatar 是包裹头像图像和回退内容的根组件。
| 属性 | 类型 | 默认值 |
|---|---|---|
size | "default" | "sm" | "lg" | "default" |
className | string | - |
AvatarImage
AvatarImage 组件显示头像图像。它接受 Base UI Avatar Image 的所有属性。
| 属性 | 类型 | 默认值 |
|---|---|---|
src | string | - |
alt | string | - |
className | string | - |
AvatarFallback
图像加载失败时,AvatarFallback 组件显示回退内容。它接受 Base UI Avatar Fallback 的所有属性。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
AvatarBadge
AvatarBadge 组件在头像上显示徽章,通常位于右下角。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
AvatarGroup
AvatarGroup 组件将一组头像重叠显示。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
AvatarGroupCount
AvatarGroupCount 组件在头像组中显示计数,通常用来表示其余头像的数量。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
有关 Base UI Avatar 属性的更多信息,请参阅 Base UI 文档。