index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Object.defineProperties(exports, {
  2. __esModule: { value: true },
  3. [Symbol.toStringTag]: { value: "Module" }
  4. });
  5. const require_aria = require("../../utils/dom/aria.js");
  6. const require_aria$1 = require("../../constants/aria.js");
  7. const require_event = require("../../utils/dom/event.js");
  8. let vue = require("vue");
  9. //#region ../../packages/directives/trap-focus/index.ts
  10. const FOCUSABLE_CHILDREN = "_trap-focus-children";
  11. const TRAP_FOCUS_HANDLER = "_trap-focus-handler";
  12. const FOCUS_STACK = [];
  13. const FOCUS_HANDLER = (e) => {
  14. if (FOCUS_STACK.length === 0) return;
  15. const code = require_event.getEventCode(e);
  16. const focusableElement = FOCUS_STACK[FOCUS_STACK.length - 1][FOCUSABLE_CHILDREN];
  17. if (focusableElement.length > 0 && code === require_aria$1.EVENT_CODE.tab) {
  18. if (focusableElement.length === 1) {
  19. e.preventDefault();
  20. if (document.activeElement !== focusableElement[0]) focusableElement[0].focus();
  21. return;
  22. }
  23. const goingBackward = e.shiftKey;
  24. const isFirst = e.target === focusableElement[0];
  25. const isLast = e.target === focusableElement[focusableElement.length - 1];
  26. if (isFirst && goingBackward) {
  27. e.preventDefault();
  28. focusableElement[focusableElement.length - 1].focus();
  29. }
  30. if (isLast && !goingBackward) {
  31. e.preventDefault();
  32. focusableElement[0].focus();
  33. }
  34. if (process.env.NODE_ENV === "test") {
  35. const index = focusableElement.indexOf(e.target);
  36. if (index !== -1) focusableElement[goingBackward ? index - 1 : index + 1]?.focus();
  37. }
  38. }
  39. };
  40. const TrapFocus = {
  41. beforeMount(el) {
  42. el[FOCUSABLE_CHILDREN] = require_aria.obtainAllFocusableElements(el);
  43. FOCUS_STACK.push(el);
  44. if (FOCUS_STACK.length <= 1) document.addEventListener("keydown", FOCUS_HANDLER);
  45. },
  46. updated(el) {
  47. (0, vue.nextTick)(() => {
  48. el[FOCUSABLE_CHILDREN] = require_aria.obtainAllFocusableElements(el);
  49. });
  50. },
  51. unmounted() {
  52. FOCUS_STACK.shift();
  53. if (FOCUS_STACK.length === 0) document.removeEventListener("keydown", FOCUS_HANDLER);
  54. }
  55. };
  56. //#endregion
  57. exports.FOCUSABLE_CHILDREN = FOCUSABLE_CHILDREN;
  58. exports.TRAP_FOCUS_HANDLER = TRAP_FOCUS_HANDLER;
  59. exports.default = TrapFocus;
  60. //# sourceMappingURL=index.js.map