diff --git a/js/skip-link.js b/js/skip-link.js new file mode 100644 index 00000000..502f079b --- /dev/null +++ b/js/skip-link.js @@ -0,0 +1,26 @@ +/** + * @file Handles IOS bug when trying to use skip link with keyboard. + */ + +(function localgovSkiplink(Drupal) { + Drupal.behaviors.skiplink = { + attach: function (context) { + const [anchor] = once('maincontent', '[href="#main-content"]', context); + if (!anchor) { + return; + } + anchor.addEventListener('keydown', (e) => { + focusContent(e); + }); + + function focusContent(e) { + const { key } = e; + if (key === 'Enter') { + var mainContent = document.querySelector('#main-content'); + mainContent.focus(); + mainContent.setAttribute('tabindex', 0); + } + } + } + }; +}(Drupal)); diff --git a/localgov_base.libraries.yml b/localgov_base.libraries.yml index 3ce9f3be..a5e57bbd 100644 --- a/localgov_base.libraries.yml +++ b/localgov_base.libraries.yml @@ -317,3 +317,10 @@ add-to-calendar: dependencies: - core/drupal - core/once + +skip-link: + js: + js/skip-link.js: {} + dependencies: + - core/drupal + - core/once diff --git a/localgov_base.theme b/localgov_base.theme index 57ca9523..7d455dcc 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -197,3 +197,10 @@ function localgov_base_preprocess_container(&$variables) { function localgov_base_form_preview_link_entity_form_alter(&$form, $form_state, $form_id) { $form['#attached']['library'][''] = 'localgov_base/preview-link'; } + +/** + * Implements hook_preprocess_html(). + */ +function localgov_base_preprocess_html(&$variables) { + $variables['#attached']['library'][] = 'localgov_base/skip-link'; +} diff --git a/templates/layout/page--moderngov-template.html.twig b/templates/layout/page--moderngov-template.html.twig index 8309d803..b772d1e6 100644 --- a/templates/layout/page--moderngov-template.html.twig +++ b/templates/layout/page--moderngov-template.html.twig @@ -6,7 +6,9 @@ {breadcrumb} -
{# The "skip to content" link jumps to here. #} +
+ {# The "skip to content" link jumps to here. #} +