Skip to content

Commit d57be48

Browse files
committed
Update focus to where the page is scrolled to
1 parent bb2ff87 commit d57be48

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ function getElAndScroll() {
2020
const element = document.getElementById(hashFragment);
2121
if (element !== null) {
2222
scrollFunction(element);
23+
24+
// update focus to where the page is scrolled to
25+
// unfortunately this doesn't work in safari (desktop and iOS) when blur() is called
26+
let originalTabIndex = element.getAttribute('tabindex');
27+
if (originalTabIndex === null) element.setAttribute('tabindex', -1);
28+
element.focus({ preventScroll: true });
29+
// for some reason calling blur() in safari resets the focus region to where it was previously,
30+
// if blur() is not called it works in safari, but then are stuck with default focus styles
31+
// on an element that otherwise might never had focus styles applied, so not an option
32+
element.blur();
33+
if (originalTabIndex === null) element.removeAttribute('tabindex');
34+
2335
reset();
2436
return true;
2537
}

0 commit comments

Comments
 (0)