aria.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 isHTMLElement = (e) => {
  5. if (typeof Element === "undefined")
  6. return false;
  7. return e instanceof Element;
  8. };
  9. const isVisible = (element) => {
  10. const computed = getComputedStyle(element);
  11. return computed.position === "fixed" ? false : element.offsetParent !== null;
  12. };
  13. const obtainAllFocusableElements = (element) => {
  14. return Array.from(element.querySelectorAll(FOCUSABLE_ELEMENT_SELECTORS)).filter((item) => isFocusable(item) && isVisible(item));
  15. };
  16. const isFocusable = (element) => {
  17. if (element.tabIndex > 0 || element.tabIndex === 0 && element.getAttribute("tabIndex") !== null) {
  18. return true;
  19. }
  20. if (element.tabIndex < 0 || element.hasAttribute("disabled") || element.getAttribute("aria-disabled") === "true") {
  21. return false;
  22. }
  23. switch (element.nodeName) {
  24. case "A": {
  25. return !!element.href && element.rel !== "ignore";
  26. }
  27. case "INPUT": {
  28. return !(element.type === "hidden" || element.type === "file");
  29. }
  30. case "BUTTON":
  31. case "SELECT":
  32. case "TEXTAREA": {
  33. return true;
  34. }
  35. default: {
  36. return false;
  37. }
  38. }
  39. };
  40. const triggerEvent = function(elm, name, ...opts) {
  41. let eventName;
  42. if (name.includes("mouse") || name.includes("click")) {
  43. eventName = "MouseEvents";
  44. } else if (name.includes("key")) {
  45. eventName = "KeyboardEvent";
  46. } else {
  47. eventName = "HTMLEvents";
  48. }
  49. const evt = document.createEvent(eventName);
  50. evt.initEvent(name, ...opts);
  51. elm.dispatchEvent(evt);
  52. return elm;
  53. };
  54. const isLeaf = (el) => !el.getAttribute("aria-owns");
  55. const getSibling = (el, distance, elClass) => {
  56. const { parentNode } = el;
  57. if (!parentNode)
  58. return null;
  59. const siblings = parentNode.querySelectorAll(elClass);
  60. const index = Array.prototype.indexOf.call(siblings, el);
  61. return siblings[index + distance] || null;
  62. };
  63. const focusElement = (el, options) => {
  64. if (!el || !el.focus)
  65. return;
  66. let cleanup = false;
  67. if (isHTMLElement(el) && !isFocusable(el) && !el.getAttribute("tabindex")) {
  68. el.setAttribute("tabindex", "-1");
  69. cleanup = true;
  70. }
  71. el.focus(options);
  72. if (isHTMLElement(el) && cleanup) {
  73. el.removeAttribute("tabindex");
  74. }
  75. };
  76. const focusNode = (el) => {
  77. if (!el)
  78. return;
  79. focusElement(el);
  80. !isLeaf(el) && el.click();
  81. };
  82. exports.focusElement = focusElement;
  83. exports.focusNode = focusNode;
  84. exports.getSibling = getSibling;
  85. exports.isFocusable = isFocusable;
  86. exports.isLeaf = isLeaf;
  87. exports.isVisible = isVisible;
  88. exports.obtainAllFocusableElements = obtainAllFocusableElements;
  89. exports.triggerEvent = triggerEvent;
  90. //# sourceMappingURL=aria.js.map