JSFiddle - React, Tailwind, and code Playground
by levchenko_d
HTML
<script src="https://beat.today/javascripts/functions.js"></script>
<div class="handle-dragover dropable">
<div class="handle-dragover-show dropable-placeholder">
<span class="text">Drop file to upload</span>
</div>
</div>
CSS
.handle-dragover{
min-height: 400px;
width: 100%;
background: silver;
}
/* Ingore Above */
/* Dropable */
.handle-dragover{
position: relative;
}
.handle-dragover:not(.dragover) .handle-dragover-show{
display: none;
}
.dropable-placeholder{
position: absolute;
top: 30px;
right: 30px;
bottom: 30px;
left: 30px;
background: #2C9EE4;
border-radius: 7px;
}
.dropable-placeholder .text {
color: #fff;
font-size: 36px;
height: 48px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
margin: auto;
font-family: 'Open Sans', sans-serif;
}
/* Dropable END*/
JavaScript
function readFile(file, callback){
function read(){
var reader = new FileReader();
reader.onloadend = function(e){
App.isFn(callback)(e.target.result);
};
reader.readAsDataURL(file);
};
if(file.type){ read(); }
return file;
};
/* Dropable */
var Dropable = function(options){
var t = this,
o = options;
t.element = o.element || App.q('dropable');
t.ondrop = o.ondrop || null;
t.files = [];
t.init();
};
Dropable.prototype.attachOndragover = function () {
var t = this;
function ondragover(e){
e.kill();
t.element.addClass('dragover');
};
t.element.addEventListener('dragover', ondragover);
};
Dropable.prototype.attachOndragend = function () {
var t = this;
function ondragend(){
t.element.removeClass('dragover');
};
t.element.addEventListener('dragend', ondragend);
};
Dropable.prototype.attachOndrop = function () {
var t = this;
function ondrop(e){
e.kill();
t.element.removeClass('dragover');
t.files = e.dataTransfer.files;
App.isFn(t.ondrop)(e);
return false;
};
t.element.addEventListener('drop', ondrop);
};
Dropable.prototype.attachEvents = function(){
var t = this;
t.attachOndragover();
t.attachOndragend();
t.attachOndrop();
};
Dropable.prototype.init = function(){
var t = this;
t.attachEvents();
t.element.Dropable = t;
};
/* Dropable END */
/* DEMO */
new Dropable({
element: App.q('.dropable'),
ondrop: function(e){
var files = e.dataTransfer.files;
for(i in files){
readFile(files[i], function(File){
console.log(File)
});
};
}
});