A file-upload component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import React, { useRef, useState } from "react";
4import { motion } from "motion/react";
5import { IconUpload } from "@tabler/icons-react";
6import { useDropzone } from "react-dropzone";
7
8const mainVariant = {
9 initial: {
10 x: 0,
11 y: 0,
12 },
13 animate: {
14 x: 20,
15 y: -20,
16 opacity: 0.9,
17 },
18};
19
20const secondaryVariant = {
21 initial: {
22 opacity: 0,
23 },
24 animate: {
25 opacity: 1,
26 },
27};
28
29export const FileUpload = ({
30 onChange,
31}: {
32 onChange?: (files: File[]) => void;
33}) => {
34 const [files, setFiles] = useState<File[]>([]);
35 const fileInputRef = useRef<HTMLInputElement>(null);
36
37 const handleFileChange = (newFiles: File[]) => {
38 setFiles((prevFiles) => [...prevFiles, ...newFiles]);
39 onChange && onChange(newFiles);
40 };
41
42 const handleClick = () => {
43 fileInputRef.current?.click();
44 };
45
46 const { getRootProps, isDragActive } = useDropzone({
47 multiple: false,
48 noClick: true,
49 onDrop: handleFileChange,
50 onDropRejected: (error) => {
51 console.log(error);
52 },
53 });
54
55 return (
56 <div className="w-full" {...getRootProps()}>
57 <motion.div
58 onClick={handleClick}
59 whileHover="animate"
60 className="group/file relative block w-full cursor-pointer overflow-hidden rounded-lg p-10"
61 >
62 <input
63 ref={fileInputRef}
64 id="file-upload-handle"
65 type="file"
66 onChange={(e) => handleFileChange(Array.from(e.target.files || []))}
67 className="hidden"
68 />
69 <div className="absolute inset-0 [mask-image:radial-gradient(ellipse_at_center,white,transparent)]">
70 <GridPattern />
71 </div>
72 <div className="flex flex-col items-center justify-center">
73 <p className="relative z-20 font-sans text-base font-bold text-neutral-700 dark:text-neutral-300">
74 Upload file
75 </p>
76 <p className="relative z-20 mt-2 font-sans text-base font-normal text-neutral-400 dark:text-neutral-400">
77 Drag or drop your files here or click to upload
78 </p>
79 <div className="relative mx-auto mt-10 w-full max-w-xl">
80 {files.length > 0 &&
81 files.map((file, idx) => (
82 <motion.div
83 key={"file" + idx}
84 layoutId={idx === 0 ? "file-upload" : "file-upload-" + idx}
85 className={cn(
86 "relative z-40 mx-auto mt-4 flex w-full flex-col items-start justify-start overflow-hidden rounded-md bg-white p-4 md:h-24 dark:bg-neutral-900",
87 "shadow-sm",
88 )}
89 >
90 <div className="flex w-full items-center justify-between gap-4">
91 <motion.p
92 initial={{ opacity: 0 }}
93 animate={{ opacity: 1 }}
94 layout
95 className="max-w-xs truncate text-base text-neutral-700 dark:text-neutral-300"
96 >
97 {file.name}
98 </motion.p>
99 <motion.p
100 initial={{ opacity: 0 }}
101 animate={{ opacity: 1 }}
102 layout
103 className="shadow-input w-fit shrink-0 rounded-lg px-2 py-1 text-sm text-neutral-600 dark:bg-neutral-800 dark:text-white"
104 >
105 {(file.size / (1024 * 1024)).toFixed(2)} MB
106 </motion.p>
107 </div>
108
109 <div className="mt-2 flex w-full flex-col items-start justify-between text-sm text-neutral-600 md:flex-row md:items-center dark:text-neutral-400">
110 <motion.p
111 initial={{ opacity: 0 }}
112 animate={{ opacity: 1 }}
113 layout
114 className="rounded-md bg-gray-100 px-1 py-0.5 dark:bg-neutral-800"
115 >
116 {file.type}
117 </motion.p>
118
119 <motion.p
120 initial={{ opacity: 0 }}
121 animate={{ opacity: 1 }}
122 layout
123 >
124 modified{" "}
125 {new Date(file.lastModified).toLocaleDateString()}
126 </motion.p>
127 </div>
128 </motion.div>
129 ))}
130 {!files.length && (
131 <motion.div
132 layoutId="file-upload"
133 variants={mainVariant}
134 transition={{
135 type: "spring",
136 stiffness: 300,
137 damping: 20,
138 }}
139 className={cn(
140 "relative z-40 mx-auto mt-4 flex h-32 w-full max-w-[8rem] items-center justify-center rounded-md bg-white group-hover/file:shadow-2xl dark:bg-neutral-900",
141 "shadow-[0px_10px_50px_rgba(0,0,0,0.1)]",
142 )}
143 >
144 {isDragActive ? (
145 <motion.p
146 initial={{ opacity: 0 }}
147 animate={{ opacity: 1 }}
148 className="flex flex-col items-center text-neutral-600"
149 >
150 Drop it
151 <IconUpload className="h-4 w-4 text-neutral-600 dark:text-neutral-400" />
152 </motion.p>
153 ) : (
154 <IconUpload className="h-4 w-4 text-neutral-600 dark:text-neutral-300" />
155 )}
156 </motion.div>
157 )}
158
159 {!files.length && (
160 <motion.div
161 variants={secondaryVariant}
162 className="absolute inset-0 z-30 mx-auto mt-4 flex h-32 w-full max-w-[8rem] items-center justify-center rounded-md border border-dashed border-sky-400 bg-transparent opacity-0"
163 ></motion.div>
164 )}
165 </div>
166 </div>
167 </motion.div>
168 </div>
169 );
170};
171
172export function GridPattern() {
173 const columns = 41;
174 const rows = 11;
175 return (
176 <div className="flex shrink-0 scale-105 flex-wrap items-center justify-center gap-x-px gap-y-px bg-gray-100 dark:bg-neutral-900">
177 {Array.from({ length: rows }).map((_, row) =>
178 Array.from({ length: columns }).map((_, col) => {
179 const index = row * columns + col;
180 return (
181 <div
182 key={`${col}-${row}`}
183 className={`flex h-10 w-10 shrink-0 rounded-[2px] ${
184 index % 2 === 0
185 ? "bg-gray-50 dark:bg-neutral-950"
186 : "bg-gray-50 shadow-[0px_0px_1px_3px_rgba(255,255,255,1)_inset] dark:bg-neutral-950 dark:shadow-[0px_0px_1px_3px_rgba(0,0,0,1)_inset]"
187 }`}
188 />
189 );
190 }),
191 )}
192 </div>
193 );
194}
195