2020-09-02 10:26:32 +08:00
|
|
|
export const EVENT_CODE = {
|
|
|
|
tab: 'Tab',
|
|
|
|
enter: 'Enter',
|
|
|
|
space: 'Space',
|
2020-10-03 20:13:19 +08:00
|
|
|
left: 'ArrowLeft', // 37
|
|
|
|
up: 'ArrowUp', // 38
|
|
|
|
right: 'ArrowRight', // 39
|
|
|
|
down: 'ArrowDown', // 40
|
2020-09-09 21:18:08 +08:00
|
|
|
esc: 'Escape',
|
2020-09-02 10:26:32 +08:00
|
|
|
delete: 'Delete',
|
|
|
|
backspace: 'Backspace',
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:46:06 +08:00
|
|
|
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])`
|
2020-09-02 10:26:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the testing element is visible on screen no matter if its on the viewport or not
|
|
|
|
*/
|
|
|
|
export const isVisible = (element: HTMLElement) => {
|
|
|
|
if (process.env.NODE_ENV === 'test') return true
|
|
|
|
const computed = getComputedStyle(element)
|
|
|
|
// element.offsetParent won't work on fix positioned
|
|
|
|
// WARNING: potential issue here, going to need some expert advices on this issue
|
|
|
|
return computed.position === 'fixed' ? false : element.offsetParent !== null
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const obtainAllFocusableElements = (
|
|
|
|
element: HTMLElement
|
|
|
|
): HTMLElement[] => {
|
2021-09-30 15:07:44 +08:00
|
|
|
return Array.from(
|
|
|
|
element.querySelectorAll<HTMLElement>(FOCUSABLE_ELEMENT_SELECTORS)
|
|
|
|
).filter((item: HTMLElement) => isFocusable(item) && isVisible(item))
|
2020-09-02 10:26:32 +08:00
|
|
|
}
|
|
|
|
|
2020-07-28 17:48:40 +08:00
|
|
|
/**
|
|
|
|
* @desc Determine if target element is focusable
|
|
|
|
* @param element {HTMLElement}
|
|
|
|
* @returns {Boolean} true if it is focusable
|
|
|
|
*/
|
2020-07-28 20:31:26 +08:00
|
|
|
export const isFocusable = (element: HTMLElement): boolean => {
|
2020-07-28 17:48:40 +08:00
|
|
|
if (
|
|
|
|
element.tabIndex > 0 ||
|
|
|
|
(element.tabIndex === 0 && element.getAttribute('tabIndex') !== null)
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// HTMLButtonElement has disabled
|
|
|
|
if ((element as HTMLButtonElement).disabled) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (element.nodeName) {
|
|
|
|
case 'A': {
|
|
|
|
// casting current element to Specific HTMLElement in order to be more type precise
|
2021-09-04 19:29:28 +08:00
|
|
|
return (
|
|
|
|
!!(element as HTMLAnchorElement).href &&
|
|
|
|
(element as HTMLAnchorElement).rel !== 'ignore'
|
|
|
|
)
|
2020-07-28 17:48:40 +08:00
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
case 'INPUT': {
|
|
|
|
return !(
|
|
|
|
(element as HTMLInputElement).type === 'hidden' ||
|
|
|
|
(element as HTMLInputElement).type === 'file'
|
|
|
|
)
|
2020-07-28 17:48:40 +08:00
|
|
|
}
|
|
|
|
case 'BUTTON':
|
|
|
|
case 'SELECT':
|
|
|
|
case 'TEXTAREA': {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Set Attempt to set focus on the current node.
|
|
|
|
* @param element
|
|
|
|
* The node to attempt to focus on.
|
|
|
|
* @returns
|
|
|
|
* true if element is focused.
|
|
|
|
*/
|
2020-07-28 20:31:26 +08:00
|
|
|
export const attemptFocus = (element: HTMLElement): boolean => {
|
2020-07-28 17:48:40 +08:00
|
|
|
if (!isFocusable(element)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
Utils.IgnoreUtilFocusChanges = true
|
|
|
|
// Remove the old try catch block since there will be no error to be thrown
|
2020-09-02 10:26:32 +08:00
|
|
|
element.focus?.()
|
2020-07-28 17:48:40 +08:00
|
|
|
Utils.IgnoreUtilFocusChanges = false
|
|
|
|
return document.activeElement === element
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger an event
|
|
|
|
* mouseenter, mouseleave, mouseover, keyup, change, click, etc.
|
|
|
|
* @param {HTMLElement} elm
|
|
|
|
* @param {String} name
|
|
|
|
* @param {*} opts
|
|
|
|
*/
|
2021-09-04 19:29:28 +08:00
|
|
|
export const triggerEvent = function (
|
|
|
|
elm: HTMLElement,
|
|
|
|
name: string,
|
|
|
|
...opts: Array<boolean>
|
|
|
|
): HTMLElement {
|
2020-07-28 17:48:40 +08:00
|
|
|
let eventName: string
|
|
|
|
|
|
|
|
if (name.includes('mouse') || name.includes('click')) {
|
|
|
|
eventName = 'MouseEvents'
|
|
|
|
} else if (name.includes('key')) {
|
|
|
|
eventName = 'KeyboardEvent'
|
|
|
|
} else {
|
|
|
|
eventName = 'HTMLEvents'
|
|
|
|
}
|
|
|
|
const evt = document.createEvent(eventName)
|
|
|
|
|
|
|
|
evt.initEvent(name, ...opts)
|
|
|
|
elm.dispatchEvent(evt)
|
|
|
|
return elm
|
|
|
|
}
|
|
|
|
|
|
|
|
const Utils = {
|
|
|
|
IgnoreUtilFocusChanges: false,
|
|
|
|
/**
|
|
|
|
* @desc Set focus on descendant nodes until the first focusable element is
|
|
|
|
* found.
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
* DOM node for which to find the first focusable descendant.
|
|
|
|
* @returns {Boolean}
|
|
|
|
* true if a focusable element is found and focus is set.
|
|
|
|
*/
|
2021-09-04 19:29:28 +08:00
|
|
|
focusFirstDescendant(element: HTMLElement): boolean {
|
2020-07-28 17:48:40 +08:00
|
|
|
for (let i = 0; i < element.childNodes.length; i++) {
|
2020-09-30 10:50:39 +08:00
|
|
|
const child = element.childNodes[i] as HTMLElement
|
2021-09-04 19:29:28 +08:00
|
|
|
if (attemptFocus(child) || this.focusFirstDescendant(child)) {
|
2020-07-28 17:48:40 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @desc Find the last descendant node that is focusable.
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
* DOM node for which to find the last focusable descendant.
|
|
|
|
* @returns {Boolean}
|
|
|
|
* true if a focusable element is found and focus is set.
|
|
|
|
*/
|
2021-09-04 19:29:28 +08:00
|
|
|
focusLastDescendant(element: HTMLElement): boolean {
|
2020-07-28 17:48:40 +08:00
|
|
|
for (let i = element.childNodes.length - 1; i >= 0; i--) {
|
2020-09-30 10:50:39 +08:00
|
|
|
const child = element.childNodes[i] as HTMLElement
|
2021-09-04 19:29:28 +08:00
|
|
|
if (attemptFocus(child) || this.focusLastDescendant(child)) {
|
2020-07-28 17:48:40 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Utils
|