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 4 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
50 changes: 50 additions & 0 deletions src/components/CookieModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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">
By clicking &quot;Accept&quot;, you agree to the storing of
cookies on your device to enhance site navigation,
improve your experience, and assist in our marketing
efforts. To learn more, read our Privacy Policy.
</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 border-[#7147E8] rounded-3xl py-2 px-8 text-sm"
onClick={() => handleModalClose('Decline')}
>
Decline
</button>
</div>
</div>
)
}

export default CookieModal
25 changes: 23 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,28 @@ 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);

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

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

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

return (
<>
{/* Google Tag Manager */}
{storedCookie === 'Accept' && (
<>
{/* Google Tag Manager */}
<Head>
{/* eslint-disable-next-line @next/next/next-script-for-ga */}
<script
Expand Down Expand Up @@ -74,6 +92,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;
}