aria.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const FOCUSABLE_ELEMENT_SELECTORS = `a[href],button:not([disabled]),button:not([hidden]),:not([tabindex="-1"]),input:not([disabled]),input:not([type="hidden"]),select:not([disabled]),textarea:not([disabled])`;
  4. const isVisible = (element) => {
  5. if (process.env.NODE_ENV === "test")
  6. return true;
  7. const computed = getComputedStyle(element);
  8. return computed.position === "fixed" ? false : element.offsetParent !== null;
  9. };
  10. const obtainAllFocusableElements = (element) => {
  11. return Array.from(element.querySelectorAll(FOCUSABLE_ELEMENT_SELECTORS)).filter((item) => isFocusable(item) && isVisible(item));
  12. };
  13. const isFocusable = (element) => {
  14. if (element.tabIndex > 0 || element.tabIndex === 0 && element.getAttribute("tabIndex") !== null) {
  15. return true;
  16. }
  17. if (element.disabled) {
  18. return false;
  19. }
  20. switch (element.nodeName) {
  21. case "A": {
  22. return !!element.href && element.rel !== "ignore";
  23. }
  24. case "INPUT": {
  25. return !(element.type === "hidden" || element.type === "file");
  26. }
  27. case "BUTTON":
  28. case "SELECT":
  29. case "TEXTAREA": {
  30. return true;
  31. }
  32. default: {
  33. return false;
  34. }
  35. }
  36. };
  37. const attemptFocus = (element) => {
  38. var _a;
  39. if (!isFocusable(element)) {
  40. return false;
  41. }
  42. (_a = element.focus) == null ? void 0 : _a.call(element);
  43. return document.activeElement === element;
  44. };
  45. const triggerEvent = function(elm, name, ...opts) {
  46. let eventName;
  47. if (name.includes("mouse") || name.includes("click")) {
  48. eventName = "MouseEvents";
  49. } else if (name.includes("key")) {
  50. eventName = "KeyboardEvent";
  51. } else {
  52. eventName = "HTMLEvents";
  53. }
  54. const evt = document.createEvent(eventName);
  55. evt.initEvent(name, ...opts);
  56. elm.dispatchEvent(evt);
  57. return elm;
  58. };
  59. const isLeaf = (el) => !el.getAttribute("aria-owns");
  60. const getSibling = (el, distance, elClass) => {
  61. const { parentNode } = el;
  62. if (!parentNode)
  63. return null;
  64. const siblings = parentNode.querySelectorAll(elClass);
  65. const index = Array.prototype.indexOf.call(siblings, el);
  66. return siblings[index + distance] || null;
  67. };
  68. const focusNode = (el) => {
  69. if (!el)
  70. return;
  71. el.focus();
  72. !isLeaf(el) && el.click();
  73. };
  74. exports.attemptFocus = attemptFocus;
  75. exports.focusNode = focusNode;
  76. exports.getSibling = getSibling;
  77. exports.isFocusable = isFocusable;
  78. exports.isLeaf = isLeaf;
  79. exports.isVisible = isVisible;
  80. exports.obtainAllFocusableElements = obtainAllFocusableElements;
  81. exports.triggerEvent = triggerEvent;
  82. //# sourceMappingURL=aria.js.map