You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 lines
9.3 KiB

{"ast":null,"code":"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])`;\nconst isVisible = element => {\n if (process.env.NODE_ENV === \"test\") return true;\n const computed = getComputedStyle(element);\n return computed.position === \"fixed\" ? false : element.offsetParent !== null;\n};\nconst obtainAllFocusableElements = element => {\n return Array.from(element.querySelectorAll(FOCUSABLE_ELEMENT_SELECTORS)).filter(item => isFocusable(item) && isVisible(item));\n};\nconst isFocusable = element => {\n if (element.tabIndex > 0 || element.tabIndex === 0 && element.getAttribute(\"tabIndex\") !== null) {\n return true;\n }\n if (element.tabIndex < 0 || element.hasAttribute(\"disabled\") || element.getAttribute(\"aria-disabled\") === \"true\") {\n return false;\n }\n switch (element.nodeName) {\n case \"A\":\n {\n return !!element.href && element.rel !== \"ignore\";\n }\n case \"INPUT\":\n {\n return !(element.type === \"hidden\" || element.type === \"file\");\n }\n case \"BUTTON\":\n case \"SELECT\":\n case \"TEXTAREA\":\n {\n return true;\n }\n default:\n {\n return false;\n }\n }\n};\nconst attemptFocus = element => {\n var _a;\n if (!isFocusable(element)) {\n return false;\n }\n (_a = element.focus) == null ? void 0 : _a.call(element);\n return document.activeElement === element;\n};\nconst triggerEvent = function (elm, name, ...opts) {\n let eventName;\n if (name.includes(\"mouse\") || name.includes(\"click\")) {\n eventName = \"MouseEvents\";\n } else if (name.includes(\"key\")) {\n eventName = \"KeyboardEvent\";\n } else {\n eventName = \"HTMLEvents\";\n }\n const evt = document.createEvent(eventName);\n evt.initEvent(name, ...opts);\n elm.dispatchEvent(evt);\n return elm;\n};\nconst isLeaf = el => !el.getAttribute(\"aria-owns\");\nconst getSibling = (el, distance, elClass) => {\n const {\n parentNode\n } = el;\n if (!parentNode) return null;\n const siblings = parentNode.querySelectorAll(elClass);\n const index = Array.prototype.indexOf.call(siblings, el);\n return siblings[index + distance] || null;\n};\nconst focusNode = el => {\n if (!el) return;\n el.focus();\n !isLeaf(el) && el.click();\n};\nexport { attemptFocus, focusNode, getSibling, isFocusable, isLeaf, isVisible, obtainAllFocusableElements, triggerEvent };","map":{"version":3,"names":["FOCUSABLE_ELEMENT_SELECTORS","isVisible","element","process","env","NODE_ENV","computed","getComputedStyle","position","offsetParent","obtainAllFocusableElements","Array","from","querySelectorAll","filter","item","isFocusable","tabIndex","getAttribute","hasAttribute","nodeName","href","rel","type","attemptFocus","_a","focus","call","document","activeElement","triggerEvent","elm","name","opts","eventName","includes","evt","createEvent","initEvent","dispatchEvent","isLeaf","el","getSibling","distance","elClass","parentNode","siblings","index","prototype","indexOf","focusNode","click"],"sources":["../../../../../packages/utils/dom/aria.ts"],"sourcesContent":["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])`\n\n/**\n * Determine if the testing element is visible on screen no matter if its on the viewport or not\n */\nexport const isVisible = (element: HTMLElement) => {\n if (process.env.NODE_ENV === 'test') return true\n const computed = getComputedStyle(element)\n // element.offsetParent won't work on fix positioned\n // WARNING: potential issue here, going to need some expert advices on this issue\n return computed.position === 'fixed' ? false : element.offsetParent !== null\n}\n\nexport const obtainAllFocusableElements = (\n element: HTMLElement\n): HTMLElement[] => {\n return Array.from(\n element.querySelectorAll<HTMLElement>(FOCUSABLE_ELEMENT_SELECTORS)\n ).filter((item: HTMLElement) => isFocusable(item) && isVisible(item))\n}\n\n/**\n * @desc Determine if target element is focusable\n * @param element {HTMLElement}\n * @returns {Boolean} true if it is focusable\n */\nexport const isFocusable = (element: HTMLElement): boolean => {\n if (\n element.tabIndex > 0 ||\n (element.tabIndex === 0 && element.getAttribute('tabIndex') !== null)\n ) {\n return true\n }\n if (\n element.tabIndex < 0 ||\n element.hasAttribute('disabled') ||\n element.getAttribute('aria-disabled') === 'true'\n ) {\n return false\n }\n\n switch (element.nodeName) {\n case 'A': {\n // casting current element to Specific HTMLElement in order to be more type precise\n return (\n !!(element as HTMLAnchorElement).href &&\n (element as HTMLAnchorElement).rel !== 'ignore'\n )\n }\n case 'INPUT': {\n return !(\n (element as HTMLInputElement).type === 'hidden' ||\n (element as HTMLInputElement).type === 'file'\n )\n }\n case 'BUTTON':\n case 'SELECT':\n case 'TEXTAREA': {\n return true\n }\n default: {\n return false\n }\n }\n}\n\n/**\n * @desc Set Attempt to set focus on the current node.\n * @param element\n * The node to attempt to focus on.\n * @returns\n * true if element is focused.\n */\nexport const attemptFocus = (element: HTMLElement): boolean => {\n if (!isFocusable(element)) {\n return false\n }\n // Remove the old try catch block since there will be no error to be thrown\n element.focus?.()\n return document.activeElement === element\n}\n\n/**\n * Trigger an event\n * mouseenter, mouseleave, mouseover, keyup, change, click, etc.\n * @param {HTMLElement} elm\n * @param {String} name\n * @param {*} opts\n */\nexport const triggerEvent = function (\n elm: HTMLElement,\n name: string,\n ...opts: Array<boolean>\n): HTMLElement {\n let eventName: string\n\n if (name.includes('mouse') || name.includes('click')) {\n eventName = 'MouseEvents'\n } else if (name.includes('key')) {\n eventName = 'KeyboardEvent'\n } else {\n eventName = 'HTMLEvents'\n }\n const evt = document.createEvent(eventName)\n\n evt.initEvent(name, ...opts)\n elm.dispatchEvent(evt)\n return elm\n}\n\nexport const isLeaf = (el: HTMLElement) => !el.getAttribute('aria-owns')\n\nexport const getSibling = (\n el: HTMLElement,\n distance: number,\n elClass: string\n) => {\n const { parentNode } = el\n if (!parentNode) return null\n const siblings = parentNode.querySelectorAll(elClass)\n const index = Array.prototype.indexOf.call(siblings, el)\n return siblings[index + distance] || null\n}\n\nexport const focusNode = (el: HTMLElement) => {\n if (!el) return\n el.focus()\n !isLeaf(el) && el.click()\n}\n"],"mappings":"AAAA,MAAMA,2BAA2B,GAAG,4KAA4K;AACpM,MAACC,SA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