Skip to content

Commit d1d2329

Browse files
authored
fix: allow dropping image into image tab (#2965)
1 parent 8f0870e commit d1d2329

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

apps/web/client/src/app/project/[id]/_components/left-panel/image-tab/hooks/use-image-drag-drop.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EditorMode, type ImageContentData } from '@onlook/models';
33
import { usePostHog } from 'posthog-js/react';
44
import { useCallback, useState } from 'react';
55

6-
export const useImageDragDrop = () => {
6+
export const useImageDragDrop = (onUpload?: (files: FileList) => Promise<void>) => {
77
const editorEngine = useEditorEngine();
88
const posthog = usePostHog();
99
const [isDragging, setIsDragging] = useState(false);
@@ -87,11 +87,23 @@ export const useImageDragDrop = () => {
8787
editorEngine.state.editorMode = EditorMode.DESIGN;
8888
}, []);
8989

90+
const handleDrop = useCallback((e: React.DragEvent<HTMLDivElement>) => {
91+
e.preventDefault();
92+
setIsDragging(false);
93+
e.currentTarget.removeAttribute('data-dragging-image');
94+
95+
const files = e.dataTransfer.files;
96+
if (files.length > 0 && onUpload) {
97+
void onUpload(files);
98+
}
99+
}, [onUpload]);
100+
90101
return {
91102
isDragging,
92103
handleDragOver,
93104
handleDragEnter,
94105
handleDragLeave,
106+
handleDrop,
95107
onImageDragStart,
96108
onImageMouseDown,
97109
onImageMouseUp,

apps/web/client/src/app/project/[id]/_components/left-panel/image-tab/image-grid.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ interface ImageGridProps {
1111
projectId: string;
1212
branchId: string;
1313
search: string;
14+
onUpload: (files: FileList) => Promise<void>;
1415
}
1516

16-
export const ImageGrid = ({ images, projectId, branchId, search, }: ImageGridProps) => {
17+
export const ImageGrid = ({ images, projectId, branchId, search, onUpload }: ImageGridProps) => {
1718
const {
18-
handleDragEnter, handleDragLeave, handleDragOver, isDragging,
19+
handleDragEnter, handleDragLeave, handleDragOver, handleDrop, isDragging,
1920
onImageDragStart, onImageDragEnd, onImageMouseDown, onImageMouseUp
20-
} = useImageDragDrop();
21+
} = useImageDragDrop(onUpload);
2122

2223
return (
2324
<div className={cn(
@@ -27,6 +28,7 @@ export const ImageGrid = ({ images, projectId, branchId, search, }: ImageGridPro
2728
onDragEnter={handleDragEnter}
2829
onDragLeave={handleDragLeave}
2930
onDragOver={handleDragOver}
31+
onDrop={handleDrop}
3032
>
3133
<div className="grid grid-cols-2 gap-2">
3234
{images.map((image) => (

apps/web/client/src/app/project/[id]/_components/left-panel/image-tab/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const ImagesTab = observer(() => {
8383
projectId={projectId}
8484
branchId={branchId}
8585
search={search}
86+
onUpload={handleUpload}
8687
/>
8788
</div>
8889
);

0 commit comments

Comments
 (0)