JSFiddle - React, Tailwind, and code Playground
by sstur
HTML
<button>One</button>
<button class="file-select">
Two
<span>
<input tabindex="-1" type="file" multiple />
</span>
</button>
<button>Three</button>
CSS
body {
font-family: Helvetica, Arial, Verdana, Tahoma, 'Lucida Sans', sans-serif;
font-size: 11px;
}
button {
display: inline-block;
position: relative;
margin: 0;
padding: 7px 20px;
background: none;
border: 1px solid #ccc;
border-radius: 3px;
cursor: pointer;
font-size: inherit;
font-family: inherit;
white-space: nowrap;
text-align: center;
}
button:hover {
background: #f2f2f2;
}
button:focus {
outline: 1px solid #900;
outline: 5px auto -webkit-focus-ring-color;
outline-offset: -2px;
-webkit-user-select: none;
}
button.file-select span {
display: block;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
cursor: pointer;
}
button input {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
border: 0;
margin: 0;
padding: 0;
opacity: 0;
font-size: 300px;
cursor: pointer;
}
JavaScript
var button = document.querySelector('button.file-select');
button.addEventListener('click', function(event) {
var fileInput = this.querySelector('input');
if (event.target === fileInput) {
return;
}
fileInput.click();
});
var fileInput = document.querySelector('button.file-select input');
fileInput.addEventListener('change', function(event) {
var files = Array.prototype.slice.call(this.files);
console.log(Array.isArray(files), files);
});