Skip to content

Commit 6239cea

Browse files
committed
Add VanillaCookieConsent
1 parent 4eee19d commit 6239cea

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

modules/CookieConsent/CookieConsent.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import type { Locale } from '@prezly/theme-kit-nextjs';
22

33
import { app } from '@/adapters/server';
44

5-
import { OneTrustCookie } from './components';
6-
import { CookieConsentBar } from './components/CookieConsentBar';
5+
import { OneTrustCookie, VanillaCookieConsent } from './components';
76

87
interface Props {
98
localeCode: Locale.Code;
109
}
1110

1211
export async function CookieConsent({ localeCode }: Props) {
12+
// TODO: remove or use language constant
13+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1314
const language = await app().languageOrDefault(localeCode);
1415
const { onetrust_cookie_consent: onetrust } = await app().newsroom();
1516

1617
if (onetrust.is_enabled) {
1718
return <OneTrustCookie script={onetrust.script} category={onetrust.category} />;
1819
}
1920

20-
return <CookieConsentBar>{language.company_information.cookie_statement}</CookieConsentBar>;
21+
return <VanillaCookieConsent />;
2122
}

modules/CookieConsent/CookieConsentContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use client';
22

3-
import type { ReactNode } from 'react';
3+
import type { Dispatch, ReactNode, SetStateAction } from 'react';
44
import { createContext, useContext, useState } from 'react';
55

66
import type { Consent } from './types';
77

88
interface Context {
99
consent: Consent | null;
10-
setConsent: (consent: Consent | null) => void;
10+
setConsent: Dispatch<SetStateAction<Consent | null>>;
1111
}
1212

1313
export const context = createContext<Context>({
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
import * as CookieConsent from 'vanilla-cookieconsent';
5+
import 'vanilla-cookieconsent/dist/cookieconsent.css';
6+
7+
import { useCookieConsent } from '../../CookieConsentContext';
8+
import { ConsentCategory } from '../../types';
9+
10+
export function VanillaCookieConsent() {
11+
const { setConsent } = useCookieConsent();
12+
13+
useEffect(() => {
14+
CookieConsent.run({
15+
autoShow: true,
16+
language: {
17+
default: 'en',
18+
translations: {
19+
en: {
20+
consentModal: {
21+
title: 'We use cookies',
22+
description: 'Cookie modal description',
23+
acceptAllBtn: 'Accept all',
24+
acceptNecessaryBtn: 'Reject all',
25+
showPreferencesBtn: 'Manage Individual preferences',
26+
},
27+
preferencesModal: {
28+
title: 'Manage cookie preferences',
29+
acceptAllBtn: 'Accept all',
30+
acceptNecessaryBtn: 'Reject all',
31+
savePreferencesBtn: 'Accept current selection',
32+
closeIconLabel: 'Close modal',
33+
sections: [
34+
{
35+
title: 'Somebody said ... cookies?',
36+
description: 'I want one!',
37+
},
38+
{
39+
title: 'Strictly Necessary cookies',
40+
description:
41+
'These cookies are essential for the proper functioning of the website and cannot be disabled.',
42+
43+
linkedCategory: 'necessary',
44+
},
45+
{
46+
title: 'Performance and Analytics',
47+
description:
48+
'These cookies collect information about how you use our website. All of the data is anonymized and cannot be used to identify you.',
49+
linkedCategory: 'analytics',
50+
},
51+
{
52+
title: 'More information',
53+
description:
54+
'For any queries in relation to my policy on cookies and your choices, please <a href="#contact-page">contact us</a>',
55+
},
56+
],
57+
},
58+
},
59+
},
60+
},
61+
categories: {
62+
[ConsentCategory.NECESSARY]: {
63+
readOnly: true,
64+
enabled: true,
65+
},
66+
[ConsentCategory.FIRST_PARTY_ANALYTICS]: {},
67+
[ConsentCategory.THIRD_PARTY_COOKIES]: {},
68+
},
69+
onConsent({ cookie }) {
70+
setConsent((current) => ({
71+
...current,
72+
categories: cookie.categories as ConsentCategory[],
73+
}));
74+
},
75+
onChange({ cookie }) {
76+
setConsent((current) => ({
77+
...current,
78+
categories: cookie.categories as ConsentCategory[],
79+
}));
80+
},
81+
});
82+
}, [setConsent]);
83+
84+
return null;
85+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { VanillaCookieConsent } from './VanillaCookieConsent';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { OneTrustCookie } from './OneTrustCookie';
2+
export { VanillaCookieConsent } from './VanillaCookieConsent';

0 commit comments

Comments
 (0)