Skip to content

Commit

Permalink
Update focus to where the page is scrolled to
Browse files Browse the repository at this point in the history
  • Loading branch information
rafgraph committed Oct 8, 2020
1 parent bb2ff87 commit d57be48
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d57be48

Please sign in to comment.