-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cookie banner and defer gtm load
- Loading branch information
1 parent
5b83938
commit afa916d
Showing
25 changed files
with
361 additions
and
28 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
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
41 changes: 41 additions & 0 deletions
41
packages/ui-react/src/components/CookieBanner/CookieBanner.module.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,41 @@ | ||
@use '@jwp/ott-ui-react/src/styles/theme'; | ||
@use '@jwp/ott-ui-react/src/styles/mixins/responsive'; | ||
|
||
.overlay { | ||
display: flex; | ||
align-items: flex-end; | ||
|
||
> div { | ||
width: 100%; | ||
color: var(--cookie-banner-color); | ||
background-color: var(--cookie-banner-background-color); | ||
} | ||
} | ||
|
||
.banner { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 16px; | ||
line-height: 1.5; | ||
|
||
a { | ||
color: inherit; | ||
} | ||
|
||
@include responsive.desktop-only() { | ||
max-width: 960px; | ||
margin: 0 auto; | ||
} | ||
} | ||
|
||
.buttonBar { | ||
display: flex; | ||
align-content: center; | ||
align-items: center; | ||
gap: 8px; | ||
|
||
@include responsive.mobile-only() { | ||
flex-direction: column; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/ui-react/src/components/CookieBanner/CookieBanner.test.tsx
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,13 @@ | ||
import React from 'react'; | ||
import { axe } from 'vitest-axe'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import CookieBanner from './CookieBanner'; | ||
|
||
describe('<CookieBanner>', () => { | ||
test('WCAG 2.2 (AA) compliant', async () => { | ||
const { container } = render(<CookieBanner onAccept={vi.fn()} onDecline={vi.fn()} />); | ||
|
||
expect(await axe(container, { runOnly: ['wcag21a', 'wcag21aa', 'wcag22aa'] })).toHaveNoViolations(); | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
packages/ui-react/src/components/CookieBanner/CookieBanner.tsx
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,83 @@ | ||
import React, { useEffect } from 'react'; | ||
import { Trans, useTranslation } from 'react-i18next'; | ||
import { useCookieConsent } from '@jwp/ott-ui-react/src/hooks/useCookieConsent'; | ||
import env from '@jwp/ott-common/src/env'; | ||
import { testId } from '@jwp/ott-common/src/utils/common'; | ||
|
||
import Button from '../Button/Button'; | ||
import Modal from '../Modal/Modal'; | ||
import Slide from '../Animation/Slide/Slide'; | ||
import createInjectableComponent from '../../modules/createInjectableComponent'; | ||
|
||
import styles from './CookieBanner.module.scss'; | ||
|
||
export const CookieBannerIdentifier = Symbol(`COOKIE_WALL`); | ||
|
||
export type Props = { | ||
onAccept?: () => void; | ||
onDecline?: () => void; | ||
onOpen?: () => void; | ||
onClose?: () => void; | ||
}; | ||
|
||
const CookieBanner: React.FC<Props> = ({ onAccept, onDecline, onOpen, onClose }) => { | ||
const { t } = useTranslation('cookiebanner'); | ||
const { isActive, consent, accept, decline } = useCookieConsent(); | ||
|
||
useEffect(() => { | ||
if (!isActive) return; | ||
if (!consent) { | ||
onOpen?.(); | ||
} else { | ||
onClose?.(); | ||
} | ||
}, [consent, isActive, onOpen, onClose]); | ||
|
||
const handleAcceptClick = () => { | ||
accept(); | ||
onAccept?.(); | ||
}; | ||
|
||
const handleDeclineClick = () => { | ||
decline(); | ||
onDecline?.(); | ||
}; | ||
|
||
if (!isActive || consent !== null) { | ||
return null; | ||
} | ||
|
||
// Multiple links are supported, separated by a pipe character. Usage for `consent_text`: "Read our <0>privacy policy</0> and <1>cookie policy</1>" | ||
const linkComponents = (env.APP_CONSENT_COOKIE_POLICY_URL || '#').split('|').map((link) => <a key={link} href={link} />); | ||
|
||
return ( | ||
<Modal | ||
open={true} | ||
AnimationComponent={Slide} | ||
className={styles.overlay} | ||
role="alertdialog" | ||
aria-labelledby="cookie-banner-title" | ||
aria-describedby="cookie-banner-description" | ||
> | ||
<div className={styles.banner}> | ||
<h1 className="hidden" id="cookie-banner-title"> | ||
{t('title')} | ||
</h1> | ||
<p id="cookie-banner-description"> | ||
<Trans | ||
t={t} | ||
components={linkComponents} | ||
i18nKey="consent_text" | ||
defaults="We use cookies to improve your experience. Do you accept the use of cookies?" | ||
/> | ||
</p> | ||
<div className={styles.buttonBar}> | ||
<Button label={t('accept')} onClick={handleAcceptClick} color="primary" data-testid={testId('accept')} /> | ||
<Button label={t('decline')} onClick={handleDeclineClick} data-testid={testId('decline')} /> | ||
</div> | ||
</div> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default createInjectableComponent(CookieBannerIdentifier, CookieBanner); |
18 changes: 17 additions & 1 deletion
18
packages/ui-react/src/components/RecaptchaField/RecaptchaField.module.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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
@use '@jwp/ott-ui-react/src/styles/mixins/responsive'; | ||
@use '@jwp/ott-ui-react/src/styles/theme'; | ||
|
||
.captcha { | ||
display: flex; | ||
justify-content: flex-end; | ||
width: 100%; | ||
margin-bottom: 8px; | ||
} | ||
} | ||
|
||
:global(#grecaptcha-challenge-container > div) { | ||
@include responsive.tablet-and-larger() { | ||
top: 5% !important; | ||
left: 5% !important; | ||
|
||
> div:first-child { | ||
opacity: 0.5 !important; | ||
} | ||
|
||
:global(.g-recaptcha-bubble-arrow) { | ||
display: none; | ||
} | ||
} | ||
} |
22 changes: 18 additions & 4 deletions
22
packages/ui-react/src/components/RecaptchaField/RecaptchaField.tsx
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
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,51 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import env from '@jwp/ott-common/src/env'; | ||
import { injectScript } from '@jwp/ott-ui-react/src/utils/dom'; | ||
import { setCookie, getCookie, removeCookie } from '@jwp/ott-ui-react/src/utils/cookies'; | ||
|
||
export type ConsentState = 'accepted' | 'declined'; | ||
|
||
export function useCookieConsent() { | ||
const cookieName = env.APP_CONSENT_COOKIE_NAME || 'cookies.consent'; | ||
const [isGTMLoaded, setIsGTMLoaded] = useState(false); | ||
const [consent, setConsent] = useState<ConsentState | null>(() => { | ||
const value = getCookie(cookieName); | ||
if (value === null) return null; | ||
return value === 'true' ? 'accepted' : 'declined'; | ||
}); | ||
|
||
const accept = useCallback(() => { | ||
setCookie(cookieName, 'true', 365); | ||
setConsent('accepted'); | ||
}, [cookieName]); | ||
|
||
const decline = useCallback(() => { | ||
setCookie(cookieName, 'false', 365); | ||
setConsent('declined'); | ||
}, [cookieName]); | ||
|
||
const reset = useCallback(() => { | ||
removeCookie(cookieName); | ||
setConsent(null); | ||
}, [cookieName]); | ||
|
||
useEffect(() => { | ||
// Wait for consent before loading GTM | ||
if (!env.APP_GTM_LOAD_ON_ACCEPT) return; | ||
if (consent === 'accepted' && !isGTMLoaded && env.APP_GTM_SCRIPT) { | ||
injectScript(env.APP_GTM_SCRIPT); | ||
setIsGTMLoaded(true); | ||
} | ||
}, [consent, isGTMLoaded]); | ||
|
||
return { | ||
isActive: !!env.APP_GTM_TAG_ID, | ||
isGTMLoaded, | ||
consent, | ||
reset, | ||
accept, | ||
decline, | ||
}; | ||
} | ||
|
||
export default useCookieConsent; |
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
Oops, something went wrong.