Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dom: Add types to focusable #29787

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a shame those are necessary 😭

/** @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",
]
}