Adding delay for dynamic

This commit is contained in:
Belen Curcio 2021-01-06 11:44:38 -03:00
parent e42880b088
commit 4594945e02

View File

@ -29,21 +29,18 @@ export default function FocusTrap({ children, focusFirst = false }: Props) {
const selectFirstFocusableEl = () => { const selectFirstFocusableEl = () => {
// Try to find focusable elements, if match then focus. // Try to find focusable elements, if match then focus.
let match = null let match = false
let end = 20 // Try to find match at least 10 times. let end = 20 // Try to find match at least n times.
let i = 0 let i = 0
const timer = setTimeout(() => {
while (!match !== i > end) { if (!match !== i > end) {
console.log('-----------', i) match = !!tabbable(root.current).length
match = !!tabbable(root.current).length tabbable(root.current)[0].focus()
if (match) { } else {
const delay = i === 0 ? 0 : 200 clearTimeout(timer)
setTimeout(() => {
tabbable(root.current)[0].focus()
}, delay)
} }
i = i + 1 console.log('-----------', i)
} }, 100)
} }
useEffect(() => { useEffect(() => {