JSFiddle - React, Tailwind, and code Playground

by Dewaun Ayers

HTML

<div id="app">
  <div id="1" class="drop-area">
    <span class="drop-area__prompt">Click here</span>
    <input type="file" accept="image/*" class="drop-area__input" name="upload"> </div>
  <div id="2" class="drop-area">
    <span class="drop-area__prompt">Click here</span>
    <input type="file" accept="image/*" class="drop-area__input" name="upload"> </div>
</div>

CSS

#app {
  display: grid;
  grid-template-columns: repeat(auto-fill, 200px);
  padding: 24px;
  gap: 24px;
}

.drop-area {
  width: 200px;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-sizing: border-box;
  padding: 8px;
  border: 2px dashed #cccccc;
  border-radius: 10px;
}

.drop-area__prompt {
  pointer-events: none;
}

.drop-area__input {
  display: none;
}

JavaScript

function clickEvent(event) {
  this.childNodes.forEach(function (node) {
    if (node && node === event.target && node.nodeType === 1) {
      node.children[1].click();
    }
  });
}

function changeEvent(event) {
  this.childNodes.forEach(function (node) {
    if (node && event.target.files.length > 0) {
    	console.log(event.dataTransfer)
      console.log(event.target.files[0]);
    }
  });
}

function eventHandler(element, eventType, callback) {
  document.getElementById(element).addEventListener(eventType, callback, false);
}

eventHandler("app", "click", clickEvent);
eventHandler("app", "change", changeEvent);