Add support for n attempts for dynamic components

This commit is contained in:
Belen Curcio 2021-01-06 12:05:24 -03:00
parent 87be4c8c5c
commit a78d667031

View File

@ -28,21 +28,23 @@ 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
// Up to 4 seconds of load time threshold // Up to 4 seconds of load time threshold
let match = false let match = false
let end = 22 // Try to find match at least n times. let end = 22 // Try to find match at least n times
let i = 0 let i = 0
const timer = setInterval( const timer = setInterval(
() => { () => {
console.log('-----------', i) console.log('-----------', i)
if (!match !== i > end) { if (!match !== i > end) {
match = !!tabbable(root.current).length match = !!tabbable(root.current).length
if (root.current) { if (match) {
// Attempt to focus the first el
tabbable(root.current)[0].focus() tabbable(root.current)[0].focus()
} }
i = i + 1 i = i + 1
} else { } else {
// Clear interval after n attempts
clearInterval(timer) clearInterval(timer)
} }
}, },