diff --git a/app/assets/stylesheets/_consul_settings.scss b/app/assets/stylesheets/_consul_settings.scss index 3bbb2dd03..bfd791968 100644 --- a/app/assets/stylesheets/_consul_settings.scss +++ b/app/assets/stylesheets/_consul_settings.scss @@ -14,6 +14,7 @@ $black: #222 !default; $white: #fdfdfd !default; +$dark-gray: #8a8a8a !default; $body-background: $white; $body-font-family: "Source Sans Pro", "Helvetica", "Arial", sans-serif !important !default; @@ -51,6 +52,8 @@ $orbit-caption-background: #eee; $show-header-for-stacked: true !default; +$switch-background: $dark-gray !default; + // 2. CONSUL DEMOCRACY variables // ----------------------------- diff --git a/app/components/layout/cookies_consent/switch_component.html.erb b/app/components/layout/cookies_consent/switch_component.html.erb new file mode 100644 index 000000000..02f7b201a --- /dev/null +++ b/app/components/layout/cookies_consent/switch_component.html.erb @@ -0,0 +1,11 @@ +
+

<%= label %>

+
<%= simple_format(description) %>
+
+
+ <%= check_box_tag name, "1", checked?, class: "switch-input", disabled: disabled?, "aria-label": label %> + +
diff --git a/app/components/layout/cookies_consent/switch_component.rb b/app/components/layout/cookies_consent/switch_component.rb new file mode 100644 index 000000000..1c03a30a9 --- /dev/null +++ b/app/components/layout/cookies_consent/switch_component.rb @@ -0,0 +1,21 @@ +class Layout::CookiesConsent::SwitchComponent < ApplicationComponent + attr_reader :checked, :disabled, :name, :label, :description + + def initialize(name, label:, description:, checked: false, disabled: false) + @name = name + @label = label + @description = description + @checked = checked + @disabled = disabled + end + + private + + def checked? + checked == true + end + + def disabled? + disabled == true + end +end diff --git a/spec/components/layout/cookies_consent/switch_component_spec.rb b/spec/components/layout/cookies_consent/switch_component_spec.rb new file mode 100644 index 000000000..3a4de9df5 --- /dev/null +++ b/spec/components/layout/cookies_consent/switch_component_spec.rb @@ -0,0 +1,62 @@ +require "rails_helper" + +describe Layout::CookiesConsent::SwitchComponent do + let(:component) do + Layout::CookiesConsent::SwitchComponent.new( + "cookie_name", + label: "Accept Cookies", + description: "Enable or disable cookies for the site." + ) + end + + it "renders the switch component with the correct label and description" do + render_inline(component) + + expect(page).to have_css ".name", text: "Accept Cookies" + expect(page).to have_css ".description", text: "Enable or disable cookies for the site." + expect(page).to have_css "input.switch-input[type='checkbox'][name='cookie_name']" + expect(page).not_to have_css "[type='checkbox'][checked]" + expect(page).not_to have_css "[type='checkbox'][disabled]" + end + + it "renders the checkbox as checked when `checked` is true" do + render_inline( + Layout::CookiesConsent::SwitchComponent.new( + "cookie_name", + label: "Accept Cookies", + description: "Enable or disable cookies for the site.", + checked: true + ) + ) + + expect(page).to have_css "input[type='checkbox'][checked]" + end + + it "renders the checkbox as disabled when `disabled` is true" do + render_inline( + Layout::CookiesConsent::SwitchComponent.new( + "cookie_name", + label: "Accept Cookies", + description: "Enable or disable cookies for the site.", + disabled: true + ) + ) + + expect(page).to have_css "input[type='checkbox'][disabled]" + end + + it "renders the correct accessibility elements" do + render_inline(component) + + expect(page).to have_field "Accept Cookies" + expect(page).to have_css "[aria-hidden]", exact_text: "Yes" + expect(page).to have_css "[aria-hidden]", exact_text: "No" + end + + it "renders the yes and no states for the switch" do + render_inline(component) + + expect(page).to have_css ".switch-active", text: "Yes" + expect(page).to have_css ".switch-inactive", text: "No" + end +end