Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: drag and drop #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
},
"dependencies": {
"babel-plugin-react-compiler": "0.0.0-experimental-734b737-20241003",
"lucide-react": "^0.456.0",
"next": "15.0.0-rc.1",
"next-plausible": "^3.12.2",
"react": "19.0.0-rc-cd22717c-20241013",
"react-dom": "19.0.0-rc-cd22717c-20241013"
"react-dom": "19.0.0-rc-cd22717c-20241013",
"react-dropzone": "^14.3.5"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
48 changes: 45 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions src/app/(tools)/components/dropzone-upload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use client";

import { useDropzone } from "react-dropzone";
import { Upload } from "lucide-react";

interface DropzoneUploadProps {
accept: Record<string, string[]>;
onFilesAdded: (event: React.ChangeEvent<HTMLInputElement>) => void;
}

export default function DropzoneUpload({
accept,
onFilesAdded,
}: DropzoneUploadProps) {
const onDrop = (acceptedFiles: File[]) => {
if (acceptedFiles.length > 0) {
onFilesAdded({
target: {
files: acceptedFiles,
} as unknown as EventTarget,
} as unknown as React.ChangeEvent<HTMLInputElement>);
}
};
const { getRootProps, getInputProps, isDragActive } = useDropzone({
accept,
onDrop,
});

return (
<div className="w-full max-w-md mx-auto">
<div
{...getRootProps()}
className={`relative border-2 border-dashed rounded-lg p-6 cursor-pointer ${
isDragActive
? "border-green-500 text-green-500"
: "border-muted-foreground"
}`}
>
<input {...getInputProps()} onChange={onFilesAdded} />
<div className="text-center">
<Upload className="mx-auto size-8 text-muted-foreground" />
<p className="mt-6 text-sm text-muted-foreground">
Drag &apos;n&apos; drop files here, or click to select files
</p>
<p className="mt-1 text-xs text-muted-foreground">
{Object.keys(accept).length > 0
? `Allowed files: ${Object.keys(accept).join(", ")}`
: "All file types are allowed"}
</p>
</div>
</div>
</div>
);
}
16 changes: 7 additions & 9 deletions src/app/(tools)/rounded-border/rounded-tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { usePlausible } from "next-plausible";
import { useMemo, useState } from "react";
import { ChangeEvent } from "react";
import React from "react";
import DropzoneUpload from "../components/dropzone-upload";

type Radius = 2 | 4 | 8 | 16 | 32 | 64;

Expand Down Expand Up @@ -196,15 +197,12 @@ export function RoundedTool() {
<div className="flex flex-col p-4 gap-4">
<p className="text-center">Round the corners of any image</p>
<div className="flex justify-center">
<label className="cursor-pointer inline-flex items-center px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-75 transition-colors duration-200 gap-2">
<span>Upload Image</span>
<input
type="file"
onChange={handleFileUpload}
accept="image/*"
className="hidden"
/>
</label>
<DropzoneUpload
accept={{
"image/*": [],
}}
onFilesAdded={handleFileUpload}
/>
</div>
</div>
);
Expand Down
16 changes: 7 additions & 9 deletions src/app/(tools)/square-image/square-tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, { useState, useEffect, ChangeEvent } from "react";
import { usePlausible } from "next-plausible";
import DropzoneUpload from "../components/dropzone-upload";

export const SquareTool: React.FC = () => {
const [imageFile, setImageFile] = useState<File | null>(null);
Expand Down Expand Up @@ -115,15 +116,12 @@ export const SquareTool: React.FC = () => {
Create square images with custom backgrounds. Fast and free.
</p>
<div className="flex justify-center">
<label className="cursor-pointer inline-flex items-center px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-75 transition-colors duration-200 gap-2">
<span>Upload Image</span>
<input
type="file"
onChange={handleImageUpload}
accept="image/*"
className="hidden"
/>
</label>
<DropzoneUpload
accept={{
"image/*": [],
}}
onFilesAdded={handleImageUpload}
/>
</div>
</div>
);
Expand Down
22 changes: 11 additions & 11 deletions src/app/(tools)/svg-to-png/svg-tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function useSvgConverter(props: {
const svgFileName = props.imageMetadata.name ?? "svg_converted";

// Remove the .svg extension
link.download = `${svgFileName.replace(".svg", "")}-${props.scale}x.png`;
link.download = `${svgFileName.replace(".svg", "")}-${
props.scale
}x.png`;
link.click();
}
};
Expand All @@ -64,7 +66,9 @@ function useSvgConverter(props: {
saveImage();
};

img.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(scaledSvg)}`;
img.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(
scaledSvg
)}`;
};

return {
Expand Down Expand Up @@ -112,6 +116,7 @@ export const useFileUploader = () => {
};

import React from "react";
import DropzoneUpload from "../components/dropzone-upload";

interface SVGRendererProps {
svgContent: string;
Expand Down Expand Up @@ -184,15 +189,10 @@ export function SVGTool() {
Make SVGs into PNGs. Also makes them bigger. (100% free btw.)
</p>
<div className="flex justify-center">
<label className="cursor-pointer inline-flex items-center px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-75 transition-colors duration-200 gap-2">
<span>Upload SVG</span>
<input
type="file"
onChange={handleFileUpload}
accept=".svg"
className="hidden"
/>
</label>
<DropzoneUpload
accept={{ "image/svg+xml": [] }}
onFilesAdded={handleFileUpload}
/>
</div>
</div>
);
Expand Down