Back

File Preview

Display uploaded files with smart type detection, file icons, image thumbnails, and upload progress.

Category
CardReact
CSS
Tailwind

Manual

Create a file and paste the following code into it.

file-preview.tsx
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"use client";

import Image from "next/image";
import type { FC } from "react";
import {
	Archive01Icon,
	Cancel01Icon,
	DocumentCodeIcon,
	File01Icon,
	HugeiconsIcon,
	Image01Icon,
	Loading03Icon,
	MusicNote01Icon,
	Txt01Icon,
	Video01Icon,
} from "@/components/icons";
import { cn } from "@/lib/cn";

export interface UploadedFile {
	id: string;
	url: string;
	name: string;
	type: string;
	description?: string;
	isUploading?: boolean;
}

export interface FilePreviewProps {
	files: UploadedFile[];
	onRemove?: (id: string) => void;
	className?: string;
}

const getFileExtension = (fileName: string): string => {
	const parts = fileName.split(".");
	return parts.length > 1 ? parts[parts.length - 1] : "";
};

const getFileIcon = (fileType: string, fileName: string) => {
	const extension = getFileExtension(fileName).toLowerCase();
	const iconSize = 24;

	// Image files
	if (fileType.startsWith("image/"))
		return (
			<HugeiconsIcon
				icon={Image01Icon}
				size={iconSize}
				className="text-emerald-500 dark:text-emerald-400"
			/>
		);

	// Document files
	if (fileType === "application/pdf" || extension === "pdf")
		return (
			<HugeiconsIcon
				icon={Txt01Icon}
				size={iconSize}
				className="text-red-500 dark:text-red-400"
			/>
		);

	if (
		["doc", "docx", "odt", "rtf"].includes(extension) ||
		fileType.includes("wordprocessing") ||
		fileType.includes("msword")
	)
		return (
			<HugeiconsIcon
				icon={Txt01Icon}
				size={iconSize}
				className="text-blue-500 dark:text-blue-400"
			/>
		);

	// Spreadsheet files
	if (
		["xls", "xlsx", "csv", "ods"].includes(extension) ||
		fileType.includes("spreadsheet") ||
		fileType.includes("excel")
	)
		return (
			<HugeiconsIcon
				icon={Txt01Icon}
				size={iconSize}
				className="text-green-500 dark:text-green-400"
			/>
		);

	// Code/text files
	if (["txt", "md"].includes(extension) || fileType === "text/plain")
		return (
			<HugeiconsIcon
				icon={Txt01Icon}
				size={iconSize}
				className="text-zinc-500 dark:text-zinc-400"
			/>
		);

	if (
		[
			"js",
			"ts",
			"jsx",
			"tsx",
			"py",
			"java",
			"c",
			"cpp",
			"html",
			"css",
		].includes(extension) ||
		fileType.includes("javascript") ||
		fileType.includes("typescript")
	)
		return (
			<HugeiconsIcon
				icon={DocumentCodeIcon}
				size={iconSize}
				className="text-yellow-500 dark:text-yellow-400"
			/>
		);

	if (["json", "xml", "yaml", "yml"].includes(extension))
		return (
			<HugeiconsIcon
				icon={DocumentCodeIcon}
				size={iconSize}
				className="text-zinc-500 dark:text-zinc-400"
			/>
		);

	// Media files
	if (
		fileType.startsWith("video/") ||
		["mp4", "avi", "mov", "mkv"].includes(extension)
	)
		return (
			<HugeiconsIcon
				icon={Video01Icon}
				size={iconSize}
				className="text-purple-500 dark:text-purple-400"
			/>
		);

	if (
		fileType.startsWith("audio/") ||
		["mp3", "wav", "ogg"].includes(extension)
	)
		return (
			<HugeiconsIcon
				icon={MusicNote01Icon}
				size={iconSize}
				className="text-pink-500 dark:text-pink-400"
			/>
		);

	// Archive files
	if (
		["zip", "rar", "tar", "gz", "7z"].includes(extension) ||
		fileType.includes("archive") ||
		fileType.includes("compressed")
	)
		return (
			<HugeiconsIcon
				icon={Archive01Icon}
				size={iconSize}
				className="text-amber-500 dark:text-amber-400"
			/>
		);

	// Default fallback
	return (
		<HugeiconsIcon
			icon={File01Icon}
			size={iconSize}
			className="text-zinc-500 dark:text-zinc-400"
		/>
	);
};

const getFormattedFileType = (fileType: string, fileName: string): string => {
	const ext = getFileExtension(fileName).toUpperCase();

	if (fileType.includes("msword") || fileType.includes("wordprocessing"))
		return "DOC";

	if (fileType.includes("spreadsheet") || fileType.includes("excel"))
		return "SPREADSHEET";

	const typePart = fileType.split("/")[1];

	if (!typePart || typePart === "octet-stream") {
		return ext || "FILE";
	}

	const cleanType = typePart
		.replace("vnd.openxmlformats-officedocument.", "")
		.replace("vnd.ms-", "")
		.replace("x-", "")
		.replace("document.", "")
		.replace("presentation.", "")
		.replace("application.", "")
		.split(".")[0];

	return cleanType.toUpperCase().substring(0, 8);
};

export const FilePreview: FC<FilePreviewProps> = ({
	files,
	onRemove,
	className,
}) => {
	if (files.length === 0) return null;

	return (
		<div className={cn("flex w-full flex-col gap-2 rounded-xl p-2", className)}>
			<div className="flex w-full flex-wrap gap-2">
				{files.map((file) => (
					<div
						key={file.id}
						className={cn(
							// Base styles - each file is its own group for hover
							"group/file relative flex items-center rounded-xl transition-all",
							// Lighter background than composer (zinc-700 instead of zinc-800)
							// Light mode: zinc-100 -> zinc-200 on hover
							// Dark mode: zinc-700 -> zinc-600 on hover
							"bg-zinc-200 hover:bg-zinc-300 dark:bg-zinc-700 dark:hover:bg-zinc-600",
							file.type.startsWith("image/")
								? "h-14 w-14 justify-center"
								: "min-w-[180px] max-w-[220px] p-2 pr-8",
						)}
					>
						{/* Loading overlay */}
						{file.isUploading && (
							<div className="absolute inset-0 flex items-center justify-center rounded-xl bg-black/30 dark:bg-black/30">
								<HugeiconsIcon
									icon={Loading03Icon}
									size={20}
									className="animate-spin text-white"
								/>
							</div>
						)}

						{/* Remove button - cursor-pointer, only shows on this specific file's hover */}
						{onRemove && (
							<button
								type="button"
								onClick={() => onRemove(file.id)}
								className={cn(
									// Position and base styles
									"absolute -top-1 -right-1 z-10 flex h-5 w-5 items-center justify-center rounded-full",
									// Hidden by default, shows on group-hover/file
									"opacity-0 scale-75 transition-all duration-150 group-hover/file:opacity-100 group-hover/file:scale-100",
									// Background colors with cursor-pointer
									"cursor-pointer bg-zinc-400 hover:bg-zinc-500 dark:bg-zinc-500 dark:hover:bg-zinc-400",
								)}
								aria-label={`Remove ${file.name}`}
							>
								<HugeiconsIcon
									icon={Cancel01Icon}
									size={10}
									className="text-white"
								/>
							</button>
						)}

						{/* Image preview */}
						{file.type.startsWith("image/") ? (
							<div className="h-12 w-12 overflow-hidden rounded-md">
								<Image
									width={48}
									height={48}
									src={file.url}
									alt={file.name}
									className="h-full w-full object-cover"
								/>
							</div>
						) : (
							<>
								{/* File icon */}
								<div className="mr-3 flex h-10 w-10 items-center justify-center rounded-lg bg-zinc-300 dark:bg-zinc-600">
									{getFileIcon(file.type, file.name)}
								</div>
								{/* File info */}
								<div className="flex min-w-0 flex-1 flex-col">
									<p className="truncate text-sm font-medium text-zinc-800 dark:text-white">
										{file.name.length > 18
											? `${file.name.substring(0, 15)}...`
											: file.name}
									</p>
									<span className="text-xs text-zinc-600 dark:text-zinc-400">
										{getFormattedFileType(file.type, file.name)}
									</span>
								</div>
							</>
						)}
					</div>
				))}
			</div>
		</div>
	);
};

Update the import paths to match your project setup.

Similar screens