Skip to content

Commit

Permalink
dom: Add types to focusable (#29787)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend authored Mar 12, 2021
1 parent 17be89a commit 9e4d27a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/dom/src/focusable.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const SELECTOR = [
* Returns true if the specified element is visible (i.e. neither display: none
* nor visibility: hidden).
*
* @param {Element} element DOM element to test.
* @param {HTMLElement} element DOM element to test.
*
* @return {boolean} Whether element is visible.
*/
Expand Down Expand Up @@ -67,16 +67,18 @@ function skipFocus( element ) {
* false otherwise. Area is only focusable if within a map where a named map
* referenced by an image somewhere in the document.
*
* @param {Element} element DOM area element to test.
* @param {HTMLAreaElement} element DOM area element to test.
*
* @return {boolean} Whether area element is valid for focus.
*/
function isValidFocusableArea( element ) {
/** @type {HTMLMapElement | null} */
const map = element.closest( 'map[name]' );
if ( ! map ) {
return false;
}

/** @type {HTMLImageElement | null} */
const img = element.ownerDocument.querySelector(
'img[usemap="#' + map.name + '"]'
);
Expand All @@ -91,6 +93,9 @@ function isValidFocusableArea( element ) {
* @return {Element[]} Focusable elements.
*/
export function find( context ) {
/* eslint-disable jsdoc/no-undefined-types */
/** @type {NodeListOf<HTMLElement>} */
/* eslint-enable jsdoc/no-undefined-types */
const elements = context.querySelectorAll( SELECTOR );

return Array.from( elements ).filter( ( element ) => {
Expand All @@ -100,7 +105,9 @@ export function find( context ) {

const { nodeName } = element;
if ( 'AREA' === nodeName ) {
return isValidFocusableArea( element );
return isValidFocusableArea(
/** @type {HTMLAreaElement} */ ( element )
);
}

return true;
Expand Down
5 changes: 4 additions & 1 deletion packages/dom/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "src/data-transfer.js" ]
"include": [
"src/data-transfer.js",
"src/focusable.js",
]
}

0 comments on commit 9e4d27a

Please sign in to comment.