From d57be4898b4d2e20afca029db7d218bb7001411b Mon Sep 17 00:00:00 2001 From: Rafael Pedicini Date: Thu, 8 Oct 2020 18:08:26 -0400 Subject: [PATCH] Update focus to where the page is scrolled to --- src/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/index.js b/src/index.js index 977e5c8..78b6f38 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,18 @@ function getElAndScroll() { const element = document.getElementById(hashFragment); if (element !== null) { scrollFunction(element); + + // update focus to where the page is scrolled to + // unfortunately this doesn't work in safari (desktop and iOS) when blur() is called + let originalTabIndex = element.getAttribute('tabindex'); + if (originalTabIndex === null) element.setAttribute('tabindex', -1); + element.focus({ preventScroll: true }); + // for some reason calling blur() in safari resets the focus region to where it was previously, + // if blur() is not called it works in safari, but then are stuck with default focus styles + // on an element that otherwise might never had focus styles applied, so not an option + element.blur(); + if (originalTabIndex === null) element.removeAttribute('tabindex'); + reset(); return true; }