Skip to content

Commit

Permalink
Cookie consent - updates documentation config, cleans up js
Browse files Browse the repository at this point in the history
  • Loading branch information
oisa committed Dec 11, 2024
1 parent 04687df commit 166de60
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 36 deletions.
28 changes: 2 additions & 26 deletions src/components/cookie-consent/_cookie-consent.hbs
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
{{#if-equals type "banner"}}
{{!-- <div class="nsw-cookie-banner js-cookie-banner" role="alert">
<div class="nsw-cookie-banner__wrapper {{#if-equals type "auto opt in"}}js-cookie-banner-auto-opt-in{{else}}js-cookie-banner-opt-in{{/if-equals}}">
<span class="nsw-cookie-banner__main-message">
<div class="nsw-cookie-banner__content">
<div class="nsw-cookie-banner__title">{{title}}</div>
<p>{{{content}}}</p>
</div>
<div class="nsw-cookie-banner__buttons-container">
<button class="nsw-button nsw-button--dark js-cookie-banner-accept">{{#if ctaAccept}}{{ctaAccept}}{{else}}Accept all cookies{{/if}}</button>
{{#if ctaReject}}<button href="{{ctaHref}}" class="nsw-button nsw-button--dark js-cookie-banner-reject">{{#if ctaReject}}{{ctaReject}}{{else}}Reject cookies{{/if}}</button>{{/if}}
<a href="{{urlManageCookies}}" class="nsw-button nsw-button--dark-outline">Manage your cookies</a>
</div>
</span>
{{#if confirmationMessage}}
<span class="nsw-cookie-banner__confirmation-message" hidden="true">
<div class="nsw-cookie-banner__content">
<p>{{{confirmationMessage}}}</p>
</div>
<div class="nsw-cookie-banner__buttons-container">
<button class="nsw-button nsw-button--dark js-cookie-banner-dismiss">Hide this message</button>
</div>
</span>
{{/if}}
</div>
</div> --}}
<a href="#" class="js-open-banner-cookie-consent" aria-haspopup="dialog">Trigger consent banner</a>
{{/if-equals}}

{{#if-equals type "dialog"}}
<a href="#" class="js-open-dialog-cookie-consent" aria-haspopup="dialog">Cookie preferences</a>
<a href="#" class="js-open-dialog-cookie-consent-preferences" aria-haspopup="dialog">Trigger cookie preferences</a>
{{/if-equals}}
4 changes: 4 additions & 0 deletions src/components/cookie-consent/blank.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ model:
cookie-consent-4: json/cookie-consent-4.json
---

<p>
{{>_cookie-consent model.cookie-consent-1 type="banner"}}
</p>
<p>
{{>_cookie-consent model.cookie-consent-4 type="dialog"}}
</p>
2 changes: 1 addition & 1 deletion src/components/cookie-consent/cookie-consent-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ waitForCookieConsent(() => {
acceptAllBtn: 'Accept all',
acceptNecessaryBtn: 'Reject all',
showPreferencesBtn: 'Manage your preferences',
confirmationMessage: 'Thanks for making your selection. View and update your cookie preferences <a href="#cookie-consent" class="js-open-dialog-cookie-consent" aria-haspopup="dialog">here</a>.',
confirmationMessage: 'Thanks for making your selection. View and update your cookie preferences <a href="#cookie-consent" class="js-open-dialog-cookie-consent-preferences" aria-haspopup="dialog">here</a>.',

Check warning on line 30 in src/components/cookie-consent/cookie-consent-config.js

View workflow job for this annotation

GitHub Actions / build-staging

This line has a length of 218. Maximum allowed is 130
},
preferencesModal: {
title: 'Manage cookie preferences',
Expand Down
47 changes: 39 additions & 8 deletions src/components/cookie-consent/cookie-consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CookieConsent {

// Create the dialog dynamically
const preferencesDialogHtml = `
<div class="js-cookie-consent nsw-cookie-dialog nsw-dialog nsw-dialog--single-action js-dialog js-dialog-dismiss" id="cookie-consent" role="dialog" aria-labelledby="cookie-consent-dialog">
<div class="js-cookie-consent nsw-cookie-dialog nsw-dialog nsw-dialog--single-action js-dialog js-dialog-dismiss" id="cookie-consent-preferences" role="dialog" aria-labelledby="cookie-consent-dialog">

Check warning on line 57 in src/components/cookie-consent/cookie-consent.js

View workflow job for this annotation

GitHub Actions / build-staging

This line has a length of 206. Maximum allowed is 130
<div class="nsw-dialog__wrapper">
<div class="nsw-dialog__container">
<div class="nsw-dialog__top">
Expand Down Expand Up @@ -97,7 +97,7 @@ class CookieConsent {
`

// Append to the body
const dialogContainer = document.querySelector('.js-open-dialog-cookie-consent')
const dialogContainer = document.querySelector('.js-open-dialog-cookie-consent-preferences')

// Initialise dialog
if (dialogContainer) {
Expand Down Expand Up @@ -141,7 +141,7 @@ class CookieConsent {
<div class="nsw-cookie-banner__buttons-container">
${consentModal.acceptAllBtn ? `<button class="nsw-button nsw-button--dark js-close-dialog ${!consentModal.confirmationMessage ? 'js-dismiss-cookie-banner' : ''}" data-role="accept-all">${consentModal.acceptAllBtn}</button>` : ''}
${consentModal.acceptNecessaryBtn ? `<button class="nsw-button nsw-button--dark js-cookie-banner-reject ${!consentModal.confirmationMessage ? 'js-dismiss-cookie-banner' : ''}" data-role="reject-all">${consentModal.acceptNecessaryBtn}</button>` : ''}
<a href="#cookie-consent" class="nsw-button nsw-button--dark-outline js-open-dialog-cookie-consent" aria-haspopup="dialog">${consentModal.showPreferencesBtn || 'Manage your cookies'}</a>
<a href="#cookie-consent" class="nsw-button nsw-button--dark-outline js-open-dialog-cookie-consent-preferences" aria-haspopup="dialog">${consentModal.showPreferencesBtn || 'Manage your cookies'}</a>
</div>
</span>
<span class="nsw-cookie-banner__confirmation-message" hidden="true">
Expand Down Expand Up @@ -171,11 +171,10 @@ class CookieConsent {
this.attachEventListeners()

// Immediately hide the banner if user has preferences set
// const preferences = CookieConsentAPI.getUserPreferences()
// if (preferences && preferences.acceptedCategories.length > 0) {
// console.log('User has already made a selection')
// this.consentBannerElement.setAttribute('hidden', 'true')
// }
const preferences = CookieConsentAPI.getUserPreferences()
if (preferences && preferences.acceptedCategories.length > 0) {
this.consentBannerElement.setAttribute('hidden', 'true')
}
} else {
console.error('Banner element not created')
}
Expand Down Expand Up @@ -218,6 +217,17 @@ class CookieConsent {
if (target.matches('.js-dismiss-cookie-banner')) {
this.hideConsentBanner()
}

// Manual trigger of cookie consent banner
if (target.matches('.js-open-banner-cookie-consent')) {
event.preventDefault()
this.showConsentBanner()
}

// Manual trigger of cookie consent preferences dialog
if (target.matches('.js-open-dialog-cookie-consent-preferences')) {
event.preventDefault()
}
})
}

Expand Down Expand Up @@ -291,6 +301,27 @@ class CookieConsent {
}
}

showConsentBanner() {
if (this.consentBannerElement) {
const description = this.consentBannerElement.querySelector('.nsw-cookie-banner__description')
const confirmationMessage = this.consentBannerElement.querySelector('.nsw-cookie-banner__confirmation-message')

if (this.consentBannerConfirmationMessage && confirmationMessage) {
// Hide the confirmation message if it's present
confirmationMessage.setAttribute('hidden', 'true')
}

if (description) {
// Show the main description
description.removeAttribute('hidden')
}

this.consentBannerElement.removeAttribute('hidden')
} else {
console.warn('Consent banner element not found.')
}
}

hideConsentBanner() {
if (this.consentBannerElement) {
// Add a hidden attribute for transition
Expand Down
4 changes: 4 additions & 0 deletions src/components/cookie-consent/theme.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ model:
---

{{#>_theme}}
<p>
{{>_cookie-consent model.cookie-consent-1 type="banner"}}
</p>
<p>
{{>_cookie-consent model.cookie-consent-4 type="dialog"}}
</p>
{{/_theme}}
2 changes: 1 addition & 1 deletion src/global/handlebars/layouts/layout.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
acceptAllBtn: 'Accept all',
acceptNecessaryBtn: 'Reject all',
showPreferencesBtn: 'Manage your preferences',
confirmationMessage: `Thanks for making your selection. View and update your cookie preferences <a href="#cookie-consent" class="js-open-dialog-cookie-consent" aria-haspopup="dialog">here</a>.`
confirmationMessage: `Thanks for making your selection. View and update your cookie preferences <a href="#cookie-consent" class="js-open-dialog-cookie-consent-preferences" aria-haspopup="dialog">here</a>.`
},
preferencesModal: {
title: 'Manage cookie preferences',
Expand Down

0 comments on commit 166de60

Please sign in to comment.