From 0ddaaa2bd89bcfa26121c66bf4a0c46449948eb5 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 2 May 2018 13:31:23 +0000 Subject: [PATCH] Modify subscription links component - add option to change link text - add option to turn feed link into a toggle to display an input box containing a link - includes toggle.js from static, included without changes --- CHANGELOG.md | 1 + .../govuk_publishing_components/lib/toggle.js | 69 +++++++++++++++ .../components/_subscription-links.scss | 20 +++++ .../components/_subscription-links.html.erb | 41 +++++++-- .../components/docs/subscription-links.yml | 22 +++++ config/locales/en.yml | 4 + spec/components/subscription_links_spec.rb | 12 +++ spec/javascripts/components/toggle-spec.js | 86 +++++++++++++++++++ 8 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/govuk_publishing_components/lib/toggle.js create mode 100644 spec/javascripts/components/toggle-spec.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 619ff75c7d..43225d6e9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased * Add department colours to components (PR #296) +* Modify subscription links component (PR #294) # 7.1.0 diff --git a/app/assets/javascripts/govuk_publishing_components/lib/toggle.js b/app/assets/javascripts/govuk_publishing_components/lib/toggle.js new file mode 100644 index 0000000000..466612f359 --- /dev/null +++ b/app/assets/javascripts/govuk_publishing_components/lib/toggle.js @@ -0,0 +1,69 @@ +/* + Toggle the display of elements + + This is a straight copy of the same file from static. + + Use `data-controls` and `data-expanded` to indicate the + starting state and the IDs of the elements that the toggle + controls. This is synonymous with ARIA attributes, which + get added when starting. +*/ + +window.GOVUK.Modules = window.GOVUK.Modules || {}; + +(function (Modules) { + 'use strict' + + Modules.GemToggle = function () { + this.start = function ($el) { + var toggleSelector = '[data-controls][data-expanded]' + + $el.on('click', toggleSelector, toggle) + $el.find(toggleSelector).each(addAriaAttrs) + + // Add the ARIA attributes with JavaScript + // If the JS fails and there's no interactive elements, having + // no aria attributes is an accurate representation. + function addAriaAttrs () { + var $toggle = $(this) + $toggle.attr('role', 'button') + $toggle.attr('aria-controls', $toggle.data('controls')) + $toggle.attr('aria-expanded', $toggle.data('expanded')) + + var $targets = getTargetElements($toggle) + $targets.attr('aria-live', 'polite') + $targets.attr('role', 'region') + $toggle.data('$targets', $targets) + } + + function toggle (event) { + var $toggle = $(event.target), + expanded = $toggle.attr('aria-expanded') === 'true', + $targets = $toggle.data('$targets') + + if (expanded) { + $toggle.attr('aria-expanded', false) + $targets.addClass('js-hidden') + } else { + $toggle.attr('aria-expanded', true) + $targets.removeClass('js-hidden') + } + + var toggledText = $toggle.data('toggled-text') + if (typeof toggledText === 'string') { + $toggle.data('toggled-text', $toggle.text()) + $toggle.text(toggledText) + } + + event.preventDefault() + } + + function getTargetElements ($toggle) { + var ids = $toggle.attr('aria-controls').split(' '), + selector = '#' + ids.join(', #') + + return $el.find(selector) + } + } + } +})(window.GOVUK.Modules) diff --git a/app/assets/stylesheets/govuk_publishing_components/components/_subscription-links.scss b/app/assets/stylesheets/govuk_publishing_components/components/_subscription-links.scss index 542a0f36e6..6fc881445a 100644 --- a/app/assets/stylesheets/govuk_publishing_components/components/_subscription-links.scss +++ b/app/assets/stylesheets/govuk_publishing_components/components/_subscription-links.scss @@ -27,6 +27,15 @@ .gem-c-subscription-links__link--feed { background-image: image-url("govuk_publishing_components/feed-icon-black.png"); + + // if this is a toggle, only show if js is enabled + &[data-controls] { + display: none; + + .js-enabled & { + display: block; + } + } } .gem-c-subscription-links__link--email-alerts { @@ -37,4 +46,15 @@ background-size: 20px 14px; } } + + .gem-c-subscription-links__feed-box { + padding: $gutter-half; + margin-bottom: $gutter-half; + background: $grey-3; + } + + .gem-c-subscription-links__feed-description { + font-weight: bold; + } } + diff --git a/app/views/govuk_publishing_components/components/_subscription-links.html.erb b/app/views/govuk_publishing_components/components/_subscription-links.html.erb index c319020d5c..951cffd2cb 100644 --- a/app/views/govuk_publishing_components/components/_subscription-links.html.erb +++ b/app/views/govuk_publishing_components/components/_subscription-links.html.erb @@ -1,21 +1,50 @@ <% email_signup_link ||= false + email_signup_link_text ||= t("govuk_component.subscription_links.email_signup_link_text", default: "Get email alerts") feed_link ||= false + feed_link_text ||= t("govuk_component.subscription_links.feed_link_text", default: "Subscribe to feed") + feed_link_box_value ||= false + feed_link_data = false + + if feed_link_box_value + feed_link = "#" + feed_link_data = { + controls: "feed-reader", + expanded: "false" + } + end %> -<% if email_signup_link || feed_link %> -