Skip to content

Commit

Permalink
Filter focusable elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed May 30, 2024
1 parent 55f746c commit 0468372
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-planets-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/loock": patch
---

Filter focusable elements.
15 changes: 11 additions & 4 deletions src/focusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export interface FocusManagerOptions {
exclude?: string[];
}

function isFocusableElement(element: HTMLElement) {
if (!element.isConnected) {
return false;
}
const { width, height } = element.getBoundingClientRect();
return !!height && !!width;
}

/**
* Find all focusable elements by options.
* @param node The target node.
Expand All @@ -34,10 +42,10 @@ function findFocusableByOptions(
}

if (typeof elements === 'function') {
return elements();
return elements().filter(isFocusableElement);
}

return elements;
return elements.filter(isFocusableElement);
}

/**
Expand Down Expand Up @@ -70,8 +78,7 @@ function findFocusableChildren(
}
}

const { width, height } = element.getBoundingClientRect();
return !!height && !!width;
return isFocusableElement(element);
});
}

Expand Down

0 comments on commit 0468372

Please sign in to comment.