Skip to content

Commit

Permalink
add skip-links javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
martyn-w committed Jan 9, 2024
1 parent c495378 commit 94e0d76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/webpacker/javascript/skip_links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Ensure skip-links behave correctly when using VoiceOver on iOS or TalkBack on
// Android. See https://axesslab.com/skip-links/ for a discussion of the issue
// and suggested fixes (including the one below from Mike Foskett).

(_ => {
const skip_lnk = document.querySelector('.skiplink');
if (!skip_lnk) return false;
skip_lnk.addEventListener('click', e => {
e.preventDefault();
const to_obj = document.getElementById(skip_lnk.href.split('#')[1]);
if (to_obj) {
to_obj.setAttribute('tabindex', '-1');
to_obj.addEventListener('blur', e => {
to_obj.removeAttribute('tabindex');
}, {
once: true
});
to_obj.focus();
}
});
})();
1 change: 1 addition & 0 deletions app/webpacker/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require.context('../documents', true);

require('../javascript/perfume');
require('../javascript/zendesk_chat_reload');
require('../javascript/skip_links');

Rails.start();
Turbolinks.start();

0 comments on commit 94e0d76

Please sign in to comment.