aria.mjs 2.6 KB

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