A versatile collection of uploaders and media handling components for React applications.
- πΌοΈ Image upload with drag-and-drop support
- βοΈ Image cropping with customizable aspect ratio
- π Built-in validation options
- π¨ Fully customizable dimensions
- π Dark mode compatible
- π€ Upload function integration
npm install @dracu/uploadersor
yarn add @dracu/uploadersThis package requires the following dependencies:
"peerDependencies": {
"react": ">=17.0.0",
"react-dom": ">=17.0.0",
"react-easy-crop": "^5.0.0",
"lucide-react": "^0.263.0"
}If you don't have these dependencies installed, you'll need to install them:
npm install react-easy-crop lucide-reactimport { ImageCrop } from "@dracu/uploaders";
function App() {
const handleCropComplete = (croppedImageDataURL) => {
console.log("Cropped image:", croppedImageDataURL);
// Do something with the cropped image
};
const handleUploadResponse = (response) => {
console.log("Upload response:", response);
// Handle the upload response
};
const uploadFunction = async (file) => {
// Example upload function
const formData = new FormData();
formData.append("image", file);
const response = await fetch("https://your-upload-endpoint.com/upload", {
method: "POST",
body: formData,
});
return response.json();
};
return (
<div className="container mx-auto p-4">
<h1 className="text-2xl font-bold mb-4">Image Upload Example</h1>
<ImageCrop
uploadFunction={uploadFunction}
onCropComplete={handleCropComplete}
onUploadResponse={handleUploadResponse}
uploadLimit={5}
aspect={16 / 9}
/>
</div>
);
}
export default App;The main component for image uploading and cropping.
| Prop | Type | Default | Description |
|---|---|---|---|
uploadFunction |
(file: File) => Promise<unknown> |
undefined |
Function to handle the image upload |
uploadLimit |
number |
4 |
Maximum file size in MB |
aspect |
number |
1 |
Aspect ratio for the cropper (width/height) |
cropperWidth |
string |
"50vw" |
Width of the cropper container |
cropperHeight |
string |
"50vw" |
Height of the cropper container |
uploaderWidth |
string |
"50vw" |
Width of the uploader container |
uploaderHeight |
string |
"50vw" |
Height of the uploader container |
onUploadResponse |
(response: unknown) => void |
undefined |
Callback when upload completes |
onCropComplete |
(croppedImageDataURL: string | null) => void |
undefined |
Callback when crop is applied |
validationOptions |
ValidationOptions |
undefined |
Options for image validation |
onError |
(message: string) => void |
undefined |
Error handling callback |
disableCrop |
boolean |
false |
Disable cropping functionality |
You can provide validation options to ensure uploaded images meet specific requirements:
interface ValidationOptions {
minWidth?: number;
minHeight?: number;
maxWidth?: number;
maxHeight?: number;
acceptedFormats?: string[];
}The component uses Tailwind CSS classes for styling. If you're using Tailwind CSS in your project, the component will use your custom theme. If not, you'll need to include the base Tailwind CSS styles.
The component uses Lucide React for icons. Make sure you have it installed:
npm install lucide-react<ImageCrop
validationOptions={{
minWidth: 800,
minHeight: 600,
acceptedFormats: ["image/jpeg", "image/png"],
}}
onError={(errorMessage) => {
toast.error(errorMessage); // Using your preferred toast library
}}
// other props...
/><ImageCrop
disableCrop={true}
// Use it as a simple image uploader
// other props...
/><ImageCrop
cropperWidth="400px"
cropperHeight="300px"
uploaderWidth="400px"
uploaderHeight="300px"
// other props...
/><ImageCrop
aspect={4 / 3} // 4:3 aspect ratio
// other props...
/>The uploadFunction prop should be a function that takes a File object and returns a Promise. This function is responsible for sending the file to your server or storage service.
const uploadToServer = async (file) => {
const formData = new FormData();
formData.append("file", file);
const response = await fetch("https://your-api.com/upload", {
method: "POST",
body: formData,
});
if (!response.ok) {
throw new Error("Upload failed");
}
return response.json();
};
<ImageCrop uploadFunction={uploadToServer} />;- More upload components for different file types
- Video uploading and trimming
- File progress uploaders
- Multiple file upload support
- And more!
Contributions are welcome! Please feel free to submit a Pull Request.
MIT