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

feat: adds simpleanalytics and cookie consent #97

Merged
merged 5 commits into from
Jan 30, 2024
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"sass": "^1.59.3",
"sass-loader": "^13.2.1",
"typescript": "^5.0.2",
"webpack": "^5.76.3"
"webpack": "^5.76.3",
"react-cookie": "7.0.2"
},
"browserslist": {
"production": [
Expand Down
46 changes: 46 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/components/CookieConsent/CookieBanner.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";

const CookieBanner = ({ onClick }) => {
return (
<div aria-live="assertive" className="cookie-consent">
<div className="cookie-consent__wrapper">
<div>
This site uses cookies. <br />
</div>
<button className="cookie-consent__confirm" onClick={onClick}>
OK
</button>
</div>
</div>
);
};

export default CookieBanner;
43 changes: 43 additions & 0 deletions src/components/CookieConsent/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from "react";
import { useCookies } from "react-cookie";
import CookieBanner from "./CookieBanner";

const COOKIES_CONSENT_NAME = "cookie-consent-given";
const COOKIES_CONSENT_VALUE = 1;

export default function CookiesConsent() {
if (typeof window === "undefined") return null;
const [cookies, setCookie] = useCookies();
const [consentGiven, setIsConsentGiven] = React.useState(
cookies?.[COOKIES_CONSENT_NAME] === COOKIES_CONSENT_VALUE
);

return consentGiven ? (
<>
<script
async
defer
src="https://scripts.simpleanalyticscdn.com/latest.js"
></script>
<noscript>
<img
src="https://queue.simpleanalyticscdn.com/noscript.gif"
alt=""
referrerPolicy="no-referrer-when-downgrade"
/>
</noscript>
</>
) : (
<CookieBanner
onClick={() => {
const yearFromNow = new Date(
new Date().setFullYear(new Date().getFullYear() + 1)
);
setCookie(COOKIES_CONSENT_NAME, COOKIES_CONSENT_VALUE, {
expires: yearFromNow,
});
setIsConsentGiven(true);
}}
/>
);
}
73 changes: 69 additions & 4 deletions src/css/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
padding: 0 var(--ifm-pre-padding);
}

html[data-theme='dark'] .docusaurus-highlight-code-line {
yellowCrimsonGator marked this conversation as resolved.
Show resolved Hide resolved
html[data-theme="dark"] .docusaurus-highlight-code-line {
background-color: rgba(0, 0, 0, 0.3);
}

body {
font-family: 'IBM Plex Sans', sans-serif;
font-family: "IBM Plex Sans", sans-serif;
}

code,
pre {
font-family: 'IBM Plex Mono', monospace;
font-family: "IBM Plex Mono", monospace;
}

p {
Expand All @@ -45,7 +45,7 @@ p {
}

h1 {
font-family: 'IBM Plex Mono', monospace;
font-family: "IBM Plex Mono", monospace;
font-style: normal;
}

Expand Down Expand Up @@ -127,3 +127,68 @@ h6 {
height: 15px;
}
}

.cookie-consent {
position: fixed;
bottom: 0;
right: 0;
z-index: 50;
padding: 0 24px 24px 24px;
}

@media screen and (max-width: 640px) {
.cookie-consent {
width: 100%;
padding: 0 16px 16px 16px;
}
}

.cookie-consent__wrapper {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
overflow: hidden;
padding: 14px;
background: var(--ifm-navbar-background-color);
border-radius: 12px;
box-shadow: 0 0px 2px rgba(0, 0, 0, 0.25);
color: var(--ifm-font-color-base);
font-weight: 500;
font-size: 15px;
}

.cookie-consent__confirm {
padding-top: 0.625rem;
padding-bottom: 0.625rem;
padding-left: 1.5rem;
padding-right: 1.5rem;
border-radius: 0.5rem;
font-size: 0.875rem;
line-height: 1.25rem;
cursor: pointer;
border: none;
font-weight: 600;
}

html[data-theme="dark"] .cookie-consent__confirm {
background: white;
color: rgb(0, 0, 0, 0.8);
}
html[data-theme="dark"] .cookie-consent__confirm:hover {
background: rgb(255, 255, 255, 0.9);
}
html[data-theme="dark"] .cookie-consent__confirm:active {
outline: 2px solid rgb(255, 255, 255, 0.6);
}

html[data-theme="light"] .cookie-consent__confirm {
background: black;
color: rgb(255, 255, 255);
}
html[data-theme="light"] .cookie-consent__confirm:hover {
background: rgb(0, 0, 0, 0.8);
}
html[data-theme="light"] .cookie-consent__confirm:active {
outline: 2px solid rgb(0, 0, 0, 0.6);
}
11 changes: 11 additions & 0 deletions src/theme/Root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import CookiesConsent from "../components/CookieConsent";

export default function Root({ children }) {
return (
<>
{children}
<CookiesConsent />
</>
);
}
Loading