From da76c848fc6d6e90e8d8c89b9bc7a8a7c059c4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 6 May 2021 20:03:36 +0200 Subject: [PATCH] Avoid rendering an empty list in footer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We've had an accessibility error reported by the Spanish "Portal Administración electrónica" (PAe). While I can't find any accessibility rule saying empty lists should be avoided, it looks like some screen readers report finding lists with 0 items, which is annoying. We could also do it with CSS using `ul:empty { display: none}`. However, at the time of writing no browser supports this rule when the tag contains whitespace. --- app/components/layout/social_component.rb | 6 ++- .../layout/social_component_spec.rb | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 spec/components/layout/social_component_spec.rb diff --git a/app/components/layout/social_component.rb b/app/components/layout/social_component.rb index 1391bd76a..3dd944c0e 100644 --- a/app/components/layout/social_component.rb +++ b/app/components/layout/social_component.rb @@ -1,6 +1,10 @@ class Layout::SocialComponent < ApplicationComponent delegate :content_block, to: :helpers + def render? + sites.any? || footer_content_block.present? + end + private def sites @@ -10,7 +14,7 @@ class Layout::SocialComponent < ApplicationComponent youtube: "https://www.youtube.com", telegram: "https://www.telegram.me", instragram: "https://www.instagram.com" - }.select { |name, _| setting["#{name}_handle"] } + }.select { |name, _| setting["#{name}_handle"].present? } end def link_title(site_name) diff --git a/spec/components/layout/social_component_spec.rb b/spec/components/layout/social_component_spec.rb new file mode 100644 index 000000000..29d29aa81 --- /dev/null +++ b/spec/components/layout/social_component_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +describe Layout::SocialComponent, type: :component do + describe "#render?" do + it "renders when a social setting is present" do + Setting["twitter_handle"] = "myhandle" + + render_inline Layout::SocialComponent.new + + expect(page).to have_css "ul" + end + + it "renders when a content block is present" do + Setting["twitter_handle"] = "" + Setting["facebook_handle"] = "" + Setting["youtube_handle"] = "" + Setting["telegram_handle"] = "" + Setting["instagram_handle"] = "" + + create(:site_customization_content_block, name: "footer") + + render_inline Layout::SocialComponent.new + + expect(page).to have_css "ul" + end + + it "does not render with no settings present and no content block present" do + Setting["twitter_handle"] = "" + Setting["facebook_handle"] = "" + Setting["youtube_handle"] = "" + Setting["telegram_handle"] = "" + Setting["instagram_handle"] = "" + + render_inline Layout::SocialComponent.new + + expect(page).not_to have_css "ul" + end + end +end