utils.mjs 1.3 KB

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