event.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { EVENT_CODE } from "../../constants/aria.mjs";
  2. import { isAndroid } from "../browser.mjs";
  3. //#region ../../packages/utils/dom/event.ts
  4. const composeEventHandlers = (theirsHandler, oursHandler, { checkForDefaultPrevented = true } = {}) => {
  5. const handleEvent = (event) => {
  6. const shouldPrevent = theirsHandler?.(event);
  7. if (checkForDefaultPrevented === false || !shouldPrevent) return oursHandler?.(event);
  8. };
  9. return handleEvent;
  10. };
  11. const whenMouse = (handler) => {
  12. return (e) => e.pointerType === "mouse" ? handler(e) : void 0;
  13. };
  14. const getEventCode = (event) => {
  15. if (event.code && event.code !== "Unidentified") return event.code;
  16. const key = getEventKey(event);
  17. if (key) {
  18. if (Object.values(EVENT_CODE).includes(key)) return key;
  19. switch (key) {
  20. case " ": return EVENT_CODE.space;
  21. default: return "";
  22. }
  23. }
  24. return "";
  25. };
  26. const getEventKey = (event) => {
  27. let key = event.key && event.key !== "Unidentified" ? event.key : "";
  28. if (!key && event.type === "keyup" && isAndroid()) {
  29. const target = event.target;
  30. key = target.value.charAt(target.selectionStart - 1);
  31. }
  32. return key;
  33. };
  34. //#endregion
  35. export { composeEventHandlers, getEventCode, getEventKey, whenMouse };
  36. //# sourceMappingURL=event.mjs.map