@daysnap/utils
Version:
33 lines (31 loc) • 813 B
JavaScript
// src/chooseMedia.ts
function chooseMedia(options = {}) {
return new Promise((resolve, reject) => {
const className = "__chooseMedia_input";
let input = document.querySelector(`.${className}`);
if (!input) {
input = document.createElement("input");
input.type = "file";
input.style.display = "none";
input.className = className;
document.body.appendChild(input);
}
Object.assign(input, options);
input.onchange = function(event) {
const target = event.target;
const files = Array.from(target.files ?? []);
target.value = "";
if (input) {
document.body.removeChild(input);
}
if (!files?.length) {
return reject();
}
resolve(files);
};
input.click();
});
}
export {
chooseMedia
};