From 371e021c9d6dab549d7dee1ed758b36aa21dded7 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Thu, 12 Oct 2023 16:59:43 -0400 Subject: [PATCH] fix: improve tabbable performance --- src/internal/tabbable.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/internal/tabbable.ts b/src/internal/tabbable.ts index fca4ec5c..c296c3dd 100644 --- a/src/internal/tabbable.ts +++ b/src/internal/tabbable.ts @@ -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; }