From a2f6e03516c730832f296fe8a2d6b659bcb41152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 29 Jun 2021 00:11:03 +0200 Subject: [PATCH] Move advanced search toggle button inside the form Since forms are landmarks, screen reader users might navigate to the form. But then they were going to find an empty form with no way to toggle it. Moving the button inside the form means screen reader users navigating to the form will find the button to toggle it. It also helps us simplifying the code; there's no need to use data-attributes to communicate whether the form should be visible since now we can easily use the button `aria-expanded` attribute. We could further simplify the JavaScript if we used a CSS rule to show/hide the form fields based on the toggle button `aria-expanded` attribute. However, implementing the "slide" animation we use when toggling the form with CSS is difficult and unreliable. --- app/assets/javascripts/advanced_search.js | 9 ++--- app/assets/stylesheets/advanced_search.scss | 39 ++++++++++--------- .../shared/advanced_search_component.html.erb | 6 +-- .../shared/advanced_search_component_spec.rb | 2 +- spec/system/advanced_search_spec.rb | 4 +- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/app/assets/javascripts/advanced_search.js b/app/assets/javascripts/advanced_search.js index 89f8be04f..d987fda8a 100644 --- a/app/assets/javascripts/advanced_search.js +++ b/app/assets/javascripts/advanced_search.js @@ -1,9 +1,6 @@ (function() { "use strict"; App.AdvancedSearch = { - advanced_search_terms: function() { - return $("#js-advanced-search").data("advanced-search-terms"); - }, toggle_date_options: function() { if ($("#js-advanced-search-date-min").val() === "custom") { $("#js-custom-date").show(); @@ -18,15 +15,15 @@ toggle_button.removeAttr("hidden"); - if (App.AdvancedSearch.advanced_search_terms()) { + if (toggle_button.attr("aria-expanded") === "true") { App.AdvancedSearch.toggle_date_options(); } else { - $("#js-advanced-search").hide(); + toggle_button.next().hide(); } toggle_button.on({ click: function() { $(this).attr("aria-expanded", !JSON.parse($(this).attr("aria-expanded"))); - $("#js-advanced-search").slideToggle(); + $(this).next().slideToggle(); } }); $("#js-advanced-search-date-min").on({ diff --git a/app/assets/stylesheets/advanced_search.scss b/app/assets/stylesheets/advanced_search.scss index 394a0d0b6..b6bf6c5ec 100644 --- a/app/assets/stylesheets/advanced_search.scss +++ b/app/assets/stylesheets/advanced_search.scss @@ -1,23 +1,6 @@ -.advanced-search { - color: $link; - cursor: pointer; - margin: $line-height 0; - - @include breakpoint(medium) { - float: right; - margin-bottom: 0; - margin-top: $line-height / 4; - position: absolute; - right: 0; - } - - &:focus { - outline: $outline-focus; - } -} - .advanced-search-form { @include grid-row-nest; + position: relative; @include breakpoint(large) { .filter { @@ -43,6 +26,26 @@ } } + > [aria-expanded] { + @include xy-gutters; + color: $link; + cursor: pointer; + margin-top: $line-height; + margin-bottom: $line-height; + + @include breakpoint(medium) { + float: right; + margin-bottom: 0; + margin-top: $line-height / 4; + position: absolute; + right: 0; + } + + &:focus { + outline: $outline-focus; + } + } + .general-search, .filter, .submit { diff --git a/app/components/shared/advanced_search_component.html.erb b/app/components/shared/advanced_search_component.html.erb index f221eed99..1155ef057 100644 --- a/app/components/shared/advanced_search_component.html.erb +++ b/app/components/shared/advanced_search_component.html.erb @@ -1,11 +1,9 @@ -
+<%= form_tag request.path, id: "advanced_search_form", class: "advanced-search-form", method: :get do %> -
-<%= form_tag request.path, id: "advanced_search_form", class: "advanced-search-form", method: :get do %> -