From 9e4d27a0fa357d6b385f08c4b13cc7899c5f846e Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Fri, 12 Mar 2021 08:46:34 -0800 Subject: [PATCH] dom: Add types to focusable (#29787) --- packages/dom/src/focusable.js | 13 ++++++++++--- packages/dom/tsconfig.json | 5 ++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/dom/src/focusable.js b/packages/dom/src/focusable.js index 35b1c9ac637bf5..80e9dcb4968318 100644 --- a/packages/dom/src/focusable.js +++ b/packages/dom/src/focusable.js @@ -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. */ @@ -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 + '"]' ); @@ -91,6 +93,9 @@ function isValidFocusableArea( element ) { * @return {Element[]} Focusable elements. */ export function find( context ) { + /* eslint-disable jsdoc/no-undefined-types */ + /** @type {NodeListOf} */ + /* eslint-enable jsdoc/no-undefined-types */ const elements = context.querySelectorAll( SELECTOR ); return Array.from( elements ).filter( ( element ) => { @@ -100,7 +105,9 @@ export function find( context ) { const { nodeName } = element; if ( 'AREA' === nodeName ) { - return isValidFocusableArea( element ); + return isValidFocusableArea( + /** @type {HTMLAreaElement} */ ( element ) + ); } return true; diff --git a/packages/dom/tsconfig.json b/packages/dom/tsconfig.json index 8821a076a20e5b..d362c0bc6e5874 100644 --- a/packages/dom/tsconfig.json +++ b/packages/dom/tsconfig.json @@ -4,5 +4,8 @@ "rootDir": "src", "declarationDir": "build-types" }, - "include": [ "src/data-transfer.js" ] + "include": [ + "src/data-transfer.js", + "src/focusable.js", + ] } \ No newline at end of file