-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cookie banner component based on govuk-template
- Loading branch information
Showing
8 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,5 +334,8 @@ DEPENDENCIES | |
webmock (~> 3.5.0) | ||
yard | ||
|
||
RUBY VERSION | ||
ruby 2.6.1p33 | ||
|
||
BUNDLED WITH | ||
1.17.3 |
47 changes: 47 additions & 0 deletions
47
app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
function CookieBanner () { } | ||
|
||
CookieBanner.prototype.start = function ($module) { | ||
this.$module = $module[0] | ||
|
||
this.$module.hideCookieMessage = this.hideCookieMessage.bind(this) | ||
this.$hideLinks = this.$module.querySelector('a[data-hide-cookie-banner]') | ||
if (this.$hideLinks) { | ||
this.$hideLinks.addEventListener('click', this.$module.hideCookieMessage) | ||
} | ||
|
||
this.showCookieMessage() | ||
} | ||
|
||
CookieBanner.prototype.showCookieMessage = function () { | ||
var hasCookieMessage = (this.$module && window.GOVUK.cookie('seen_cookie_message') !== 'true') | ||
|
||
if (hasCookieMessage) { | ||
this.$module.style.display = 'block' | ||
document.addEventListener('DOMContentLoaded', function (event) { | ||
if (window.GOVUK.analytics && typeof window.GOVUK.analytics.trackEvent === 'function') { | ||
window.GOVUK.analytics.trackEvent('cookieBanner', 'Cookie banner shown', { | ||
value: 1, | ||
nonInteraction: true | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
CookieBanner.prototype.hideCookieMessage = function (event) { | ||
if (this.$module) { | ||
this.$module.style.display = 'none' | ||
window.GOVUK.cookie('seen_cookie_message', 'true', { days: 365 }) | ||
} | ||
|
||
if (event.target) { | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
Modules.CookieBanner = CookieBanner | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/assets/stylesheets/govuk_publishing_components/components/_cookie-banner.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
$govuk-cookie-banner-background: #d5e8f3; | ||
|
||
.js-enabled { | ||
.gem-c-cookie-banner { | ||
display: none; // shown with JS, always on for non-JS | ||
} | ||
} | ||
|
||
.gem-c-cookie-banner { | ||
@include govuk-font($size: 16); | ||
padding: 10px 0; | ||
background-color: $govuk-cookie-banner-background; | ||
} | ||
|
||
.gem-c-cookie-banner__message { | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
} |
9 changes: 9 additions & 0 deletions
9
app/views/govuk_publishing_components/components/_cookie_banner.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% | ||
id ||= 'global-cookie-message' | ||
message ||= capture do %> | ||
GOV.UK uses cookies to make the site simpler. <a class="govuk-link" href="https://www.gov.uk/help/cookies" data-module="track-click" data-track-category="cookieBanner" data-track-action="Cookie banner clicked">Find out more about cookies</a> or <a class="govuk-link" href="#" data-hide-cookie-banner="true" data-module="track-click" data-track-category="cookieBanner" data-track-action="Cookie banner hidden">hide this message</a> | ||
<% end %> | ||
|
||
<div id="<%= id %>" class="gem-c-cookie-banner" data-module="cookie-banner"> | ||
<p class="gem-c-cookie-banner__message govuk-width-container"><%= message %></p> | ||
</div> |
20 changes: 20 additions & 0 deletions
20
app/views/govuk_publishing_components/components/docs/cookie_banner.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Cookie banner | ||
description: Help users manage their personal data by telling them when you store cookies on their device. | ||
body: | | ||
Setting `data-hide-cookie-banner="true"` on any link inside the banner will overwrite the default action and when clicked will dismiss the cookie banner for a period of 365 days (approx. 1 year). | ||
accessibility_criteria: | | ||
Text in the cookie banner must be clear and unambiguous and should provide a way to dismiss the message. | ||
Links in the component must: | ||
- accept focus | ||
- be focusable with a keyboard | ||
- indicate when they have focus | ||
examples: | ||
default: | ||
data: | ||
id: default | ||
custom_message: | ||
data: | ||
id: custom-message | ||
message: GOV.UK uses cookies to make the site simpler. <a class="govuk-link" href="https://www.gov.uk/help/cookies">Find out more about cookies</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rails_helper' | ||
|
||
describe "Cookie banner", type: :view do | ||
def component_name | ||
"cookie_banner" | ||
end | ||
|
||
it "renders with default values" do | ||
render_component({}) | ||
assert_select '.gem-c-cookie-banner[id="global-cookie-message"][data-module="cookie-banner"]' | ||
assert_select '.gem-c-cookie-banner__message.govuk-width-container', text: "GOV.UK uses cookies to make the site simpler. Find out more about cookies or hide this message" | ||
assert_select 'a[data-hide-cookie-banner="true"]' | ||
end | ||
|
||
it "renders with custom values" do | ||
render_component(id: 'custom-cookie-message', message: "Custom message") | ||
assert_select '.gem-c-cookie-banner[id="custom-cookie-message"][data-module="cookie-banner"]' | ||
assert_select '.gem-c-cookie-banner__message.govuk-width-container', text: "Custom message" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-env jasmine, jquery */ | ||
/* global GOVUK */ | ||
|
||
describe('Cookie banner component', function () { | ||
'use strict' | ||
|
||
var container | ||
|
||
beforeEach(function () { | ||
container = document.createElement('div') | ||
container.innerHTML = | ||
'<div id="global-cookie-message" class="gem-c-cookie-banner" data-module="cookie-banner">' + | ||
'<p class="gem-c-cookie-banner__message govuk-width-container">' + | ||
'<a class="govuk-link" href="https://www.gov.uk/help/cookies">Find out more about cookies</a> or <a class="govuk-link" href="#" data-hide-cookie-banner="true">hide this message</a>' + | ||
'</p>' + | ||
'</div>' | ||
|
||
document.body.appendChild(container) | ||
var element = document.querySelector('[data-module="cookie-banner"]') | ||
|
||
window.GOVUK.cookie('seen_cookie_message', null) | ||
|
||
new GOVUK.Modules.CookieBanner().start($(element)) | ||
}) | ||
|
||
afterEach(function () { | ||
document.body.removeChild(container) | ||
window.GOVUK.cookie('seen_cookie_message', null) | ||
}) | ||
|
||
it('should show the cookie banner', function () { | ||
var banner = document.querySelector('[data-module="cookie-banner"]') | ||
expect(banner).toBeVisible() | ||
}) | ||
|
||
it('should hide the cookie banner when pressing the link', function () { | ||
var banner = document.querySelector('[data-module="cookie-banner"]') | ||
var link = document.querySelector('[data-hide-cookie-banner="true"]') | ||
link.click() | ||
expect(banner).toBeHidden() | ||
}) | ||
}) |