UNPKG

react-native-web-internals

Version:

React Native for Web

43 lines (42 loc) 1.58 kB
import canUseDOM from "../canUseDOM.mjs"; const emptyFunction = () => {}; function supportsPassiveEvents() { let supported = !1; if (canUseDOM) try { const options = {}; Object.defineProperty(options, "passive", { get() { return supported = !0, !1; } }), window.addEventListener("test", null, options), window.removeEventListener("test", null, options); } catch {} return supported; } const canUsePassiveEvents = supportsPassiveEvents(); function getOptions(options) { return options == null ? !1 : canUsePassiveEvents ? options : !!options.capture; } function isPropagationStopped() { return this.cancelBubble; } function isDefaultPrevented() { return this.defaultPrevented; } function normalizeEvent(event) { return event.nativeEvent = event, event.persist = emptyFunction, event.isDefaultPrevented = isDefaultPrevented, event.isPropagationStopped = isPropagationStopped, event; } function createEventHandle(type, options) { const opts = getOptions(options); return function (target, listener) { if (target == null || typeof target.addEventListener != "function") throw new Error("createEventHandle: called on an invalid target."); const element = target; if (listener != null) { const compatListener = e => listener(normalizeEvent(e)); return element.addEventListener(type, compatListener, opts), function () { element?.removeEventListener(type, compatListener, opts); }; } else return emptyFunction; }; } export { createEventHandle as default }; //# sourceMappingURL=index.mjs.map