-
Notifications
You must be signed in to change notification settings - Fork 2
/
CookieConsent.jsx
72 lines (66 loc) · 3 KB
/
CookieConsent.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use client";
import { CookieIcon } from "lucide-react";
import { Button } from "./ui/button";
import { useEffect, useState } from "react";
import { cn } from "@/lib/utils";
export default function CookieConsent({ demo = false, onAcceptCallback = () => { }, onDeclineCallback = () => { } }) {
const [isOpen, setIsOpen] = useState(false);
const [hide, setHide] = useState(false);
const accept = () => {
setIsOpen(false);
document.cookie = "cookieConsent=true; expires=Fri, 31 Dec 9999 23:59:59 GMT";
setTimeout(() => {
setHide(true);
}, 700);
onAcceptCallback();
};
const decline = () => {
setIsOpen(false);
setTimeout(() => {
setHide(true);
}, 700);
onDeclineCallback();
};
useEffect(() => {
try {
setIsOpen(true);
if (document.cookie.includes("cookieConsent=true")) {
if (!demo) {
setIsOpen(false);
setTimeout(() => {
setHide(true);
}, 700);
}
}
}
catch (e) {
// console.log("Error: ", e);
}
}, []);
return (
<div className={cn("fixed z-[200] bottom-0 left-0 right-0 sm:left-4 sm:bottom-4 w-full sm:max-w-md duration-700", !isOpen ? "transition-[opacity,transform] translate-y-8 opacity-0" : "transition-[opacity,transform] translate-y-0 opacity-100", hide && "hidden")}>
<div className="dark:bg-secondary bg-background rounded-md m-3 border border-border shadow-lg dark:shadow-none">
<div className="grid gap-2">
<div className="border-b border-border dark:border-background/20 h-14 flex items-center justify-between p-4">
<h1 className="text-lg font-medium">We use cookies</h1>
<CookieIcon className="h-[1.2rem] w-[1.2rem]" />
</div>
<div className="p-4">
<p className="text-sm font-normal text-start">
We use cookies to ensure you get the best experience on our website. For more information on how we use cookies, please see our cookie policy.
<br />
<br />
<span className="text-xs">By clicking "<span className="font-medium opacity-80">Accept</span>", you agree to our use of cookies.</span>
<br />
<a href="#" className="text-xs underline">Learn more.</a>
</p>
</div>
<div className="flex gap-2 p-4 py-5 border-t border-border dark:bg-background/20">
<Button onClick={accept} className="w-full">Accept</Button>
<Button onClick={decline} className="w-full" variant="secondary">Decline</Button>
</div>
</div>
</div>
</div>
)
}