react-native-web-internals
Version:
React Native for Web
112 lines (111 loc) • 5.36 kB
JavaScript
import canUseDOM from "../canUseDOM.native.js";
import createEventHandle from "../createEventHandle/index.native.js";
var supportsPointerEvent = function () {
return typeof window < "u" && window.PointerEvent != null;
},
activeModality = "keyboard",
modality = "keyboard",
previousModality,
previousActiveModality,
isEmulatingMouseEvents = !1,
listeners = /* @__PURE__ */new Set(),
KEYBOARD = "keyboard",
MOUSE = "mouse",
TOUCH = "touch",
BLUR = "blur",
CONTEXTMENU = "contextmenu",
FOCUS = "focus",
KEYDOWN = "keydown",
MOUSEDOWN = "mousedown",
MOUSEMOVE = "mousemove",
MOUSEUP = "mouseup",
POINTERDOWN = "pointerdown",
POINTERMOVE = "pointermove",
SCROLL = "scroll",
SELECTIONCHANGE = "selectionchange",
TOUCHCANCEL = "touchcancel",
TOUCHMOVE = "touchmove",
TOUCHSTART = "touchstart",
VISIBILITYCHANGE = "visibilitychange",
bubbleOptions = {
passive: !0
},
captureOptions = {
capture: !0,
passive: !0
},
addBlurListener = createEventHandle(BLUR, bubbleOptions),
addFocusListener = createEventHandle(FOCUS, bubbleOptions),
addVisibilityChangeListener = createEventHandle(VISIBILITYCHANGE, captureOptions),
addKeyDownListener = createEventHandle(KEYDOWN, captureOptions),
addPointerDownListener = createEventHandle(POINTERDOWN, captureOptions),
addPointerMoveListener = createEventHandle(POINTERMOVE, captureOptions),
addContextMenuListener = createEventHandle(CONTEXTMENU, captureOptions),
addMouseDownListener = createEventHandle(MOUSEDOWN, captureOptions),
addMouseMoveListener = createEventHandle(MOUSEMOVE, captureOptions),
addMouseUpListener = createEventHandle(MOUSEUP, captureOptions),
addScrollListener = createEventHandle(SCROLL, captureOptions),
addSelectiomChangeListener = createEventHandle(SELECTIONCHANGE, captureOptions),
addTouchCancelListener = createEventHandle(TOUCHCANCEL, captureOptions),
addTouchMoveListener = createEventHandle(TOUCHMOVE, captureOptions),
addTouchStartListener = createEventHandle(TOUCHSTART, captureOptions);
function restoreModality() {
(previousModality != null || previousActiveModality != null) && (previousModality != null && (modality = previousModality, previousModality = null), previousActiveModality != null && (activeModality = previousActiveModality, previousActiveModality = null), callListeners());
}
function onBlurWindow() {
previousModality = modality, previousActiveModality = activeModality, activeModality = KEYBOARD, modality = KEYBOARD, callListeners(), isEmulatingMouseEvents = !1;
}
function onFocusWindow() {
restoreModality();
}
function onKeyDown(event) {
event.metaKey || event.altKey || event.ctrlKey || modality !== KEYBOARD && (modality = KEYBOARD, activeModality = KEYBOARD, callListeners());
}
function onVisibilityChange() {
document.visibilityState !== "hidden" && restoreModality();
}
function onPointerish(event) {
var eventType = event.type;
if (supportsPointerEvent()) {
if (eventType === POINTERDOWN) {
activeModality !== event.pointerType && (modality = event.pointerType, activeModality = event.pointerType, callListeners());
return;
}
if (eventType === POINTERMOVE) {
modality !== event.pointerType && (modality = event.pointerType, callListeners());
return;
}
} else {
if (isEmulatingMouseEvents || (eventType === MOUSEDOWN && activeModality !== MOUSE && (modality = MOUSE, activeModality = MOUSE, callListeners()), eventType === MOUSEMOVE && modality !== MOUSE && (modality = MOUSE, callListeners())), eventType === TOUCHSTART) {
isEmulatingMouseEvents = !0, event.touches && event.touches.length > 1 && (isEmulatingMouseEvents = !1), activeModality !== TOUCH && (modality = TOUCH, activeModality = TOUCH, callListeners());
return;
}
(eventType === CONTEXTMENU || eventType === MOUSEUP || eventType === SELECTIONCHANGE || eventType === SCROLL || eventType === TOUCHCANCEL || eventType === TOUCHMOVE) && (isEmulatingMouseEvents = !1);
}
}
canUseDOM && (addBlurListener(window, onBlurWindow), addFocusListener(window, onFocusWindow), addKeyDownListener(document, onKeyDown), addPointerDownListener(document, onPointerish), addPointerMoveListener(document, onPointerish), addVisibilityChangeListener(document, onVisibilityChange), addContextMenuListener(document, onPointerish), addMouseDownListener(document, onPointerish), addMouseMoveListener(document, onPointerish), addMouseUpListener(document, onPointerish), addTouchCancelListener(document, onPointerish), addTouchMoveListener(document, onPointerish), addTouchStartListener(document, onPointerish), addSelectiomChangeListener(document, onPointerish), addScrollListener(document, onPointerish));
function callListeners() {
var value = {
activeModality,
modality
};
listeners.forEach(function (listener) {
listener(value);
});
}
function getActiveModality() {
return activeModality;
}
function getModality() {
return modality;
}
function addModalityListener(listener) {
return listeners.add(listener), function () {
listeners.delete(listener);
};
}
function testOnly_resetActiveModality() {
isEmulatingMouseEvents = !1, activeModality = KEYBOARD, modality = KEYBOARD;
}
export { addModalityListener, getActiveModality, getModality, testOnly_resetActiveModality };
//# sourceMappingURL=index.native.js.map