Skip to content
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

Add cookie banner #193

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/components/CookieModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useCallback, useEffect, useState } from "react";

const CookieModal = ({ handleButtonClick }: { handleButtonClick: (choice: string) => void }) => {
const handleModalClose = (choice: string) => {
const modalElement = document.querySelector(".cookie-modal");
if (modalElement) {
modalElement.classList.remove("visible");
setTimeout(() => handleButtonClick(choice), 1200);
}
};

useEffect(() => {
const modalElement = document.querySelector(".cookie-modal");
const timer = setTimeout(() => {
if (modalElement) {
modalElement.classList.add("visible");
}
}, 2000);

return () => clearTimeout(timer);
}, []);

return (
<div className="fixed bottom-0 bg-[#F8FBFC] z-[20] p-6 shadow-shadow card sm:m-5 sm:max-w-xl lg:max-w-3xl cookie-modal">
<p className="text-[#292929] font-light">
We use cookies to improve site navigation, analyze site usage, and enhance your user experience. Click &quot;Accept&quot; to enable cookies or &quot;Reject&quot; to reject cookies.
</p>

<div className="mt-8 flex gap-3 justify-end">
<button
className="text-white bg-[#7147E8] rounded-3xl py-2 px-8 text-sm"
onClick={() => handleModalClose('Accept')}
>
Accept
</button>
<button
className="text-[#7147E8] bg-white border-[1px] border-[#7147E8] rounded-3xl py-2 px-8 text-sm"
onClick={() => handleModalClose('Decline')}
>
Reject
</button>
</div>
</div>
)
}

export default CookieModal
39 changes: 37 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '../styles/styles.css';
import '../styles/globals.css';
import '../styles/cve-style.css';
import '../styles/modal.css';
import type { AppProps } from 'next/app';
import '@fortawesome/fontawesome-svg-core/styles.css';
import '@fontsource/metropolis';
Expand All @@ -11,13 +12,42 @@ import '@fontsource/metropolis/700.css';
import { config } from '@fortawesome/fontawesome-svg-core';
import AOS from 'aos';
import 'aos/dist/aos.css';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import Script from 'next/script';
import Head from 'next/head';
import CookieModal from '@/components/CookieModal';

config.autoAddCss = false;

export default function App({ Component, pageProps }: AppProps) {
const [storedCookie, setStoredCookie] = useState<string | null>(null);

const handleButtonClick = (choice: string) => {
localStorage.setItem("userCookie", choice);
setStoredCookie(choice);
};

useEffect(() => {
if (typeof window !== "undefined") {
const userCookie = window.localStorage.getItem("userCookie");
setStoredCookie(userCookie);
}
}, []);

useEffect(() => {
if (storedCookie === "Decline") {
const scriptTags = document.querySelectorAll(
'script[src*="googletagmanager"], script#gtag-init, script#tag-manager'
);
scriptTags.forEach((tag) => tag.remove());

const iframes = document.querySelectorAll(
'iframe[src*="googletagmanager"]'
);
iframes.forEach((iframe) => iframe.remove());
}
}, [storedCookie]);

useEffect(() => {
AOS.init({
offset: 80,
Expand All @@ -27,7 +57,9 @@ export default function App({ Component, pageProps }: AppProps) {

return (
<>
{/* Google Tag Manager */}
{(!storedCookie || storedCookie === "Accept") && (
<>
{/* Google Tag Manager */}
<Head>
{/* eslint-disable-next-line @next/next/next-script-for-ga */}
<script
Expand Down Expand Up @@ -74,6 +106,9 @@ export default function App({ Component, pageProps }: AppProps) {
id="gtag-init"
strategy="afterInteractive"
/>
</>
)}
{!storedCookie && <CookieModal handleButtonClick={handleButtonClick} />}
<Component {...pageProps} />
</>
);
Expand Down
10 changes: 10 additions & 0 deletions src/styles/modal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.cookie-modal {
transform: translateY(100%);
transition: transform 1.2s ease-in-out, visibility 1.2s ease-in-out;
visibility: hidden;
}

.cookie-modal.visible {
transform: translateY(0);
visibility: visible;
}