Skip to content

Commit f320044

Browse files
committed
restore tabindex if unable to focus even after modifying tabindex
1 parent fa4780d commit f320044

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/client/app/router.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,16 @@ export function scrollTo(hash: string, smooth = false, scrollPosition = 0) {
290290
// target is not focusable, make it temporarily focusable
291291
const tabindex = target.getAttribute('tabindex')
292292
target.setAttribute('tabindex', '-1')
293-
target.addEventListener(
294-
'blur',
295-
() => {
296-
if (tabindex == null) target.removeAttribute('tabindex')
297-
else target.setAttribute('tabindex', tabindex)
298-
},
299-
{ once: true }
300-
)
293+
294+
const restoreTabindex = () => {
295+
if (tabindex == null) target.removeAttribute('tabindex')
296+
else target.setAttribute('tabindex', tabindex)
297+
target.removeEventListener('blur', restoreTabindex)
298+
}
299+
target.addEventListener('blur', restoreTabindex)
300+
301301
target.focus({ preventScroll: true })
302+
if (document.activeElement !== target) restoreTabindex()
302303
}
303304

304305
requestAnimationFrame(scrollToTarget)

0 commit comments

Comments
 (0)