From f320044aa9820c198ea7d6d684378f9f594fc887 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sat, 13 Sep 2025 16:20:46 +0530 Subject: [PATCH] restore tabindex if unable to focus even after modifying tabindex --- src/client/app/router.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 846010a2..5b24ed74 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -290,15 +290,16 @@ export function scrollTo(hash: string, smooth = false, scrollPosition = 0) { // target is not focusable, make it temporarily focusable const tabindex = target.getAttribute('tabindex') target.setAttribute('tabindex', '-1') - target.addEventListener( - 'blur', - () => { - if (tabindex == null) target.removeAttribute('tabindex') - else target.setAttribute('tabindex', tabindex) - }, - { once: true } - ) + + const restoreTabindex = () => { + if (tabindex == null) target.removeAttribute('tabindex') + else target.setAttribute('tabindex', tabindex) + target.removeEventListener('blur', restoreTabindex) + } + target.addEventListener('blur', restoreTabindex) + target.focus({ preventScroll: true }) + if (document.activeElement !== target) restoreTabindex() } requestAnimationFrame(scrollToTarget)