From 7702ca025fcae6209ae3851d0ccd25579f93e243 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Thu, 30 Jun 2022 10:05:35 +0200 Subject: [PATCH] fix: error on modals missing focusable elements --- src/renderer/composables/useFocusTrap.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/renderer/composables/useFocusTrap.ts b/src/renderer/composables/useFocusTrap.ts index 8f02021d..7fd7c5c8 100644 --- a/src/renderer/composables/useFocusTrap.ts +++ b/src/renderer/composables/useFocusTrap.ts @@ -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 () {