fix: improve tabbable performance

pull/1614/head
konnorrogers 2023-10-12 16:59:43 -04:00
rodzic d5ab0fef22
commit 371e021c9d
1 zmienionych plików z 7 dodań i 4 usunięć

Wyświetl plik

@ -1,5 +1,8 @@
import { offsetParent } from 'composed-offset-position';
// It doesn't technically check visibility, it checks if the element has been rendered and can maybe possibly be tabbed to.
// This is a workaround for shadowroots not having an `offsetParent`
function isTakingUpSpace (elem: HTMLElement): boolean {
return Boolean( elem.offsetParent || elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
}
/** Determines if the specified element is tabbable using heuristics inspired by https://github.com/focus-trap/tabbable */
function isTabbable(el: HTMLElement) {
const tag = el.tagName.toLowerCase();
@ -20,8 +23,8 @@ function isTabbable(el: HTMLElement) {
}
// Elements that are hidden have no offsetParent and are not tabbable
// offsetParent() is added because otherwise it misses elements in Safari
if (el.offsetParent === null && offsetParent(el) === null) {
// !isRendered is added because otherwise it misses elements in Safari
if (!isTakingUpSpace(el)) {
return false;
}