diff --git a/CHANGELOG.md b/CHANGELOG.md index 4251edd708..0ac1391b8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ useful summary for people upgrading their application, not a replication of the commit log. +## Unreleased + +* Update cookies banner to align it with govuk-frontend ([PR #1918](https://github.com/alphagov/govuk_publishing_components/pull/1918)) + ## 24.1.1 * Fix deprecation warnings when running tests ([PR #1899](https://github.com/alphagov/govuk_publishing_components/pull/1899)) diff --git a/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js b/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js index d50f05cd94..63a952ce3a 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js +++ b/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js @@ -9,10 +9,11 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; this.$module.hideCookieMessage = this.hideCookieMessage.bind(this) this.$module.showConfirmationMessage = this.showConfirmationMessage.bind(this) this.$module.setCookieConsent = this.setCookieConsent.bind(this) + this.$module.rejectCookieConsent = this.rejectCookieConsent.bind(this) this.$module.cookieBanner = document.querySelector('.gem-c-cookie-banner') this.$module.cookieBannerConfirmationMessage = this.$module.querySelector('.gem-c-cookie-banner__confirmation') - + this.$module.cookieBannerConfirmationMessageText = this.$module.querySelector('.gem-c-cookie-banner__confirmation-message') this.setupCookieMessage() } @@ -24,9 +25,16 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; } } - this.$acceptCookiesLink = this.$module.querySelector('button[data-accept-cookies]') - if (this.$acceptCookiesLink) { - this.$acceptCookiesLink.addEventListener('click', this.$module.setCookieConsent) + this.$acceptCookiesButton = this.$module.querySelector('button[data-accept-cookies]') + if (this.$acceptCookiesButton) { + this.$acceptCookiesButton.style.display = 'block' + this.$acceptCookiesButton.addEventListener('click', this.$module.setCookieConsent) + } + + this.$rejectCookiesButton = this.$module.querySelector('button[data-reject-cookies]') + if (this.$rejectCookiesButton) { + this.$rejectCookiesButton.style.display = 'block' + this.$rejectCookiesButton.addEventListener('click', this.$module.rejectCookieConsent) } this.showCookieMessage() @@ -56,6 +64,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; CookieBanner.prototype.hideCookieMessage = function (event) { if (this.$module) { + this.$module.hidden = true this.$module.style.display = 'none' window.GOVUK.cookie('cookies_preferences_set', 'true', { days: 365 }) } @@ -66,6 +75,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; } CookieBanner.prototype.setCookieConsent = function () { + if (this.$acceptCookiesButton.getAttribute('data-cookie-types') === 'all') { + this.$module.cookieBannerConfirmationMessageText.insertAdjacentHTML('afterbegin', 'You have accepted additional cookies. ') + } window.GOVUK.approveAllCookieTypes() this.$module.showConfirmationMessage() this.$module.cookieBannerConfirmationMessage.focus() @@ -78,11 +90,19 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; } } + CookieBanner.prototype.rejectCookieConsent = function () { + this.$module.cookieBannerConfirmationMessageText.insertAdjacentHTML('afterbegin', 'You have rejected additional cookies. ') + this.$module.showConfirmationMessage() + this.$module.cookieBannerConfirmationMessage.focus() + window.GOVUK.cookie('cookies_preferences_set', 'true', { days: 365 }) + } + CookieBanner.prototype.showConfirmationMessage = function () { - this.$cookieBannerMainContent = document.querySelector('.gem-c-cookie-banner__wrapper') + this.$cookieBannerMainContent = document.querySelector('.js-banner-wrapper') - this.$cookieBannerMainContent.style.display = 'none' + this.$cookieBannerMainContent.hidden = true this.$module.cookieBannerConfirmationMessage.style.display = 'block' + this.$module.cookieBannerConfirmationMessage.hidden = false } CookieBanner.prototype.isInCookiesPage = function () { diff --git a/app/assets/stylesheets/govuk_publishing_components/components/_cookie-banner.scss b/app/assets/stylesheets/govuk_publishing_components/components/_cookie-banner.scss index ab970e2e25..1d8718e247 100644 --- a/app/assets/stylesheets/govuk_publishing_components/components/_cookie-banner.scss +++ b/app/assets/stylesheets/govuk_publishing_components/components/_cookie-banner.scss @@ -1,3 +1,4 @@ +@import "govuk/components/cookie-banner/cookie-banner"; $govuk-cookie-banner-background: govuk-colour("light-grey", "grey-4"); .js-enabled { @@ -7,50 +8,14 @@ $govuk-cookie-banner-background: govuk-colour("light-grey", "grey-4"); } .gem-c-cookie-banner { - @include govuk-font($size: 16); - padding: govuk-spacing(2) 0; background-color: $govuk-cookie-banner-background; } -.gem-c-cookie-banner--services { +// can't be used without js so implement there +.gem-c-cookie-banner .gem-c-button { display: none; } -.gem-c-cookie-banner__message { - display: inline-block; - padding-bottom: govuk-spacing(2); - - @include govuk-font($size: 16); - @include govuk-media-query($from: desktop) { - padding-right: govuk-spacing(4); - } -} - -.gem-c-cookie-banner__button { - &.govuk-grid-column-one-half-from-desktop { - padding: 0; - } - - .govuk-button { - @include govuk-media-query($from: desktop) { - width: 90%; - } - - @include govuk-media-query($until: desktop) { - margin-bottom: govuk-spacing(4); - } - } -} - -// Only show accept button if users have js and can accept -.gem-c-cookie-banner__button-accept { - display: none; -} - -.js-enabled .gem-c-cookie-banner__button-accept { - display: inline-block; -} - .gem-c-cookie-banner__confirmation { display: none; position: relative; @@ -81,51 +46,11 @@ $govuk-cookie-banner-background: govuk-colour("light-grey", "grey-4"); } } -.gem-c-cookie-banner__hide-button { - @include govuk-font($size: 19); - outline: 0; - border: 0; - background: none; - text-decoration: underline; - color: $govuk-link-colour; - padding: govuk-spacing(0); - margin-top: govuk-spacing(2); - - &:hover { - color: $govuk-link-hover-colour; - cursor: pointer; - } - - &:focus { - @include govuk-focused-text; - } - - @include govuk-media-query($from: desktop) { - margin-top: govuk-spacing(0); - position: absolute; - right: govuk-spacing(1); - } -} - -.gem-c-cookie-banner__buttons--flex { - display: flex; - flex-wrap: wrap; - align-items: baseline; - - .govuk-button, - .gem-c-cookie-banner__link { - flex-grow: 1; - flex-basis: 10rem; - margin-right: govuk-spacing(3); - margin-bottom: govuk-spacing(3); - } -} - // Override the styles from govuk_template // stylelint-disable selector-max-id .gem-c-cookie-banner#global-cookie-message { background-color: $govuk-cookie-banner-background; - padding: govuk-spacing(4) 0; + padding: 0; box-sizing: border-box; .gem-c-cookie-banner__message, @@ -135,18 +60,13 @@ $govuk-cookie-banner-background: govuk-colour("light-grey", "grey-4"); @include govuk-font($size: 19); } - .gem-c-cookie-banner__message { - margin-bottom: 0; - } - p { @include govuk-font($size: 19); margin: 0 0 govuk-spacing(2) 0; } - .gem-c-cookie-banner__confirmation-message { - @include govuk-media-query($from: desktop) { - margin-bottom: 0; - } + .gem-c-cookie-banner__message, + .gem-c-cookie-banner__confirmation { + margin-bottom: - govuk-spacing(2); } } diff --git a/app/views/govuk_publishing_components/components/_cookie_banner.html.erb b/app/views/govuk_publishing_components/components/_cookie_banner.html.erb index a796fff8fc..a35937b1fd 100644 --- a/app/views/govuk_publishing_components/components/_cookie_banner.html.erb +++ b/app/views/govuk_publishing_components/components/_cookie_banner.html.erb @@ -1,72 +1,83 @@ <% id ||= 'global-cookie-message' - title ||= "Tell us whether you accept cookies" - text ||= raw("We use cookies to collect information about how you use GOV.UK. We use this information to make the website work as well as possible and improve government services.") + title ||= "Cookies on GOV.UK" + text ||= ["We use some essential cookies to make this website work.", "We’d like to set additional cookies to understand how you use GOV.UK, remember your settings and improve government services.", "We also use cookies set by other sites to help us deliver content from their services."] + if text.kind_of?(Array) + newtext = "" + text.each do |t| + newtext += "

#{t}

" + end + text = newtext + else + text = "

#{text}

" + end + text = raw(text) + cookie_preferences_href ||= "/help/cookies" - confirmation_message ||= raw("You’ve accepted all cookies. You can change your cookie settings at any time.") + confirmation_message ||= raw("You can change your cookie settings at any time.") services_cookies ||= nil css_classes = %w(gem-c-cookie-banner govuk-clearfix) css_classes << "gem-c-cookie-banner--services" if services_cookies %> -
-