Skip to content

Commit

Permalink
refactor: ensure Safari binds events to iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Mar 13, 2024
1 parent a29a760 commit ce25b30
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/view/event-bindings/bind-events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IframeHTMLAttributes } from 'react';
import { querySelectorAll } from '../../query-selector-all';
import type {
AnyEventBinding,
Expand All @@ -18,6 +17,26 @@ function getOptions(
};
}

let loaded = false;

function bindEvent(win: Window, binding: EventBinding, options: EventOptions) {
let timer: number | undefined;

if (!loaded) {
// Some browsers require us to defer binding events, i.e. Safari
timer = setInterval(() => {
if ((win as Window).document.readyState === 'complete') {
win.addEventListener(binding.eventName, binding.fn, options);
loaded = true;
}
}, 100);
} else {
win.addEventListener(binding.eventName, binding.fn, options);
}

return timer;
}

export default function bindEvents(
el: HTMLElement | Window,
bindings: AnyEventBinding[],
Expand All @@ -37,9 +56,10 @@ export default function bindEvents(

const options = getOptions(sharedOptions, binding.options);

win.addEventListener(binding.eventName, binding.fn, options);
const timer = bindEvent(win as Window, binding, options);

return function unbind() {
clearInterval(timer);
win.removeEventListener(binding.eventName, binding.fn, options);
};
});
Expand Down

0 comments on commit ce25b30

Please sign in to comment.