fix: error on modals missing focusable elements

This commit is contained in:
Fabio Di Stasio 2022-06-30 10:05:35 +02:00 committed by GitHub
parent e97da37103
commit 7702ca025f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 5 deletions

View File

@ -60,11 +60,14 @@ const useFocusTrap = (args?: {disableAutofocus?: boolean}) => {
focusableElements = (trapRef.value as HTMLElement).querySelectorAll(
focusableElementsSelector
);
$firstFocusable = focusableElements[0];
$lastFocusable = focusableElements[focusableElements.length - 1];
document.addEventListener('keydown', keyHandler);
isInitiated.value = true;
if (!localArgs.disableAutofocus) $firstFocusable.focus();
if (focusableElements.length) {
$firstFocusable = focusableElements[0];
$lastFocusable = focusableElements[focusableElements.length - 1];
document.addEventListener('keydown', keyHandler);
isInitiated.value = true;
if (!localArgs.disableAutofocus) $firstFocusable.focus();
}
}
function clearFocusTrap () {