Attachment(附件)
显示文件或图像附件,包含媒体、元数据、上传状态和操作。
Attachment 组件显示文件或图像附件及其媒体、名称和元数据,还可显示操作和上传状态。可在聊天输入框、消息线程和上传列表中用它展示文件和图像。
安装
npx shadcn@latest add attachment1. 安装所需的 shadcn/ui 依赖:
npx shadcn@latest add button2. 将以下代码复制到你的项目中。
import * as React from "react"
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Button } from "./button"
const attachmentVariants = cva(
"group/attachment relative flex w-fit max-w-full min-w-0 shrink-0 flex-wrap rounded-2xl border bg-card text-card-foreground transition-colors focus-within:ring-1 focus-within:ring-ring/50 has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed",
{
variants: {
size: {
default:
"gap-2 text-sm has-data-[slot=attachment-content]:px-2.5 has-data-[slot=attachment-content]:py-2 has-data-[slot=attachment-media]:p-2",
sm: "gap-2.5 text-xs has-data-[slot=attachment-content]:px-2 has-data-[slot=attachment-content]:py-1.5 has-data-[slot=attachment-media]:p-1.5",
xs: "gap-1.5 rounded-xl text-xs has-data-[slot=attachment-content]:px-1.5 has-data-[slot=attachment-content]:py-1 has-data-[slot=attachment-media]:p-1",
},
orientation: {
horizontal: "min-w-40 items-center",
vertical: "w-24 flex-col has-data-[slot=attachment-content]:w-30",
},
},
}
)
function Attachment({
className,
state = "done",
size = "default",
orientation = "horizontal",
...props
}: React.ComponentProps<"div"> &
VariantProps<typeof attachmentVariants> & {
state?: "idle" | "uploading" | "processing" | "error" | "done"
}) {
return (
<div
data-slot="attachment"
data-state={state}
data-size={size}
data-orientation={orientation}
className={cn(attachmentVariants({ size, orientation }), className)}
{...props}
/>
)
}
const attachmentMediaVariants = cva(
"relative flex aspect-square w-10 shrink-0 items-center justify-center overflow-hidden rounded-xl bg-muted text-foreground group-data-[orientation=vertical]/attachment:w-full group-data-[size=sm]/attachment:w-8 group-data-[size=xs]/attachment:w-7 group-data-[size=xs]/attachment:rounded-lg group-data-[state=error]/attachment:bg-destructive/10 group-data-[state=error]/attachment:text-destructive group-data-[orientation=vertical]/attachment:*:data-[slot=spinner]:size-6! [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 group-data-[orientation=vertical]/attachment:[&_svg:not([class*='size-'])]:size-6 group-data-[size=xs]/attachment:[&_svg:not([class*='size-'])]:size-3.5",
{
variants: {
variant: {
icon: "",
image:
"opacity-60 group-data-[state=done]/attachment:opacity-100 group-data-[state=idle]/attachment:opacity-100 *:[img]:aspect-square *:[img]:w-full *:[img]:object-cover",
},
},
defaultVariants: {
variant: "icon",
},
}
)
function AttachmentMedia({
className,
variant = "icon",
...props
}: React.ComponentProps<"div"> & VariantProps<typeof attachmentMediaVariants>) {
return (
<div
data-slot="attachment-media"
data-variant={variant}
className={cn(attachmentMediaVariants({ variant }), className)}
{...props}
/>
)
}
function AttachmentContent({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="attachment-content"
className={cn(
"max-w-full min-w-0 flex-1 group-data-[orientation=vertical]/attachment:px-1",
className
)}
{...props}
/>
)
}
function AttachmentTitle({
className,
...props
}: React.ComponentProps<"span">) {
return (
<span
data-slot="attachment-title"
className={cn(
"block max-w-full min-w-0 truncate font-medium group-data-[state=processing]/attachment:shimmer group-data-[state=uploading]/attachment:shimmer",
className
)}
{...props}
/>
)
}
function AttachmentDescription({
className,
...props
}: React.ComponentProps<"span">) {
return (
<span
data-slot="attachment-description"
className={cn(
"mt-0.5 block min-w-0 truncate text-xs text-muted-foreground group-data-[state=error]/attachment:text-destructive/80",
"max-w-full",
className
)}
{...props}
/>
)
}
function AttachmentActions({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="attachment-actions"
className={cn(
"relative z-20 flex shrink-0 items-center group-data-[orientation=vertical]/attachment:absolute group-data-[orientation=vertical]/attachment:top-3 group-data-[orientation=vertical]/attachment:end-3 group-data-[orientation=vertical]/attachment:gap-1",
className
)}
{...props}
/>
)
}
function AttachmentAction({
className,
variant,
size = "icon-xs",
...props
}: React.ComponentProps<typeof Button>) {
return (
<Button
data-slot="attachment-action"
variant={variant ?? "ghost"}
size={size}
className={cn(className)}
{...props}
/>
)
}
function AttachmentTrigger({
className,
render,
type,
...props
}: useRender.ComponentProps<"button">) {
return useRender({
defaultTagName: "button",
props: mergeProps<"button">(
{
type: render ? type : (type ?? "button"),
className: cn("absolute inset-0 z-10 outline-none", className),
},
props
),
render,
state: {
slot: "attachment-trigger",
},
})
}
function AttachmentGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="attachment-group"
className={cn(
"flex min-w-0 scroll-fade-x snap-x snap-mandatory scroll-px-1 scrollbar-none gap-3 overflow-x-auto overscroll-x-contain py-1 *:data-[slot=attachment]:flex-none *:data-[slot=attachment]:snap-start",
className
)}
{...props}
/>
)
}
export {
Attachment,
AttachmentGroup,
AttachmentMedia,
AttachmentContent,
AttachmentTitle,
AttachmentDescription,
AttachmentActions,
AttachmentAction,
AttachmentTrigger,
}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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
3. 根据你的项目配置调整导入路径。
用法
import {
Attachment,
AttachmentAction,
AttachmentActions,
AttachmentContent,
AttachmentDescription,
AttachmentMedia,
AttachmentTitle,
} from "@/components/ui/attachment"2
3
4
5
6
7
8
9
<Attachment>
<AttachmentMedia>
<FileTextIcon />
</AttachmentMedia>
<AttachmentContent>
<AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
<AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
</AttachmentContent>
<AttachmentActions>
<AttachmentAction aria-label="Remove sales-dashboard.pdf">
<XIcon />
</AttachmentAction>
</AttachmentActions>
</Attachment>2
3
4
5
6
7
8
9
10
11
12
13
14
结构
按以下结构组合附件:
Attachment
├── AttachmentMedia
├── AttachmentContent
│ ├── AttachmentTitle
│ └── AttachmentDescription
├── AttachmentActions
│ └── AttachmentAction
└── AttachmentTrigger2
3
4
5
6
7
8
使用 AttachmentGroup 将多个附件排成一行,支持滚动:
AttachmentGroup
├── Attachment
└── Attachment2
3
功能
- 通过
AttachmentMedia显示图标和图像 - 内置
idle、uploading、processing、error和done上传状态的样式,进行中显示流光效果 - 支持三种尺寸,以及水平或垂直布局
- 覆盖整张卡片的
AttachmentTrigger可打开链接或对话框,各个操作仍可单独点击 AttachmentGroup支持滚动、吸附和边缘渐隐- 每个部分均可通过
className属性自定义样式
图像
在 AttachmentMedia 上设置 variant="image" 并在其中渲染 <img>。使用 orientation="vertical" 将媒体放在内容上方。
状态
设置 state 表示上传进度。uploading 和 processing 会为标题加上流光效果,error 则会切换到错误样式。
尺寸
使用 size 在 default、sm 和 xs 之间切换。
分组
用 AttachmentGroup 包裹附件,即可将它们排成支持横向滚动、吸附和边缘渐隐的一行。
触发器
添加 AttachmentTrigger 后,点击卡片任意位置即可打开链接或对话框。它覆盖卡片并位于操作元素下方,因此仍可点击各个操作。
<Dialog>
<Attachment>
{/* media, content, actions */}
<DialogTrigger
render={<AttachmentTrigger aria-label="Preview research-summary.pdf" />}
/>
</Attachment>
<DialogContent>{/* ... */}</DialogContent>
</Dialog>2
3
4
5
6
7
8
9
无障碍
AttachmentAction 渲染为 Button,AttachmentTrigger 渲染为原生 <button>(也可通过 render 使用自定义元素)。按照以下指引,让二者都可操作,也能被辅助技术播报。
为纯图标操作添加标签
AttachmentAction 通常只显示图标,因此应为每个操作设置 aria-label,说明操作及其目标。
<AttachmentAction aria-label="Remove sales-dashboard.pdf">
<XIcon />
</AttachmentAction>2
3
为触发器添加标签
AttachmentTrigger 覆盖整张卡片,本身没有文字,因此应通过 aria-label 说明触发后的效果。
<AttachmentTrigger
render={
<a
href={url}
target="_blank"
rel="noreferrer"
aria-label="Open workspace.png"
/>
}
/>2
3
4
5
6
7
8
9
10
触发器的层叠顺序低于操作元素,因此 AttachmentAction 和 AttachmentTrigger 互不遮挡,两者仍可分别聚焦和点击。
键盘滚动
AttachmentGroup 支持横向滚动。附件具有触发器或操作等交互时,键盘用户可通过 Tab 键聚焦屏幕外的条目。对于一排仅供展示的附件,请为组本身添加 tabIndex={0}、role="group" 和 aria-label,使其可聚焦和滚动。
用文字补充颜色信息
error 状态使用错误颜色。请在 AttachmentDescription 中保留失败原因,让用户也能通过文字得知状态。
API 参考
Attachment
附件的根容器。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
state | "idle" | "uploading" | "processing" | "error" | "done" | "done" | 上传状态,决定样式和流光效果。 |
size | "default" | "sm" | "xs" | "default" | 附件尺寸。 |
orientation | "horizontal" | "vertical" | "horizontal" | 将媒体放在内容旁边或上方。 |
className | string | - | 应用于根元素的附加类。 |
AttachmentMedia
用于图标或图像预览的媒体插槽。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
variant | "icon" | "image" | "icon" | 媒体显示图标还是 <img>。 |
className | string | - | 应用于媒体插槽的附加类。 |
AttachmentContent
包裹标题和说明。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于内容插槽的附加类。 |
AttachmentTitle
附件名称。在附件处于 uploading 或 processing 状态时显示流光效果。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于标题的附加类。 |
AttachmentDescription
文件类型、大小或上传状态等辅助元数据。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于说明的附加类。 |
AttachmentActions
容纳一个或多个操作的容器,对齐到附件末端。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于操作容器的附加类。 |
AttachmentAction
操作按钮。渲染为 Button,并接受它的所有属性。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
size | Button["size"] | "icon-xs" | 按钮尺寸。 |
...props | React.ComponentProps<typeof Button> | - | 透传给底层 Button 的属性。 |
AttachmentTrigger
覆盖整张卡片,用于触发附件操作。默认渲染为 <button>。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
render | ReactElement | function | - | 渲染为其他元素,例如链接。 |
...props | React.ComponentProps<"button"> | - | 透传给触发元素的属性。 |
AttachmentGroup
将附件排成支持横向滚动和吸附的一行。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于组的附加类。 |