Skip to content

Commit eb10d90

Browse files
committed
Add feature to show cookie consent message
1 parent b1dcce5 commit eb10d90

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/theme/Root.js

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import Clarity from '@microsoft/clarity';
3+
import CookieConsent from "react-cookie-consent";
34

4-
const projectId = "p90sis7r0o"
5-
Clarity.init(projectId);
5+
// Initialize Clarity with the given project ID
6+
const projectId = "p90sis7r0o";
67

7-
// Default implementation that can be customized
8-
export default function Root({children}) {
9-
return <>{children}</>;
8+
export default function Root({ children }) {
9+
useEffect(() => {
10+
if (typeof window !== "undefined") {
11+
// Initialize Clarity only on the client-side
12+
Clarity.init(projectId);
13+
14+
// Add an event listener for the consent event
15+
window.addEventListener("CookieConsent", () => {
16+
if (window.clarity) {
17+
window.clarity('consent');
18+
console.log("Clarity consent event triggered.");
19+
}
20+
});
21+
}
22+
}, []);
23+
24+
const handleConsentAccept = () => {
25+
// Dispatch a custom "CookieConsent" event when the user accepts cookies
26+
const consentEvent = new Event("CookieConsent");
27+
window.dispatchEvent(consentEvent);
28+
console.log("CookieConsent event dispatched.");
29+
};
30+
31+
return (
32+
<>
33+
{children}
34+
<div>
35+
<CookieConsent
36+
location="bottom"
37+
buttonText="I understand"
38+
style={{
39+
backgroundColor: "#080f53",
40+
padding: "20px",
41+
}}
42+
buttonStyle={{
43+
backgroundColor: "#fff",
44+
color: "#000",
45+
fontWeight: "500",
46+
fontFamily:
47+
"system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'",
48+
}}
49+
expires={150}
50+
onAccept={handleConsentAccept}
51+
>
52+
This website uses cookies to enhance the visitor experience. By continuing to use this website, you acknowledge that you have read and understood our <a href="https://www.scalar-labs.com/privacy-policy" style={{color: "white", textDecorationLine: "underline"}} target="_blank">privacy policy</a> and consent to the use of cookies to help improve your browsing experience and personalize content.
53+
</CookieConsent>
54+
</div>
55+
</>
56+
);
1057
}

0 commit comments

Comments
 (0)