Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

202408 skiplink ios keyboard #587

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions js/skip-link.js
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 7 additions & 0 deletions localgov_base.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,10 @@ add-to-calendar:
dependencies:
- core/drupal
- core/once

skip-link:
js:
js/skip-link.js: {}
dependencies:
- core/drupal
- core/once
7 changes: 7 additions & 0 deletions localgov_base.theme
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
4 changes: 3 additions & 1 deletion templates/layout/page--moderngov-template.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
{breadcrumb}
</div>

<main class="main" id="main-content"> {# The "skip to content" link jumps to here. #}
<main class="main">
{# The "skip to content" link jumps to here. #}
<a id="main-content" />

<div class="lgd-container">
<div class="lgd-row">
Expand Down
4 changes: 3 additions & 1 deletion templates/layout/page.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@

{{ page.messages }}

<main class="main" id="main-content"> {# The "skip to content" link jumps to here. #}
<main class="main">
{# The "skip to content" link jumps to here. #}
<a id="main-content" />

{% if has_content_top %}
{{ page.content_top }}
Expand Down