Skip to content

Commit

Permalink
Fix #107 (#115)
Browse files Browse the repository at this point in the history
input file is required in the DOM for working in Safari
  • Loading branch information
Barba828 authored Jun 20, 2022
1 parent 7a6ac9d commit 7148489
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/legacy/file-open.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default async (options = [{}]) => {
input.multiple = options[0].multiple || false;
// Empty string allows everything.
input.accept = accept || '';
input.style.display = 'none';
document.body.appendChild(input);
const _reject = () => cleanupListenersAndMaybeReject(reject);
const _resolve = (value) => {
if (typeof cleanupListenersAndMaybeReject === 'function') {
Expand All @@ -45,7 +47,18 @@ export default async (options = [{}]) => {
const cleanupListenersAndMaybeReject =
options[0].legacySetup &&
options[0].legacySetup(_resolve, _reject, input);

const cancelDetector = () => {
window.removeEventListener('focus', cancelDetector);
document.body.removeChild(input);
};

input.addEventListener('click', () => {
window.addEventListener('focus', cancelDetector, true);
});

input.addEventListener('change', () => {
document.body.removeChild(input);
_resolve(input.multiple ? Array.from(input.files) : input.files[0]);
});

Expand Down

0 comments on commit 7148489

Please sign in to comment.