| 1 |
const FOCUSABLE_ELEMENTS = [ |
| 2 |
"a[href]", |
| 3 |
'input:not([disabled]):not([type="hidden"]):not([aria-hidden])', |
| 4 |
"select:not([disabled]):not([aria-hidden])", |
| 5 |
"textarea:not([disabled]):not([aria-hidden])", |
| 6 |
"button:not([disabled]):not([aria-hidden])", |
| 7 |
"object", |
| 8 |
"[contenteditable]", |
| 9 |
].join(", "); |
| 10 |
|
| 11 |
export function getFocusableElements(el: Element): Element[] { |
| 12 |
return [...el.querySelectorAll(FOCUSABLE_ELEMENTS)]; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Focus a node (if possible). |
| 17 |
* @returns whether focusing succeeded. |
| 18 |
*/ |
| 19 |
export function attemptFocus(el: Node): boolean { |
| 20 |
try { |
| 21 |
// @ts-expect-error We are attempting to focus any kind of element here. |
| 22 |
// eslint-disable-next-line @typescript-eslint/no-unsafe-call |
| 23 |
el.focus(); |
| 24 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 25 |
} catch (e) { |
| 26 |
// pass |
| 27 |
} |
| 28 |
return document.activeElement === el; |
| 29 |
} |