UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 5.65 kB
{"version":3,"file":"aria.mjs","names":[],"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\nexport const isShadowRoot = (e: unknown): e is ShadowRoot => {\n if (typeof ShadowRoot === 'undefined') return false\n return e instanceof ShadowRoot\n}\n\nconst isHTMLElement = (e: unknown): e is Element => {\n if (typeof Element === 'undefined') return false\n return e instanceof Element\n}\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 * 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 focusElement = (\n el?: HTMLElement | { focus: () => void } | null,\n options?: FocusOptions\n) => {\n if (!el || !el.focus) return\n let cleanup: boolean = false\n\n if (isHTMLElement(el) && !isFocusable(el) && !el.getAttribute('tabindex')) {\n el.setAttribute('tabindex', '-1')\n cleanup = true\n }\n\n el.focus(options)\n\n if (isHTMLElement(el) && cleanup) {\n el.removeAttribute('tabindex')\n }\n}\n\nexport const focusNode = (el: HTMLElement) => {\n if (!el) return\n focusElement(el)\n !isLeaf(el) && el.click()\n}\n"],"mappings":";AAAA,MAAM,8BAA8B;AAEpC,MAAa,gBAAgB,MAAgC;AAC3D,KAAI,OAAO,eAAe,YAAa,QAAO;AAC9C,QAAO,aAAa;;AAGtB,MAAM,iBAAiB,MAA6B;AAClD,KAAI,OAAO,YAAY,YAAa,QAAO;AAC3C,QAAO,aAAa;;;;;AAMtB,MAAa,aAAa,YAAyB;AAKjD,QAHiB,iBAAiB,QAAQ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