1cde8410ff7e530945ccd332f455e9a2e05d6414e277e5b88728e32418c80c79b42498097a45b07b6d7e1ee412c7d4889e53965b1e511077f8dd4f0f4cdc0d 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var aria = require('../../../constants/aria.js');
  4. const MAP_KEY_TO_FOCUS_INTENT = {
  5. ArrowLeft: "prev",
  6. ArrowUp: "prev",
  7. ArrowRight: "next",
  8. ArrowDown: "next",
  9. PageUp: "first",
  10. Home: "first",
  11. PageDown: "last",
  12. End: "last"
  13. };
  14. const getDirectionAwareKey = (key, dir) => {
  15. if (dir !== "rtl")
  16. return key;
  17. switch (key) {
  18. case aria.EVENT_CODE.right:
  19. return aria.EVENT_CODE.left;
  20. case aria.EVENT_CODE.left:
  21. return aria.EVENT_CODE.right;
  22. default:
  23. return key;
  24. }
  25. };
  26. const getFocusIntent = (event, orientation, dir) => {
  27. const key = getDirectionAwareKey(event.code, dir);
  28. if (orientation === "vertical" && [aria.EVENT_CODE.left, aria.EVENT_CODE.right].includes(key))
  29. return void 0;
  30. if (orientation === "horizontal" && [aria.EVENT_CODE.up, aria.EVENT_CODE.down].includes(key))
  31. return void 0;
  32. return MAP_KEY_TO_FOCUS_INTENT[key];
  33. };
  34. const reorderArray = (array, atIdx) => {
  35. return array.map((_, idx) => array[(idx + atIdx) % array.length]);
  36. };
  37. const focusFirst = (elements) => {
  38. const { activeElement: prevActive } = document;
  39. for (const element of elements) {
  40. if (element === prevActive)
  41. return;
  42. element.focus();
  43. if (prevActive !== document.activeElement)
  44. return;
  45. }
  46. };
  47. exports.focusFirst = focusFirst;
  48. exports.getFocusIntent = getFocusIntent;
  49. exports.reorderArray = reorderArray;
  50. //# sourceMappingURL=utils.js.map