File size: 829 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Return the focusable children of an HTML element
 * @param {HTMLElement} parent HTML element to get the focusable children from
 * @returns {HTMLElement[]} Collection of focusable elements
 */
export function getFocusableElements( parent: HTMLElement ): NodeListOf< HTMLElement > {
	return parent.querySelectorAll(
		'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
	);
}

/**
 * Return the last focusable child of an HTML element
 * @param {HTMLElement} parent HTML element to get the focusable child from
 * @returns {HTMLElement} Last focusable child
 */
export function getLastFocusableElement( parent: HTMLElement ): HTMLElement | undefined {
	const focusableElts = getFocusableElements( parent );

	return focusableElts.length > 0 ? focusableElts[ focusableElts.length - 1 ] : undefined;
}