event.mjs 1.3 KB

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