Skip to content

Commit cd26261

Browse files
committed
edge cases for uploading and dropping images
1 parent 4cf6956 commit cd26261

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

drag-and-drop-vanilla-js/index.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
</head>
99
<body>
1010
<div>
11-
<h1>Drag and Drop</h1>
11+
<h1 style="text-align: center;">Drag and Drop</h1>
1212
<div class="drag-upload">
1313
<div class="drag-area green" id="img-show">
14-
<input type="file" id="file-upload" accept="image/*" multiple />
14+
<label for="file-upload" id="upload-label"
15+
><span>Drag &amp; Drop Images</span>
16+
<span>Or</span>
17+
<span>Click for browse Images...🖼️</span>
18+
<input type="file" id="file-upload" accept="image/*" multiple />
19+
</label>
1520
</div>
1621
</div>
1722
<div class="container">

drag-and-drop-vanilla-js/src/main.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ const draggableArea = document.querySelectorAll(".drag-area");
22
const imageShow = document.querySelector("#img-show");
33
const dragUploadFile = document.getElementById("file-upload");
44
const drag_img = document.querySelector(".drag-img");
5+
const label_Onupload = document.getElementById("upload-label");
56

67
// drag and drap events
78
let isDragged; // store the dragged elements
89

910
dragUploadFile.addEventListener("change", (e) => {
10-
displayMultipleImages(e.target.files);
11+
if (e.target.files.length > 3) {
12+
label_Onupload.style.display = "flex";
13+
alert("upload not more than 3 images...");
14+
} else if (e.target.files.length <= 3) {
15+
displayMultipleImages(e.target.files);
16+
label_Onupload.style.display = "none";
17+
return;
18+
}
1119
});
1220

1321
//function to handle drag start
@@ -36,10 +44,16 @@ function onDragLeave(e) {
3644
function onDragDrop(e) {
3745
e.preventDefault();
3846
e.target.classList.remove("currentBox");
39-
const dropTarget = e.target.closest('.drag-area');
47+
const dropTarget = e.target.closest(".drag-area");
4048
if (!dropTarget) return;
41-
42-
if (isDragged && isDragged.tagName === 'IMG') {
49+
50+
for (let file of e.dataTransfer.files) {
51+
if (file.type === "image/*") {
52+
displayMultipleImages(file);
53+
}
54+
}
55+
56+
if (isDragged) {
4357
if (dropTarget !== isDragged.parentElement) {
4458
dropTarget.appendChild(isDragged);
4559
}
@@ -51,28 +65,43 @@ function onDragEnter(e) {
5165
}
5266

5367
function onDragEnd(e) {
68+
let checkLength = dropZonEmptyCheck(imageShow);
69+
if (checkLength <= 1) {
70+
label_Onupload.style.display = "none";
71+
} else {
72+
label_Onupload.style.display = "flex";
73+
}
74+
5475
setTimeout(() => {
5576
e.target.classList.add("hoverLeave");
5677
}, 100);
5778
}
5879

80+
function dropZonEmptyCheck(image) {
81+
const imageCount = image.querySelectorAll("img").length;
82+
if (imageCount) {
83+
return true;
84+
}
85+
}
86+
5987
// handling multiple files
6088
function displayMultipleImages(files) {
6189
for (let file of files) {
62-
if (!file && !file.type("image/*")) {
90+
if (!file && !file.type("image/*") && files.length > 3) {
6391
return false;
6492
}
6593
}
6694

95+
//
6796
for (let file of files) {
6897
let reader = new FileReader();
6998
reader.onload = () => {
7099
let image = new Image();
71100
image.src = reader.result;
72101
image.style.display = "block";
73102
image.className = "drag-img";
74-
image.setAttribute('draggable', 'true');
75-
image.addEventListener('dragstart', function(e) {
103+
image.setAttribute("draggable", "true");
104+
image.addEventListener("dragstart", function (e) {
76105
isDragged = e.target;
77106
});
78107
imageShow.appendChild(image);

drag-and-drop-vanilla-js/src/style.css

+20-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ body {
1818

1919
.drag-area {
2020
width: 200px;
21+
height: max-content;
2122
min-height: 250px;
22-
max-height: max-content;
2323
padding: 10px;
2424
border: 1px solid rgba(181, 170, 170, 0.338);
2525
border-radius: 10px;
2626
box-shadow: 5px 0px 18px rgba(64, 64, 64, 0.573);
2727
flex-wrap: wrap;
28+
display: flex;
2829
}
2930

3031
.drag-img {
@@ -34,19 +35,35 @@ body {
3435
min-width: 50px;
3536
}
3637

37-
.drag-upload{
38+
.drag-upload {
3839
display: flex;
3940
align-items: center;
4041
margin-bottom: 10px;
4142
justify-content: center;
4243
}
4344

4445
.green {
45-
background-color: rgba(16, 255, 44, 0.873);
46+
background-color: rgba(255, 255, 255, 0.911);
47+
position: relative;
48+
min-width: 250px;
49+
font-size: 18px;
4650
}
4751

4852
.green input[type="file"] {
4953
width: 100%;
54+
visibility: hidden;
55+
}
56+
57+
.green label {
58+
min-height: inherit;
59+
display: flex;
60+
position: absolute;
61+
max-width: 240px;
62+
flex-direction: column;
63+
align-items: center;
64+
justify-content: center;
65+
gap: 8px;
66+
border: dashed;
5067
}
5168

5269
.orange {

0 commit comments

Comments
 (0)