From 4594945e021e58545cac73ddd5709a8b32c83137 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 6 Jan 2021 11:44:38 -0300 Subject: [PATCH] Adding delay for dynamic --- lib/focus-trap.tsx | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/focus-trap.tsx b/lib/focus-trap.tsx index 688ac562e..d0fe87a43 100644 --- a/lib/focus-trap.tsx +++ b/lib/focus-trap.tsx @@ -29,21 +29,18 @@ export default function FocusTrap({ children, focusFirst = false }: Props) { const selectFirstFocusableEl = () => { // Try to find focusable elements, if match then focus. - let match = null - let end = 20 // Try to find match at least 10 times. + let match = false + let end = 20 // Try to find match at least n times. let i = 0 - - while (!match !== i > end) { - console.log('-----------', i) - match = !!tabbable(root.current).length - if (match) { - const delay = i === 0 ? 0 : 200 - setTimeout(() => { - tabbable(root.current)[0].focus() - }, delay) + const timer = setTimeout(() => { + if (!match !== i > end) { + match = !!tabbable(root.current).length + tabbable(root.current)[0].focus() + } else { + clearTimeout(timer) } - i = i + 1 - } + console.log('-----------', i) + }, 100) } useEffect(() => {