@vvedantb/ui 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/package.json +3 -2
- package/src/ai-elements/artifact.tsx +99 -99
- package/src/ai-elements/attachments.tsx +366 -366
- package/src/ai-elements/chain-of-thought.tsx +212 -212
- package/src/ai-elements/code-block.tsx +478 -478
- package/src/ai-elements/conversation.tsx +157 -157
- package/src/ai-elements/image.tsx +25 -25
- package/src/ai-elements/inline-citation.tsx +252 -252
- package/src/ai-elements/message.tsx +269 -269
- package/src/ai-elements/model-selector.tsx +211 -211
- package/src/ai-elements/prompt-input.tsx +1018 -1018
- package/src/ai-elements/queue.tsx +251 -251
- package/src/ai-elements/reasoning.tsx +178 -178
- package/src/ai-elements/shimmer.tsx +56 -56
- package/src/ai-elements/sources.tsx +77 -77
- package/src/ai-elements/speech-input.tsx +290 -290
- package/src/ai-elements/suggestion.tsx +191 -191
- package/src/css.d.ts +2 -2
- package/src/index.ts +96 -96
- package/src/motion/presets.ts +35 -35
- package/src/motion/presets.vibot.ts +105 -105
- package/src/ui/_menu-classes.ts +30 -30
- package/src/ui/_menu-classes.vibot.ts +33 -33
- package/src/ui/accordion.tsx +81 -81
- package/src/ui/accordion.verve.tsx +56 -56
- package/src/ui/badge.tsx +34 -34
- package/src/ui/breadcrumb.tsx +83 -83
- package/src/ui/button-group.tsx +84 -84
- package/src/ui/button.tsx +57 -57
- package/src/ui/card.tsx +27 -27
- package/src/ui/carousel.tsx +211 -211
- package/src/ui/checkbox.tsx +27 -27
- package/src/ui/clear-input.tsx +170 -170
- package/src/ui/clear-input.vibot.tsx +76 -76
- package/src/ui/clearInputDissolve.ts +214 -214
- package/src/ui/collapsible.tsx +7 -7
- package/src/ui/command.tsx +148 -148
- package/src/ui/context-menu.tsx +173 -173
- package/src/ui/dialog.tsx +149 -149
- package/src/ui/dropdown-menu.tsx +178 -178
- package/src/ui/hover-card.tsx +31 -31
- package/src/ui/input-group.tsx +162 -162
- package/src/ui/input-group.verve.tsx +150 -150
- package/src/ui/input.tsx +21 -21
- package/src/ui/kbd.tsx +18 -18
- package/src/ui/label.tsx +20 -20
- package/src/ui/labeled-switch-row.tsx +53 -53
- package/src/ui/modalTransition.ts +85 -85
- package/src/ui/pagination.tsx +47 -47
- package/src/ui/popover.tsx +33 -33
- package/src/ui/progress.tsx +25 -25
- package/src/ui/radix-select.tsx +202 -202
- package/src/ui/scroll-area.tsx +55 -55
- package/src/ui/scroll-area.verve.tsx +43 -43
- package/src/ui/search-input.tsx +29 -29
- package/src/ui/select.tsx +163 -163
- package/src/ui/separator.tsx +28 -28
- package/src/ui/sheet.tsx +100 -87
- package/src/ui/skeleton.tsx +18 -18
- package/src/ui/sonner.css +16 -16
- package/src/ui/sonner.tsx +47 -47
- package/src/ui/spinner.tsx +27 -27
- package/src/ui/surface-classes.ts +55 -55
- package/src/ui/switch.tsx +26 -26
- package/src/ui/table.tsx +123 -123
- package/src/ui/tabs.tsx +140 -140
- package/src/ui/tabsSliding.ts +56 -56
- package/src/ui/textarea.tsx +21 -21
- package/src/ui/time-picker.tsx +170 -170
- package/src/ui/tooltip.tsx +25 -25
- package/src/ui/visually-hidden.tsx +3 -3
- package/src/utils/cn.ts +16 -16
- package/src/utils/ref.ts +10 -10
|
@@ -1,366 +1,366 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import type { FileUIPart, SourceDocumentUIPart } from "ai";
|
|
3
|
-
import type { ComponentProps, HTMLAttributes, ReactNode } from "react";
|
|
4
|
-
import { Button } from "../ui/button";
|
|
5
|
-
import {
|
|
6
|
-
HoverCard,
|
|
7
|
-
HoverCardContent,
|
|
8
|
-
HoverCardTrigger,
|
|
9
|
-
} from "../ui/hover-card";
|
|
10
|
-
import { cn } from "../utils/cn";
|
|
11
|
-
import {
|
|
12
|
-
flatElevatedSurfaceHoverClass,
|
|
13
|
-
flatPanelSubtleClass,
|
|
14
|
-
} from "../ui/surface-classes";
|
|
15
|
-
import {
|
|
16
|
-
IconFileText,
|
|
17
|
-
IconMusic,
|
|
18
|
-
IconPaperclip,
|
|
19
|
-
IconPhoto,
|
|
20
|
-
IconVideo,
|
|
21
|
-
IconWorld,
|
|
22
|
-
IconX,
|
|
23
|
-
} from "@tabler/icons-react";
|
|
24
|
-
import { createContext, useContext } from "react";
|
|
25
|
-
// ============================================================================
|
|
26
|
-
// Types
|
|
27
|
-
// ============================================================================
|
|
28
|
-
export type AttachmentData =
|
|
29
|
-
| (FileUIPart & {
|
|
30
|
-
id: string;
|
|
31
|
-
})
|
|
32
|
-
| (SourceDocumentUIPart & {
|
|
33
|
-
id: string;
|
|
34
|
-
});
|
|
35
|
-
export type AttachmentMediaCategory =
|
|
36
|
-
| "image"
|
|
37
|
-
| "video"
|
|
38
|
-
| "audio"
|
|
39
|
-
| "document"
|
|
40
|
-
| "source"
|
|
41
|
-
| "unknown";
|
|
42
|
-
export type AttachmentVariant = "grid" | "inline" | "list";
|
|
43
|
-
const mediaCategoryIcons: Record<AttachmentMediaCategory, typeof IconPhoto> = {
|
|
44
|
-
audio: IconMusic,
|
|
45
|
-
document: IconFileText,
|
|
46
|
-
image: IconPhoto,
|
|
47
|
-
source: IconWorld,
|
|
48
|
-
unknown: IconPaperclip,
|
|
49
|
-
video: IconVideo,
|
|
50
|
-
};
|
|
51
|
-
// ============================================================================
|
|
52
|
-
// Utility Functions
|
|
53
|
-
// ============================================================================
|
|
54
|
-
export const getMediaCategory = (
|
|
55
|
-
data: AttachmentData,
|
|
56
|
-
): AttachmentMediaCategory => {
|
|
57
|
-
if (data.type === "source-document") {
|
|
58
|
-
return "source";
|
|
59
|
-
}
|
|
60
|
-
const mediaType = data.mediaType ?? "";
|
|
61
|
-
if (mediaType.startsWith("image/")) {
|
|
62
|
-
return "image";
|
|
63
|
-
}
|
|
64
|
-
if (mediaType.startsWith("video/")) {
|
|
65
|
-
return "video";
|
|
66
|
-
}
|
|
67
|
-
if (mediaType.startsWith("audio/")) {
|
|
68
|
-
return "audio";
|
|
69
|
-
}
|
|
70
|
-
if (mediaType.startsWith("application/") || mediaType.startsWith("text/")) {
|
|
71
|
-
return "document";
|
|
72
|
-
}
|
|
73
|
-
return "unknown";
|
|
74
|
-
};
|
|
75
|
-
export const getAttachmentLabel = (data: AttachmentData): string => {
|
|
76
|
-
if (data.type === "source-document") {
|
|
77
|
-
return data.title || data.filename || "Source";
|
|
78
|
-
}
|
|
79
|
-
const category = getMediaCategory(data);
|
|
80
|
-
return data.filename || (category === "image" ? "Image" : "Attachment");
|
|
81
|
-
};
|
|
82
|
-
const renderAttachmentImage = (
|
|
83
|
-
url: string,
|
|
84
|
-
filename: string | undefined,
|
|
85
|
-
isGrid: boolean,
|
|
86
|
-
) =>
|
|
87
|
-
isGrid ? (
|
|
88
|
-
<img
|
|
89
|
-
alt={filename || "Image"}
|
|
90
|
-
className="size-full object-cover"
|
|
91
|
-
height={96}
|
|
92
|
-
src={url}
|
|
93
|
-
width={96}
|
|
94
|
-
/>
|
|
95
|
-
) : (
|
|
96
|
-
<img
|
|
97
|
-
alt={filename || "Image"}
|
|
98
|
-
className="size-full rounded object-cover"
|
|
99
|
-
height={20}
|
|
100
|
-
src={url}
|
|
101
|
-
width={20}
|
|
102
|
-
/>
|
|
103
|
-
);
|
|
104
|
-
// ============================================================================
|
|
105
|
-
// Contexts
|
|
106
|
-
// ============================================================================
|
|
107
|
-
interface AttachmentsContextValue {
|
|
108
|
-
variant: AttachmentVariant;
|
|
109
|
-
}
|
|
110
|
-
const AttachmentsContext = createContext<AttachmentsContextValue | null>(null);
|
|
111
|
-
interface AttachmentContextValue {
|
|
112
|
-
data: AttachmentData;
|
|
113
|
-
mediaCategory: AttachmentMediaCategory;
|
|
114
|
-
onRemove?: () => void;
|
|
115
|
-
variant: AttachmentVariant;
|
|
116
|
-
}
|
|
117
|
-
const AttachmentContext = createContext<AttachmentContextValue | null>(null);
|
|
118
|
-
// ============================================================================
|
|
119
|
-
// Hooks
|
|
120
|
-
// ============================================================================
|
|
121
|
-
export const useAttachmentsContext = () =>
|
|
122
|
-
useContext(AttachmentsContext) ?? { variant: "grid" as const };
|
|
123
|
-
export const useAttachmentContext = () => {
|
|
124
|
-
const ctx = useContext(AttachmentContext);
|
|
125
|
-
if (!ctx) {
|
|
126
|
-
throw new Error("Attachment components must be used within <Attachment>");
|
|
127
|
-
}
|
|
128
|
-
return ctx;
|
|
129
|
-
};
|
|
130
|
-
// ============================================================================
|
|
131
|
-
// Attachments - Container
|
|
132
|
-
// ============================================================================
|
|
133
|
-
export type AttachmentsProps = HTMLAttributes<HTMLDivElement> & {
|
|
134
|
-
variant?: AttachmentVariant;
|
|
135
|
-
};
|
|
136
|
-
export const Attachments = ({
|
|
137
|
-
variant = "grid",
|
|
138
|
-
className,
|
|
139
|
-
children,
|
|
140
|
-
...props
|
|
141
|
-
}: AttachmentsProps) => {
|
|
142
|
-
const contextValue = { variant };
|
|
143
|
-
return (
|
|
144
|
-
<AttachmentsContext.Provider value={contextValue}>
|
|
145
|
-
<div
|
|
146
|
-
className={cn(
|
|
147
|
-
"flex items-start",
|
|
148
|
-
variant === "list" ? "flex-col gap-2" : "flex-wrap gap-2",
|
|
149
|
-
variant === "grid" && "ml-auto w-fit",
|
|
150
|
-
className,
|
|
151
|
-
)}
|
|
152
|
-
{...props}
|
|
153
|
-
>
|
|
154
|
-
{children}
|
|
155
|
-
</div>
|
|
156
|
-
</AttachmentsContext.Provider>
|
|
157
|
-
);
|
|
158
|
-
};
|
|
159
|
-
// ============================================================================
|
|
160
|
-
// Attachment - Item
|
|
161
|
-
// ============================================================================
|
|
162
|
-
export type AttachmentProps = HTMLAttributes<HTMLDivElement> & {
|
|
163
|
-
data: AttachmentData;
|
|
164
|
-
onRemove?: () => void;
|
|
165
|
-
};
|
|
166
|
-
export const Attachment = ({
|
|
167
|
-
data,
|
|
168
|
-
onRemove,
|
|
169
|
-
className,
|
|
170
|
-
children,
|
|
171
|
-
...props
|
|
172
|
-
}: AttachmentProps) => {
|
|
173
|
-
const { variant } = useAttachmentsContext();
|
|
174
|
-
const mediaCategory = getMediaCategory(data);
|
|
175
|
-
const contextValue = { data, mediaCategory, onRemove, variant };
|
|
176
|
-
return (
|
|
177
|
-
<AttachmentContext.Provider value={contextValue}>
|
|
178
|
-
<div
|
|
179
|
-
className={cn(
|
|
180
|
-
"group relative",
|
|
181
|
-
variant === "grid" && "size-24 overflow-hidden rounded-lg",
|
|
182
|
-
variant === "inline" && [
|
|
183
|
-
"flex h-8 cursor-pointer select-none items-center gap-1.5",
|
|
184
|
-
"rounded-md px-1.5",
|
|
185
|
-
"font-medium text-sm transition-[background-color,color,transform] duration-150 ease-out",
|
|
186
|
-
"active:scale-[0.97] hover:bg-surface-secondary hover:text-foreground",
|
|
187
|
-
],
|
|
188
|
-
variant === "list" && [
|
|
189
|
-
"flex w-full items-center gap-3 rounded-lg p-3",
|
|
190
|
-
"transition-[background-color,transform] duration-150 ease-out active:scale-[0.99] hover:bg-surface-secondary",
|
|
191
|
-
],
|
|
192
|
-
className,
|
|
193
|
-
)}
|
|
194
|
-
{...props}
|
|
195
|
-
>
|
|
196
|
-
{children}
|
|
197
|
-
</div>
|
|
198
|
-
</AttachmentContext.Provider>
|
|
199
|
-
);
|
|
200
|
-
};
|
|
201
|
-
// ============================================================================
|
|
202
|
-
// AttachmentPreview - Media preview
|
|
203
|
-
// ============================================================================
|
|
204
|
-
export type AttachmentPreviewProps = HTMLAttributes<HTMLDivElement> & {
|
|
205
|
-
fallbackIcon?: ReactNode;
|
|
206
|
-
};
|
|
207
|
-
export const AttachmentPreview = ({
|
|
208
|
-
fallbackIcon,
|
|
209
|
-
className,
|
|
210
|
-
...props
|
|
211
|
-
}: AttachmentPreviewProps) => {
|
|
212
|
-
const { data, mediaCategory, variant } = useAttachmentContext();
|
|
213
|
-
const iconSize = variant === "inline" ? "size-3" : "size-4";
|
|
214
|
-
const renderIcon = (Icon: typeof IconPhoto) => (
|
|
215
|
-
<Icon className={cn(iconSize, "text-muted")} />
|
|
216
|
-
);
|
|
217
|
-
const renderContent = () => {
|
|
218
|
-
if (mediaCategory === "image" && data.type === "file" && data.url) {
|
|
219
|
-
return renderAttachmentImage(data.url, data.filename, variant === "grid");
|
|
220
|
-
}
|
|
221
|
-
if (mediaCategory === "video" && data.type === "file" && data.url) {
|
|
222
|
-
return <video className="size-full object-cover" muted src={data.url} />;
|
|
223
|
-
}
|
|
224
|
-
const Icon = mediaCategoryIcons[mediaCategory];
|
|
225
|
-
return fallbackIcon ?? renderIcon(Icon);
|
|
226
|
-
};
|
|
227
|
-
return (
|
|
228
|
-
<div
|
|
229
|
-
className={cn(
|
|
230
|
-
"flex shrink-0 items-center justify-center overflow-hidden",
|
|
231
|
-
variant === "grid" && cn("size-full", flatPanelSubtleClass),
|
|
232
|
-
variant === "inline" && cn("size-5 rounded", flatPanelSubtleClass),
|
|
233
|
-
variant === "list" && cn("size-12 rounded", flatPanelSubtleClass),
|
|
234
|
-
className,
|
|
235
|
-
)}
|
|
236
|
-
{...props}
|
|
237
|
-
>
|
|
238
|
-
{renderContent()}
|
|
239
|
-
</div>
|
|
240
|
-
);
|
|
241
|
-
};
|
|
242
|
-
// ============================================================================
|
|
243
|
-
// AttachmentInfo - Name and type display
|
|
244
|
-
// ============================================================================
|
|
245
|
-
export type AttachmentInfoProps = HTMLAttributes<HTMLDivElement> & {
|
|
246
|
-
showMediaType?: boolean;
|
|
247
|
-
};
|
|
248
|
-
export const AttachmentInfo = ({
|
|
249
|
-
showMediaType = false,
|
|
250
|
-
className,
|
|
251
|
-
...props
|
|
252
|
-
}: AttachmentInfoProps) => {
|
|
253
|
-
const { data, variant } = useAttachmentContext();
|
|
254
|
-
const label = getAttachmentLabel(data);
|
|
255
|
-
if (variant === "grid") {
|
|
256
|
-
return null;
|
|
257
|
-
}
|
|
258
|
-
return (
|
|
259
|
-
<div className={cn("min-w-0 flex-1", className)} {...props}>
|
|
260
|
-
<span className="block truncate">{label}</span>
|
|
261
|
-
{showMediaType && data.mediaType && (
|
|
262
|
-
<span className="block truncate text-muted text-xs">
|
|
263
|
-
{data.mediaType}
|
|
264
|
-
</span>
|
|
265
|
-
)}
|
|
266
|
-
</div>
|
|
267
|
-
);
|
|
268
|
-
};
|
|
269
|
-
// ============================================================================
|
|
270
|
-
// AttachmentRemove - Remove button
|
|
271
|
-
// ============================================================================
|
|
272
|
-
export type AttachmentRemoveProps = ComponentProps<typeof Button> & {
|
|
273
|
-
label?: string;
|
|
274
|
-
};
|
|
275
|
-
export const AttachmentRemove = ({
|
|
276
|
-
label = "Remove",
|
|
277
|
-
className,
|
|
278
|
-
children,
|
|
279
|
-
...props
|
|
280
|
-
}: AttachmentRemoveProps) => {
|
|
281
|
-
const { onRemove, variant } = useAttachmentContext();
|
|
282
|
-
const handleClick = (e: React.MouseEvent) => {
|
|
283
|
-
e.stopPropagation();
|
|
284
|
-
onRemove?.();
|
|
285
|
-
};
|
|
286
|
-
if (!onRemove) {
|
|
287
|
-
return null;
|
|
288
|
-
}
|
|
289
|
-
return (
|
|
290
|
-
<Button
|
|
291
|
-
aria-label={label}
|
|
292
|
-
className={cn(
|
|
293
|
-
variant === "grid" && [
|
|
294
|
-
"absolute top-2 right-2 size-6 rounded-full p-0",
|
|
295
|
-
cn(flatPanelSubtleClass, flatElevatedSurfaceHoverClass),
|
|
296
|
-
"opacity-0 transition-opacity group-hover:opacity-100",
|
|
297
|
-
"[&>svg]:size-3",
|
|
298
|
-
],
|
|
299
|
-
variant === "inline" && [
|
|
300
|
-
"size-5 rounded p-0",
|
|
301
|
-
"opacity-0 transition-opacity group-hover:opacity-100",
|
|
302
|
-
"[&>svg]:size-2.5",
|
|
303
|
-
],
|
|
304
|
-
variant === "list" && ["size-8 shrink-0 rounded p-0", "[&>svg]:size-4"],
|
|
305
|
-
className,
|
|
306
|
-
)}
|
|
307
|
-
onClick={handleClick}
|
|
308
|
-
type="button"
|
|
309
|
-
variant="ghost"
|
|
310
|
-
{...props}
|
|
311
|
-
>
|
|
312
|
-
{children ?? <IconX />}
|
|
313
|
-
<span className="sr-only">{label}</span>
|
|
314
|
-
</Button>
|
|
315
|
-
);
|
|
316
|
-
};
|
|
317
|
-
// ============================================================================
|
|
318
|
-
// AttachmentHoverCard - Hover preview
|
|
319
|
-
// ============================================================================
|
|
320
|
-
export type AttachmentHoverCardProps = ComponentProps<typeof HoverCard>;
|
|
321
|
-
export const AttachmentHoverCard = ({
|
|
322
|
-
openDelay = 0,
|
|
323
|
-
closeDelay = 0,
|
|
324
|
-
...props
|
|
325
|
-
}: AttachmentHoverCardProps) => (
|
|
326
|
-
<HoverCard closeDelay={closeDelay} openDelay={openDelay} {...props} />
|
|
327
|
-
);
|
|
328
|
-
export type AttachmentHoverCardTriggerProps = ComponentProps<
|
|
329
|
-
typeof HoverCardTrigger
|
|
330
|
-
>;
|
|
331
|
-
export const AttachmentHoverCardTrigger = (
|
|
332
|
-
props: AttachmentHoverCardTriggerProps,
|
|
333
|
-
) => <HoverCardTrigger {...props} />;
|
|
334
|
-
export type AttachmentHoverCardContentProps = ComponentProps<
|
|
335
|
-
typeof HoverCardContent
|
|
336
|
-
>;
|
|
337
|
-
export const AttachmentHoverCardContent = ({
|
|
338
|
-
align = "start",
|
|
339
|
-
className,
|
|
340
|
-
...props
|
|
341
|
-
}: AttachmentHoverCardContentProps) => (
|
|
342
|
-
<HoverCardContent
|
|
343
|
-
align={align}
|
|
344
|
-
className={cn("w-auto p-2", className)}
|
|
345
|
-
{...props}
|
|
346
|
-
/>
|
|
347
|
-
);
|
|
348
|
-
// ============================================================================
|
|
349
|
-
// AttachmentEmpty - Empty state
|
|
350
|
-
// ============================================================================
|
|
351
|
-
export type AttachmentEmptyProps = HTMLAttributes<HTMLDivElement>;
|
|
352
|
-
export const AttachmentEmpty = ({
|
|
353
|
-
className,
|
|
354
|
-
children,
|
|
355
|
-
...props
|
|
356
|
-
}: AttachmentEmptyProps) => (
|
|
357
|
-
<div
|
|
358
|
-
className={cn(
|
|
359
|
-
"flex items-center justify-center p-4 text-muted text-sm",
|
|
360
|
-
className,
|
|
361
|
-
)}
|
|
362
|
-
{...props}
|
|
363
|
-
>
|
|
364
|
-
{children ?? "No attachments"}
|
|
365
|
-
</div>
|
|
366
|
-
);
|
|
1
|
+
"use client";
|
|
2
|
+
import type { FileUIPart, SourceDocumentUIPart } from "ai";
|
|
3
|
+
import type { ComponentProps, HTMLAttributes, ReactNode } from "react";
|
|
4
|
+
import { Button } from "../ui/button";
|
|
5
|
+
import {
|
|
6
|
+
HoverCard,
|
|
7
|
+
HoverCardContent,
|
|
8
|
+
HoverCardTrigger,
|
|
9
|
+
} from "../ui/hover-card";
|
|
10
|
+
import { cn } from "../utils/cn";
|
|
11
|
+
import {
|
|
12
|
+
flatElevatedSurfaceHoverClass,
|
|
13
|
+
flatPanelSubtleClass,
|
|
14
|
+
} from "../ui/surface-classes";
|
|
15
|
+
import {
|
|
16
|
+
IconFileText,
|
|
17
|
+
IconMusic,
|
|
18
|
+
IconPaperclip,
|
|
19
|
+
IconPhoto,
|
|
20
|
+
IconVideo,
|
|
21
|
+
IconWorld,
|
|
22
|
+
IconX,
|
|
23
|
+
} from "@tabler/icons-react";
|
|
24
|
+
import { createContext, useContext } from "react";
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Types
|
|
27
|
+
// ============================================================================
|
|
28
|
+
export type AttachmentData =
|
|
29
|
+
| (FileUIPart & {
|
|
30
|
+
id: string;
|
|
31
|
+
})
|
|
32
|
+
| (SourceDocumentUIPart & {
|
|
33
|
+
id: string;
|
|
34
|
+
});
|
|
35
|
+
export type AttachmentMediaCategory =
|
|
36
|
+
| "image"
|
|
37
|
+
| "video"
|
|
38
|
+
| "audio"
|
|
39
|
+
| "document"
|
|
40
|
+
| "source"
|
|
41
|
+
| "unknown";
|
|
42
|
+
export type AttachmentVariant = "grid" | "inline" | "list";
|
|
43
|
+
const mediaCategoryIcons: Record<AttachmentMediaCategory, typeof IconPhoto> = {
|
|
44
|
+
audio: IconMusic,
|
|
45
|
+
document: IconFileText,
|
|
46
|
+
image: IconPhoto,
|
|
47
|
+
source: IconWorld,
|
|
48
|
+
unknown: IconPaperclip,
|
|
49
|
+
video: IconVideo,
|
|
50
|
+
};
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// Utility Functions
|
|
53
|
+
// ============================================================================
|
|
54
|
+
export const getMediaCategory = (
|
|
55
|
+
data: AttachmentData,
|
|
56
|
+
): AttachmentMediaCategory => {
|
|
57
|
+
if (data.type === "source-document") {
|
|
58
|
+
return "source";
|
|
59
|
+
}
|
|
60
|
+
const mediaType = data.mediaType ?? "";
|
|
61
|
+
if (mediaType.startsWith("image/")) {
|
|
62
|
+
return "image";
|
|
63
|
+
}
|
|
64
|
+
if (mediaType.startsWith("video/")) {
|
|
65
|
+
return "video";
|
|
66
|
+
}
|
|
67
|
+
if (mediaType.startsWith("audio/")) {
|
|
68
|
+
return "audio";
|
|
69
|
+
}
|
|
70
|
+
if (mediaType.startsWith("application/") || mediaType.startsWith("text/")) {
|
|
71
|
+
return "document";
|
|
72
|
+
}
|
|
73
|
+
return "unknown";
|
|
74
|
+
};
|
|
75
|
+
export const getAttachmentLabel = (data: AttachmentData): string => {
|
|
76
|
+
if (data.type === "source-document") {
|
|
77
|
+
return data.title || data.filename || "Source";
|
|
78
|
+
}
|
|
79
|
+
const category = getMediaCategory(data);
|
|
80
|
+
return data.filename || (category === "image" ? "Image" : "Attachment");
|
|
81
|
+
};
|
|
82
|
+
const renderAttachmentImage = (
|
|
83
|
+
url: string,
|
|
84
|
+
filename: string | undefined,
|
|
85
|
+
isGrid: boolean,
|
|
86
|
+
) =>
|
|
87
|
+
isGrid ? (
|
|
88
|
+
<img
|
|
89
|
+
alt={filename || "Image"}
|
|
90
|
+
className="size-full object-cover"
|
|
91
|
+
height={96}
|
|
92
|
+
src={url}
|
|
93
|
+
width={96}
|
|
94
|
+
/>
|
|
95
|
+
) : (
|
|
96
|
+
<img
|
|
97
|
+
alt={filename || "Image"}
|
|
98
|
+
className="size-full rounded object-cover"
|
|
99
|
+
height={20}
|
|
100
|
+
src={url}
|
|
101
|
+
width={20}
|
|
102
|
+
/>
|
|
103
|
+
);
|
|
104
|
+
// ============================================================================
|
|
105
|
+
// Contexts
|
|
106
|
+
// ============================================================================
|
|
107
|
+
interface AttachmentsContextValue {
|
|
108
|
+
variant: AttachmentVariant;
|
|
109
|
+
}
|
|
110
|
+
const AttachmentsContext = createContext<AttachmentsContextValue | null>(null);
|
|
111
|
+
interface AttachmentContextValue {
|
|
112
|
+
data: AttachmentData;
|
|
113
|
+
mediaCategory: AttachmentMediaCategory;
|
|
114
|
+
onRemove?: () => void;
|
|
115
|
+
variant: AttachmentVariant;
|
|
116
|
+
}
|
|
117
|
+
const AttachmentContext = createContext<AttachmentContextValue | null>(null);
|
|
118
|
+
// ============================================================================
|
|
119
|
+
// Hooks
|
|
120
|
+
// ============================================================================
|
|
121
|
+
export const useAttachmentsContext = () =>
|
|
122
|
+
useContext(AttachmentsContext) ?? { variant: "grid" as const };
|
|
123
|
+
export const useAttachmentContext = () => {
|
|
124
|
+
const ctx = useContext(AttachmentContext);
|
|
125
|
+
if (!ctx) {
|
|
126
|
+
throw new Error("Attachment components must be used within <Attachment>");
|
|
127
|
+
}
|
|
128
|
+
return ctx;
|
|
129
|
+
};
|
|
130
|
+
// ============================================================================
|
|
131
|
+
// Attachments - Container
|
|
132
|
+
// ============================================================================
|
|
133
|
+
export type AttachmentsProps = HTMLAttributes<HTMLDivElement> & {
|
|
134
|
+
variant?: AttachmentVariant;
|
|
135
|
+
};
|
|
136
|
+
export const Attachments = ({
|
|
137
|
+
variant = "grid",
|
|
138
|
+
className,
|
|
139
|
+
children,
|
|
140
|
+
...props
|
|
141
|
+
}: AttachmentsProps) => {
|
|
142
|
+
const contextValue = { variant };
|
|
143
|
+
return (
|
|
144
|
+
<AttachmentsContext.Provider value={contextValue}>
|
|
145
|
+
<div
|
|
146
|
+
className={cn(
|
|
147
|
+
"flex items-start",
|
|
148
|
+
variant === "list" ? "flex-col gap-2" : "flex-wrap gap-2",
|
|
149
|
+
variant === "grid" && "ml-auto w-fit",
|
|
150
|
+
className,
|
|
151
|
+
)}
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
{children}
|
|
155
|
+
</div>
|
|
156
|
+
</AttachmentsContext.Provider>
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
// ============================================================================
|
|
160
|
+
// Attachment - Item
|
|
161
|
+
// ============================================================================
|
|
162
|
+
export type AttachmentProps = HTMLAttributes<HTMLDivElement> & {
|
|
163
|
+
data: AttachmentData;
|
|
164
|
+
onRemove?: () => void;
|
|
165
|
+
};
|
|
166
|
+
export const Attachment = ({
|
|
167
|
+
data,
|
|
168
|
+
onRemove,
|
|
169
|
+
className,
|
|
170
|
+
children,
|
|
171
|
+
...props
|
|
172
|
+
}: AttachmentProps) => {
|
|
173
|
+
const { variant } = useAttachmentsContext();
|
|
174
|
+
const mediaCategory = getMediaCategory(data);
|
|
175
|
+
const contextValue = { data, mediaCategory, onRemove, variant };
|
|
176
|
+
return (
|
|
177
|
+
<AttachmentContext.Provider value={contextValue}>
|
|
178
|
+
<div
|
|
179
|
+
className={cn(
|
|
180
|
+
"group relative",
|
|
181
|
+
variant === "grid" && "size-24 overflow-hidden rounded-lg",
|
|
182
|
+
variant === "inline" && [
|
|
183
|
+
"flex h-8 cursor-pointer select-none items-center gap-1.5",
|
|
184
|
+
"rounded-md px-1.5",
|
|
185
|
+
"font-medium text-sm transition-[background-color,color,transform] duration-150 ease-out",
|
|
186
|
+
"active:scale-[0.97] hover:bg-surface-secondary hover:text-foreground",
|
|
187
|
+
],
|
|
188
|
+
variant === "list" && [
|
|
189
|
+
"flex w-full items-center gap-3 rounded-lg p-3",
|
|
190
|
+
"transition-[background-color,transform] duration-150 ease-out active:scale-[0.99] hover:bg-surface-secondary",
|
|
191
|
+
],
|
|
192
|
+
className,
|
|
193
|
+
)}
|
|
194
|
+
{...props}
|
|
195
|
+
>
|
|
196
|
+
{children}
|
|
197
|
+
</div>
|
|
198
|
+
</AttachmentContext.Provider>
|
|
199
|
+
);
|
|
200
|
+
};
|
|
201
|
+
// ============================================================================
|
|
202
|
+
// AttachmentPreview - Media preview
|
|
203
|
+
// ============================================================================
|
|
204
|
+
export type AttachmentPreviewProps = HTMLAttributes<HTMLDivElement> & {
|
|
205
|
+
fallbackIcon?: ReactNode;
|
|
206
|
+
};
|
|
207
|
+
export const AttachmentPreview = ({
|
|
208
|
+
fallbackIcon,
|
|
209
|
+
className,
|
|
210
|
+
...props
|
|
211
|
+
}: AttachmentPreviewProps) => {
|
|
212
|
+
const { data, mediaCategory, variant } = useAttachmentContext();
|
|
213
|
+
const iconSize = variant === "inline" ? "size-3" : "size-4";
|
|
214
|
+
const renderIcon = (Icon: typeof IconPhoto) => (
|
|
215
|
+
<Icon className={cn(iconSize, "text-muted")} />
|
|
216
|
+
);
|
|
217
|
+
const renderContent = () => {
|
|
218
|
+
if (mediaCategory === "image" && data.type === "file" && data.url) {
|
|
219
|
+
return renderAttachmentImage(data.url, data.filename, variant === "grid");
|
|
220
|
+
}
|
|
221
|
+
if (mediaCategory === "video" && data.type === "file" && data.url) {
|
|
222
|
+
return <video className="size-full object-cover" muted src={data.url} />;
|
|
223
|
+
}
|
|
224
|
+
const Icon = mediaCategoryIcons[mediaCategory];
|
|
225
|
+
return fallbackIcon ?? renderIcon(Icon);
|
|
226
|
+
};
|
|
227
|
+
return (
|
|
228
|
+
<div
|
|
229
|
+
className={cn(
|
|
230
|
+
"flex shrink-0 items-center justify-center overflow-hidden",
|
|
231
|
+
variant === "grid" && cn("size-full", flatPanelSubtleClass),
|
|
232
|
+
variant === "inline" && cn("size-5 rounded", flatPanelSubtleClass),
|
|
233
|
+
variant === "list" && cn("size-12 rounded", flatPanelSubtleClass),
|
|
234
|
+
className,
|
|
235
|
+
)}
|
|
236
|
+
{...props}
|
|
237
|
+
>
|
|
238
|
+
{renderContent()}
|
|
239
|
+
</div>
|
|
240
|
+
);
|
|
241
|
+
};
|
|
242
|
+
// ============================================================================
|
|
243
|
+
// AttachmentInfo - Name and type display
|
|
244
|
+
// ============================================================================
|
|
245
|
+
export type AttachmentInfoProps = HTMLAttributes<HTMLDivElement> & {
|
|
246
|
+
showMediaType?: boolean;
|
|
247
|
+
};
|
|
248
|
+
export const AttachmentInfo = ({
|
|
249
|
+
showMediaType = false,
|
|
250
|
+
className,
|
|
251
|
+
...props
|
|
252
|
+
}: AttachmentInfoProps) => {
|
|
253
|
+
const { data, variant } = useAttachmentContext();
|
|
254
|
+
const label = getAttachmentLabel(data);
|
|
255
|
+
if (variant === "grid") {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return (
|
|
259
|
+
<div className={cn("min-w-0 flex-1", className)} {...props}>
|
|
260
|
+
<span className="block truncate">{label}</span>
|
|
261
|
+
{showMediaType && data.mediaType && (
|
|
262
|
+
<span className="block truncate text-muted text-xs">
|
|
263
|
+
{data.mediaType}
|
|
264
|
+
</span>
|
|
265
|
+
)}
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
};
|
|
269
|
+
// ============================================================================
|
|
270
|
+
// AttachmentRemove - Remove button
|
|
271
|
+
// ============================================================================
|
|
272
|
+
export type AttachmentRemoveProps = ComponentProps<typeof Button> & {
|
|
273
|
+
label?: string;
|
|
274
|
+
};
|
|
275
|
+
export const AttachmentRemove = ({
|
|
276
|
+
label = "Remove",
|
|
277
|
+
className,
|
|
278
|
+
children,
|
|
279
|
+
...props
|
|
280
|
+
}: AttachmentRemoveProps) => {
|
|
281
|
+
const { onRemove, variant } = useAttachmentContext();
|
|
282
|
+
const handleClick = (e: React.MouseEvent) => {
|
|
283
|
+
e.stopPropagation();
|
|
284
|
+
onRemove?.();
|
|
285
|
+
};
|
|
286
|
+
if (!onRemove) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
return (
|
|
290
|
+
<Button
|
|
291
|
+
aria-label={label}
|
|
292
|
+
className={cn(
|
|
293
|
+
variant === "grid" && [
|
|
294
|
+
"absolute top-2 right-2 size-6 rounded-full p-0",
|
|
295
|
+
cn(flatPanelSubtleClass, flatElevatedSurfaceHoverClass),
|
|
296
|
+
"opacity-0 transition-opacity group-hover:opacity-100",
|
|
297
|
+
"[&>svg]:size-3",
|
|
298
|
+
],
|
|
299
|
+
variant === "inline" && [
|
|
300
|
+
"size-5 rounded p-0",
|
|
301
|
+
"opacity-0 transition-opacity group-hover:opacity-100",
|
|
302
|
+
"[&>svg]:size-2.5",
|
|
303
|
+
],
|
|
304
|
+
variant === "list" && ["size-8 shrink-0 rounded p-0", "[&>svg]:size-4"],
|
|
305
|
+
className,
|
|
306
|
+
)}
|
|
307
|
+
onClick={handleClick}
|
|
308
|
+
type="button"
|
|
309
|
+
variant="ghost"
|
|
310
|
+
{...props}
|
|
311
|
+
>
|
|
312
|
+
{children ?? <IconX />}
|
|
313
|
+
<span className="sr-only">{label}</span>
|
|
314
|
+
</Button>
|
|
315
|
+
);
|
|
316
|
+
};
|
|
317
|
+
// ============================================================================
|
|
318
|
+
// AttachmentHoverCard - Hover preview
|
|
319
|
+
// ============================================================================
|
|
320
|
+
export type AttachmentHoverCardProps = ComponentProps<typeof HoverCard>;
|
|
321
|
+
export const AttachmentHoverCard = ({
|
|
322
|
+
openDelay = 0,
|
|
323
|
+
closeDelay = 0,
|
|
324
|
+
...props
|
|
325
|
+
}: AttachmentHoverCardProps) => (
|
|
326
|
+
<HoverCard closeDelay={closeDelay} openDelay={openDelay} {...props} />
|
|
327
|
+
);
|
|
328
|
+
export type AttachmentHoverCardTriggerProps = ComponentProps<
|
|
329
|
+
typeof HoverCardTrigger
|
|
330
|
+
>;
|
|
331
|
+
export const AttachmentHoverCardTrigger = (
|
|
332
|
+
props: AttachmentHoverCardTriggerProps,
|
|
333
|
+
) => <HoverCardTrigger {...props} />;
|
|
334
|
+
export type AttachmentHoverCardContentProps = ComponentProps<
|
|
335
|
+
typeof HoverCardContent
|
|
336
|
+
>;
|
|
337
|
+
export const AttachmentHoverCardContent = ({
|
|
338
|
+
align = "start",
|
|
339
|
+
className,
|
|
340
|
+
...props
|
|
341
|
+
}: AttachmentHoverCardContentProps) => (
|
|
342
|
+
<HoverCardContent
|
|
343
|
+
align={align}
|
|
344
|
+
className={cn("w-auto p-2", className)}
|
|
345
|
+
{...props}
|
|
346
|
+
/>
|
|
347
|
+
);
|
|
348
|
+
// ============================================================================
|
|
349
|
+
// AttachmentEmpty - Empty state
|
|
350
|
+
// ============================================================================
|
|
351
|
+
export type AttachmentEmptyProps = HTMLAttributes<HTMLDivElement>;
|
|
352
|
+
export const AttachmentEmpty = ({
|
|
353
|
+
className,
|
|
354
|
+
children,
|
|
355
|
+
...props
|
|
356
|
+
}: AttachmentEmptyProps) => (
|
|
357
|
+
<div
|
|
358
|
+
className={cn(
|
|
359
|
+
"flex items-center justify-center p-4 text-muted text-sm",
|
|
360
|
+
className,
|
|
361
|
+
)}
|
|
362
|
+
{...props}
|
|
363
|
+
>
|
|
364
|
+
{children ?? "No attachments"}
|
|
365
|
+
</div>
|
|
366
|
+
);
|