-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: change from delayed
flow to include consent
#376
Draft
davidnuescheler
wants to merge
5
commits into
main
Choose a base branch
from
marketing-tech
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
58d788a
chore: change initial flow
davidnuescheler 88d66bc
Merge branch 'main' into marketing-tech
davidnuescheler c2efe45
chore: refactor of consent based loading
davidnuescheler 84563a8
chore: refactor number 2
davidnuescheler aaafd81
chore: focus cleanup
davidnuescheler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,79 @@ | ||
/** | ||
* Handles consent, including the loading of consented.js | ||
* where needed. | ||
* | ||
* Dispatches `consent` event on document on change | ||
*/ | ||
|
||
// eslint-disable-next-line import/no-cycle | ||
import { loadFragment } from '../blocks/fragment/fragment.js'; | ||
|
||
/* create button helper */ | ||
function createButton(text, clickHandler, style, label) { | ||
const button = document.createElement('button'); | ||
button.textContent = text; | ||
if (style) button.className = style; | ||
if (label) button.ariaLabel = label; | ||
button.addEventListener('click', clickHandler); | ||
return button; | ||
} | ||
|
||
/* display consent banner */ | ||
export async function displayConsentBanner() { | ||
const consentBanner = (await loadFragment('/drafts/uncled/consent-banner')).firstElementChild; | ||
|
||
const bannerClose = new Promise((resolve) => { | ||
const dialog = document.createElement('dialog'); | ||
|
||
const storeAndClose = (status) => { | ||
localStorage.setItem('consentStatus', status); | ||
dialog.remove(); | ||
resolve(status); | ||
document.dispatchEvent(new CustomEvent('consent', { detail: { consentStatus: status } })); | ||
const loc = window.location; | ||
if (loc.hash === '#consent') window.history.pushState({}, null, `${loc.pathname}${loc.search}`); | ||
}; | ||
|
||
const close = () => { storeAndClose('declineAll'); }; | ||
const accept = () => { storeAndClose('acceptAll'); }; | ||
const decline = () => { storeAndClose('declineAll'); }; | ||
|
||
const config = consentBanner.dataset; | ||
dialog.id = 'consent-banner'; | ||
dialog.append(consentBanner); | ||
const buttons = document.createElement('span'); | ||
buttons.className = 'consent-banner-buttons'; | ||
if (config.accept) buttons.append(createButton(config.accept, accept)); | ||
if (config.decline) buttons.append(createButton(config.decline, decline, 'secondary')); | ||
if (config.close) buttons.append(createButton('\u2715', close, 'consent-banner-close', config.close)); | ||
dialog.append(buttons); | ||
document.body.append(dialog); | ||
dialog.show(); | ||
}); | ||
|
||
return bannerClose; | ||
} | ||
|
||
/* main consent handler */ | ||
export default async function handleConsent() { | ||
const mapStatus = (consentStatus) => { | ||
if (consentStatus === 'declineAll') return false; | ||
return true; | ||
}; | ||
|
||
document.addEventListener('consent', (e) => { | ||
if (mapStatus(e.detail.consentStatus)) import('./consented.js'); | ||
}); | ||
|
||
const consentStatus = localStorage.getItem('consentStatus'); | ||
|
||
if (consentStatus === null) { | ||
return mapStatus(await displayConsentBanner()); | ||
} | ||
|
||
window.addEventListener('hashchange', (e) => { | ||
if (new URL(e.newURL).hash === '#consent') displayConsentBanner(); | ||
}); | ||
|
||
return mapStatus(consentStatus); | ||
} |
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,5 @@ | ||
// add tag management code here, marketing or advertising functionality | ||
// this file is gated by consent | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('loading consented.js'); |
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1 +1,42 @@ | ||
/* add global styles that can be loaded post LCP here */ | ||
|
||
dialog#consent-banner { | ||
position: fixed; | ||
left: 0; | ||
bottom: 0; | ||
background-color: var(--light-color); | ||
max-width: 600px; | ||
padding: 16px; | ||
padding-right: 40px; | ||
font-size: var(--body-font-size-xs); | ||
margin: 16px; | ||
border-radius: 16px; | ||
border: 0; | ||
box-shadow: 3px 3px 10px #0004; | ||
} | ||
|
||
dialog#consent-banner p { | ||
margin: 0px; | ||
} | ||
|
||
dialog#consent-banner .consent-banner-buttons { | ||
display: flex; | ||
gap: 8px; | ||
} | ||
|
||
dialog#consent-banner .consent-banner-buttons button { | ||
margin: 8px 0; | ||
} | ||
|
||
dialog#consent-banner .consent-banner-close { | ||
position: absolute; | ||
margin: 0; | ||
padding: 0; | ||
right: 16px; | ||
top: 12px; | ||
border: unset; | ||
padding: unset; | ||
color: unset; | ||
background-color: unset; | ||
margin: 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return consentStatus !== 'declineAll';
? Or is that unclear?