diff --git a/hdbt.libraries.yml b/hdbt.libraries.yml index 47f438858..0381cf299 100644 --- a/hdbt.libraries.yml +++ b/hdbt.libraries.yml @@ -191,3 +191,8 @@ news-archive: dependencies: - core/drupalSettings - core/drupal + +search-helper: + version: 1.x + js: + src/js/search-helper.js: {} diff --git a/hdbt.theme b/hdbt.theme index e09eeb54d..a871d09b3 100644 --- a/hdbt.theme +++ b/hdbt.theme @@ -1687,3 +1687,10 @@ function hdbt_theme_suggestions_container_alter(array &$suggestions, array &$var array_unshift($suggestions, 'container__' . $type . '__' . $name); } + +/** + * Implements hook_preprocess_HOOK(). + */ +function hdbt_preprocess_views_exposed_form(array &$variables) { + $variables['#attached']['library'][] = 'hdbt/search-helper'; +} diff --git a/src/js/search-helper.js b/src/js/search-helper.js new file mode 100644 index 000000000..c7fb0da32 --- /dev/null +++ b/src/js/search-helper.js @@ -0,0 +1,15 @@ +(function ($) { + + // Set focus on search result count + $(document).ajaxComplete(function onDataLoaded(e, xhr, settings) { + // Check form ID to prevent mixing on multi-form page + const viewDomId = settings.extraData.view_dom_id; + const resultsContainerEl = $(`[data-id-number=${ viewDomId }]`); + const resultCountEl = $('[class$="__count-container"]', resultsContainerEl[0])[0]; + + if (!resultCountEl) return; + resultCountEl.setAttribute('tabindex', '-1'); + resultCountEl.focus(); + resultCountEl.scrollIntoView({behavior: 'smooth', block: 'center'}); + }); +})(jQuery); diff --git a/templates/views/views-view--service-list--block-search.html.twig b/templates/views/views-view--service-list--block-search.html.twig index 9a2ca9cb4..a4141fb22 100644 --- a/templates/views/views-view--service-list--block-search.html.twig +++ b/templates/views/views-view--service-list--block-search.html.twig @@ -50,19 +50,22 @@ {% endif %} - {% if is_ajax_request or total_rows > pager_items_per_page %} - {{ exposed }} + {{ exposed }} +