Skip to content

Commit a20e48f

Browse files
committed
Cookie Banner homepage
1 parent 2820e9e commit a20e48f

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

front/pages/index.tsx

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636

3737
const defaultFlexClasses = "flex flex-col gap-4";
3838

39+
import { Transition } from "@headlessui/react";
40+
3941
import {
4042
SignInDropDownButton,
4143
SignUpDropDownButton,
@@ -90,6 +92,9 @@ export default function Home({
9092
const scrollRef3 = useRef<HTMLDivElement | null>(null);
9193
const scrollRef4 = useRef<HTMLDivElement | null>(null);
9294

95+
const [showCookieBanner, setShowCookieBanner] = useState<boolean>(true);
96+
const [hasAcceptedCookies, setHasAcceptedCookies] = useState<boolean>(false);
97+
9398
useEffect(() => {
9499
if (logoRef.current) {
95100
const logoPosition = logoRef.current.offsetTop;
@@ -752,21 +757,34 @@ export default function Home({
752757
</div>
753758

754759
<Footer />
755-
<>
756-
<Script
757-
src={`https://www.googletagmanager.com/gtag/js?id=${gaTrackingId}`}
758-
strategy="afterInteractive"
759-
/>
760-
<Script id="google-analytics" strategy="afterInteractive">
761-
{`
760+
<CookieBanner
761+
className="fixed bottom-4 right-4"
762+
show={showCookieBanner}
763+
onClickAccept={() => {
764+
setHasAcceptedCookies(true);
765+
setShowCookieBanner(false);
766+
}}
767+
onClickRefuse={() => {
768+
setShowCookieBanner(false);
769+
}}
770+
/>
771+
{hasAcceptedCookies && (
772+
<>
773+
<Script
774+
src={`https://www.googletagmanager.com/gtag/js?id=${gaTrackingId}`}
775+
strategy="afterInteractive"
776+
/>
777+
<Script id="google-analytics" strategy="afterInteractive">
778+
{`
762779
window.dataLayer = window.dataLayer || [];
763780
function gtag(){window.dataLayer.push(arguments);}
764781
gtag('js', new Date());
765782
766783
gtag('config', '${gaTrackingId}');
767784
`}
768-
</Script>
769-
</>
785+
</Script>
786+
</>
787+
)}
770788
</main>
771789
</>
772790
);
@@ -969,3 +987,55 @@ const Footer = () => {
969987
</div>
970988
);
971989
};
990+
991+
const CookieBanner = ({
992+
show,
993+
onClickAccept,
994+
onClickRefuse,
995+
className,
996+
}: {
997+
show: boolean;
998+
onClickAccept: () => void;
999+
onClickRefuse: () => void;
1000+
className?: string;
1001+
}) => {
1002+
return (
1003+
<Transition
1004+
show={show}
1005+
enter="transition-opacity s-duration-300"
1006+
appear={true}
1007+
enterFrom="opacity-0"
1008+
enterTo="opacity-100"
1009+
leave="transition-opacity duration-300"
1010+
leaveFrom="opacity-100"
1011+
leaveTo="opacity-0"
1012+
className={classNames(
1013+
"z-30 flex w-72 flex-col gap-3 rounded-xl border border-pink-100 bg-pink-50 p-4 shadow-xl",
1014+
className || ""
1015+
)}
1016+
>
1017+
<div className="text-sm font-normal text-element-900">
1018+
We use cookies to improve your experience on our site. By using our
1019+
service, you agree to our{" "}
1020+
<A variant="tertiary">
1021+
<Link href="/terms">Terms of Use</Link>
1022+
</A>
1023+
.
1024+
</div>
1025+
<div className="flex gap-2">
1026+
<Button
1027+
variant="primary"
1028+
size="sm"
1029+
label="Accept All"
1030+
onClick={onClickAccept}
1031+
/>
1032+
<Button
1033+
variant="tertiary"
1034+
size="sm"
1035+
label="Reject All"
1036+
onClick={onClickRefuse}
1037+
/>
1038+
</div>
1039+
</Transition>
1040+
);
1041+
};

0 commit comments

Comments
 (0)