file-attachment
agent-elements/file-attachmentFreeComponent
agent-elements publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by agent-elements on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://agent-elements.21st.dev/r/file-attachment.jsonSource
1import { useState } from "react";2import {3 IconX as X,4 IconFileText as FileText,5 IconFileCode as FileCode,6 IconFileTypeJs as FileJson,7 IconPhoto as ImageIcon,8} from "@tabler/icons-react";9import { cn } from "../utils/cn";10import { ImageLightbox } from "../image-lightbox";1112export type FileAttachmentProps = {13 id: string;14 filename: string;15 size?: number;16 isImage?: boolean;17 url?: string;18 onRemove?: () => void;19 className?: string;20 display?: "chip" | "image-only";21 /**22 * When true (default) clicking the image thumbnail opens a fullscreen23 * preview. Set to false to render a non-interactive thumbnail.24 */25 enableImagePreview?: boolean;26};2728function formatFileSize(bytes: number): string {29 if (bytes < 1024) return `${bytes} B`;30 if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;31 return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;32}3334type FileIconName = "image" | "code" | "data" | "text";3536function getFileIconName(filename: string, isImage?: boolean): FileIconName {37 if (isImage) return "image";3839 const ext = filename.split(".").pop()?.toLowerCase();4041 if (42 [43 "js",44 "ts",45 "jsx",46 "tsx",47 "py",48 "rb",49 "go",50 "rs",51 "java",52 "kt",53 "swift",54 "c",55 "cpp",56 "h",57 "hpp",58 "cs",59 "php",60 ].includes(ext || "")61 ) {62 return "code";63 }6465 if (["json", "yaml", "yml", "xml"].includes(ext || "")) {66 return "data";67 }6869 return "text";70}7172function renderFileIcon(iconName: FileIconName) {73 switch (iconName) {74 case "image":75 return <ImageIcon className="size-4 text-muted-foreground" />;76 case "code":77 return <FileCode className="size-4 text-muted-foreground" />;78 case "data":79 return <FileJson className="size-4 text-muted-foreground" />;80 default:81 return <FileText className="size-4 text-muted-foreground" />;82 }83}8485export function FileAttachment({86 id,87 filename,88 size,89 isImage,90 url,91 onRemove,92 className,93 display = "chip",94 enableImagePreview = true,95}: FileAttachmentProps) {96 const [isHovered, setIsHovered] = useState(false);97 const [isLightboxOpen, setIsLightboxOpen] = useState(false);98 const iconName = getFileIconName(filename, isImage);99 const isImageOnly = display === "image-only" && isImage && !!url;100 const canPreview = Boolean(enableImagePreview && isImage && url);101102 const openLightbox = (event: React.MouseEvent) => {103 event.stopPropagation();104// … truncated
More from agent-elements
All 25 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| agent-chatagent-chat | agent-elements | Components | Free | no deps | |
| attachment-buttonattachment-button | agent-elements | Components | Free | no deps | |
| bash-toolbash-tool | agent-elements | Components | Free | no deps | |
| edit-tooledit-tool | agent-elements | Components | Free | no deps | |
| error-messageerror-message | agent-elements | Components | Free | no deps | |
| generic-toolgeneric-tool | agent-elements | Components | Free | no deps | |
| input-barinput-bar | agent-elements | Components | Free | no deps | |
| markdownmarkdown | agent-elements | Components | Free | no deps |
